Skip to content

Commit 85ee34b

Browse files
Python: Implement Function calling for Chat (#2356)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> I built support for function calling into SK! Related to/fixes: - #2315 - #2175 - #1450 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> This implementation builds on top of all the existing pieces, but did require some major work, so feel free to comment on where that is or is not appropriate. - Added a `ChatMessage` class to capture the relevant pieces of function calling (name and content) - Added a `complete_chat_with_functions_async` into OpenAIChatCompletions class - Added a `function_call` field to ChatRequestSettings class - Added several helper functions and smaller changes - Added a sample with updated core_skill that uses function calling to demonstrate - Added a second sample that shows how to use function_calling with non-sk functions. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Abby Harrison <[email protected]>
1 parent 9606215 commit 85ee34b

28 files changed

Lines changed: 1023 additions & 143 deletions

‎python/.vscode/settings.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
"OPENAI",
1919
"skfunction"
2020
],
21+
"python.testing.pytestArgs": [
22+
"tests"
23+
],
24+
"python.testing.unittestEnabled": false,
25+
"python.testing.pytestEnabled": true,
2126
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
import os
5+
from typing import Tuple
6+
7+
import semantic_kernel as sk
8+
import semantic_kernel.connectors.ai.open_ai as sk_oai
9+
from semantic_kernel.connectors.ai.open_ai.semantic_functions.open_ai_chat_prompt_template import (
10+
OpenAIChatPromptTemplate,
11+
)
12+
from semantic_kernel.connectors.ai.open_ai.utils import (
13+
chat_completion_with_function_call,
14+
get_function_calling_object,
15+
)
16+
from semantic_kernel.core_skills import MathSkill
17+
18+
system_message = """
19+
You are a chat bot. Your name is Mosscap and
20+
you have one goal: figure out what people need.
21+
Your full name, should you need to know it, is
22+
Splendid Speckled Mosscap. You communicate
23+
effectively, but you tend to answer with long
24+
flowery prose. You are also a math wizard,
25+
especially for adding and subtracting.
26+
You also excel at joke telling, where your tone is often sarcastic.
27+
Once you have the answer I am looking for,
28+
you will return a full answer to me as soon as possible.
29+
"""
30+
31+
kernel = sk.Kernel()
32+
33+
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
34+
api_version = "2023-07-01-preview"
35+
kernel.add_chat_service(
36+
"chat-gpt",
37+
sk_oai.AzureChatCompletion(
38+
deployment_name,
39+
endpoint,
40+
api_key,
41+
api_version=api_version,
42+
),
43+
)
44+
45+
skills_directory = os.path.join(__file__, "../../../../samples/skills")
46+
# adding skills to the kernel
47+
# the joke skill in the FunSkills is a semantic skill and has the function calling disabled.
48+
kernel.import_semantic_skill_from_directory(skills_directory, "FunSkill")
49+
# the math skill is a core skill and has the function calling enabled.
50+
kernel.import_skill(MathSkill(), skill_name="math")
51+
52+
# enabling or disabling function calling is done by setting the function_call parameter for the completion.
53+
# when the function_call parameter is set to "auto" the model will decide which function to use, if any.
54+
# if you only want to use a specific function, set the name of that function in this parameter,
55+
# the format for that is 'SkillName-FunctionName', (i.e. 'math-Add').
56+
# if the model or api version do not support this you will get an error.
57+
prompt_config = sk.PromptTemplateConfig.from_completion_parameters(
58+
max_tokens=2000,
59+
temperature=0.7,
60+
top_p=0.8,
61+
function_call="auto",
62+
chat_system_prompt=system_message,
63+
)
64+
prompt_template = OpenAIChatPromptTemplate(
65+
"{{$user_input}}", kernel.prompt_template_engine, prompt_config
66+
)
67+
prompt_template.add_user_message("Hi there, who are you?")
68+
prompt_template.add_assistant_message(
69+
"I am Mosscap, a chat bot. I'm trying to figure out what people need."
70+
)
71+
72+
function_config = sk.SemanticFunctionConfig(prompt_config, prompt_template)
73+
chat_function = kernel.register_semantic_function("ChatBot", "Chat", function_config)
74+
75+
# calling the chat, you could add a overloaded version of the settings here,
76+
# to enable or disable function calling or set the function calling to a specific skill.
77+
# see the openai_function_calling example for how to use this with a unrelated function definition
78+
filter = {"exclude_skill": ["ChatBot"]}
79+
functions = get_function_calling_object(kernel, filter)
80+
81+
82+
async def chat(context: sk.SKContext) -> Tuple[bool, sk.SKContext]:
83+
try:
84+
user_input = input("User:> ")
85+
context.variables["user_input"] = user_input
86+
except KeyboardInterrupt:
87+
print("\n\nExiting chat...")
88+
return False, None
89+
except EOFError:
90+
print("\n\nExiting chat...")
91+
return False, None
92+
93+
if user_input == "exit":
94+
print("\n\nExiting chat...")
95+
return False, None
96+
97+
context = await chat_completion_with_function_call(
98+
kernel,
99+
chat_skill_name="ChatBot",
100+
chat_function_name="Chat",
101+
context=context,
102+
functions=functions,
103+
)
104+
print(f"Mosscap:> {context.result}")
105+
return True, context
106+
107+
108+
async def main() -> None:
109+
chatting = True
110+
context = kernel.create_new_context()
111+
print(
112+
"Welcome to the chat bot!\
113+
\n Type 'exit' to exit.\
114+
\n Try a math question to see the function calling in action (i.e. what is 3+3?)."
115+
)
116+
while chatting:
117+
chatting, context = await chat(context)
118+
119+
120+
if __name__ == "__main__":
121+
asyncio.run(main())
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
import os
5+
6+
import semantic_kernel as sk
7+
import semantic_kernel.connectors.ai.open_ai as sk_oai
8+
from semantic_kernel.core_skills import MathSkill
9+
10+
system_message = """
11+
You are a chat bot. Your name is Mosscap and
12+
you have one goal: figure out what people need.
13+
Your full name, should you need to know it, is
14+
Splendid Speckled Mosscap. You communicate
15+
effectively, but you tend to answer with long
16+
flowery prose. You are also a math wizard,
17+
especially for adding and subtracting.
18+
You also excel at joke telling, where your tone is often sarcastic.
19+
Once you have the answer I am looking for,
20+
you will return a full answer to me as soon as possible.
21+
"""
22+
23+
kernel = sk.Kernel()
24+
25+
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env()
26+
api_version = "2023-07-01-preview"
27+
kernel.add_chat_service(
28+
"chat-gpt",
29+
sk_oai.AzureChatCompletion(
30+
deployment_name,
31+
endpoint,
32+
api_key,
33+
api_version=api_version,
34+
),
35+
)
36+
37+
skills_directory = os.path.join(__file__, "../../../../samples/skills")
38+
# adding skills to the kernel
39+
# the joke skill in the FunSkills is a semantic skill and has the function calling disabled.
40+
kernel.import_semantic_skill_from_directory(skills_directory, "FunSkill")
41+
# the math skill is a core skill and has the function calling enabled.
42+
kernel.import_skill(MathSkill(), skill_name="math")
43+
44+
# enabling or disabling function calling is done by setting the function_call parameter for the completion.
45+
# when the function_call parameter is set to "auto" the model will decide which function to use, if any.
46+
# if you only want to use a specific function, set the name of that function in this parameter,
47+
# the format for that is 'SkillName-FunctionName', (i.e. 'math-Add').
48+
# if the model or api version do not support this you will get an error.
49+
prompt_config = sk.PromptTemplateConfig.from_completion_parameters(
50+
max_tokens=2000,
51+
temperature=0.7,
52+
top_p=0.8,
53+
function_call="auto",
54+
chat_system_prompt=system_message,
55+
)
56+
prompt_template = sk.ChatPromptTemplate(
57+
"{{$user_input}}", kernel.prompt_template_engine, prompt_config
58+
)
59+
prompt_template.add_user_message("Hi there, who are you?")
60+
prompt_template.add_assistant_message(
61+
"I am Mosscap, a chat bot. I'm trying to figure out what people need."
62+
)
63+
64+
function_config = sk.SemanticFunctionConfig(prompt_config, prompt_template)
65+
chat_function = kernel.register_semantic_function("ChatBot", "Chat", function_config)
66+
# define the functions available
67+
functions = [
68+
{
69+
"name": "search_hotels",
70+
"description": "Retrieves hotels from the search index based on the parameters provided",
71+
"parameters": {
72+
"type": "object",
73+
"properties": {
74+
"location": {
75+
"type": "string",
76+
"description": "The location of the hotel (i.e. Seattle, WA)",
77+
},
78+
"max_price": {
79+
"type": "number",
80+
"description": "The maximum price for the hotel",
81+
},
82+
"features": {
83+
"type": "string",
84+
"description": "A comma separated list of features (i.e. beachfront, free wifi, etc.)",
85+
},
86+
},
87+
"required": ["location"],
88+
},
89+
}
90+
]
91+
92+
93+
async def main() -> None:
94+
context = kernel.create_new_context()
95+
context.variables[
96+
"user_input"
97+
] = "I want to find a hotel in Seattle with free wifi and a pool."
98+
99+
context = await chat_function.invoke_async(context=context, functions=functions)
100+
if function_call := context.pop_function_call():
101+
print(f"Function to be called: {function_call.name}")
102+
print(f"Function parameters: \n{function_call.arguments}")
103+
return
104+
print("No function was called")
105+
print(f"Output was: {str(context)}")
106+
107+
108+
if __name__ == "__main__":
109+
asyncio.run(main())

‎python/semantic_kernel/connectors/ai/chat_completion_client_base.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
from abc import ABC, abstractmethod
44
from logging import Logger
5-
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
5+
from typing import TYPE_CHECKING, List, Optional, Union
66

77
if TYPE_CHECKING:
88
from semantic_kernel.connectors.ai.chat_request_settings import ChatRequestSettings
9+
from semantic_kernel.models.chat.chat_message import ChatMessage
910

1011

1112
class ChatCompletionClientBase(ABC):
1213
@abstractmethod
1314
async def complete_chat_async(
1415
self,
15-
messages: List[Tuple[str, str]],
16+
messages: List["ChatMessage"],
1617
settings: "ChatRequestSettings",
1718
logger: Optional[Logger] = None,
1819
) -> Union[str, List[str]]:
1920
"""
2021
This is the method that is called from the kernel to get a response from a chat-optimized LLM.
2122
2223
Arguments:
23-
messages {List[Tuple[str, str]]} -- A list of tuples, where each tuple is
24-
comprised of a speaker ID and a message.
24+
messages {List[ChatMessage]} -- A list of chat messages, that can be rendered into a
25+
set of messages, from system, user, assistant and function.
2526
settings {ChatRequestSettings} -- Settings for the request.
2627
logger {Logger} -- A logger to use for logging.
2728
@@ -33,16 +34,16 @@ async def complete_chat_async(
3334
@abstractmethod
3435
async def complete_chat_stream_async(
3536
self,
36-
messages: List[Tuple[str, str]],
37+
messages: List["ChatMessage"],
3738
settings: "ChatRequestSettings",
3839
logger: Optional[Logger] = None,
3940
):
4041
"""
4142
This is the method that is called from the kernel to get a stream response from a chat-optimized LLM.
4243
4344
Arguments:
44-
messages {List[Tuple[str, str]]} -- A list of tuples, where each tuple is
45-
comprised of a speaker ID and a message.
45+
messages {List[ChatMessage]} -- A list of chat messages, that can be rendered into a
46+
set of messages, from system, user, assistant and function.
4647
settings {ChatRequestSettings} -- Settings for the request.
4748
logger {Logger} -- A logger to use for logging.
4849

‎python/semantic_kernel/connectors/ai/chat_request_settings.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

33
from dataclasses import dataclass, field
4-
from typing import TYPE_CHECKING, Dict, List
4+
from typing import TYPE_CHECKING, Dict, List, Optional
55

66
if TYPE_CHECKING:
77
from semantic_kernel.semantic_functions.prompt_template_config import (
@@ -19,6 +19,7 @@ class ChatRequestSettings:
1919
max_tokens: int = 256
2020
token_selection_biases: Dict[int, int] = field(default_factory=dict)
2121
stop_sequences: List[str] = field(default_factory=list)
22+
function_call: Optional[str] = None
2223

2324
def update_from_completion_config(
2425
self, completion_config: "PromptTemplateConfig.CompletionConfig"
@@ -31,6 +32,11 @@ def update_from_completion_config(
3132
self.presence_penalty = completion_config.presence_penalty
3233
self.frequency_penalty = completion_config.frequency_penalty
3334
self.token_selection_biases = completion_config.token_selection_biases
35+
self.function_call = (
36+
completion_config.function_call
37+
if hasattr(completion_config, "function_call")
38+
else None
39+
)
3440

3541
@staticmethod
3642
def from_completion_config(
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Class to hold chat messages."""
2+
import json
3+
from typing import Dict, Tuple
4+
5+
from semantic_kernel.orchestration.context_variables import ContextVariables
6+
from semantic_kernel.sk_pydantic import SKBaseModel
7+
8+
9+
class FunctionCall(SKBaseModel):
10+
"""Class to hold a function call response."""
11+
12+
name: str
13+
arguments: str
14+
15+
def parse_arguments(self) -> Dict[str, str]:
16+
"""Parse the arguments into a dictionary."""
17+
try:
18+
return json.loads(self.arguments)
19+
except json.JSONDecodeError:
20+
return None
21+
22+
def to_context_variables(self) -> ContextVariables:
23+
"""Return the arguments as a ContextVariables instance."""
24+
args = self.parse_arguments()
25+
return ContextVariables(variables={k.lower(): v for k, v in args.items()})
26+
27+
def split_name(self) -> Tuple[str, str]:
28+
"""Split the name into a skill and function name."""
29+
if "-" not in self.name:
30+
return None, self.name
31+
return self.name.split("-")
32+
33+
def split_name_dict(self) -> dict:
34+
"""Split the name into a skill and function name."""
35+
parts = self.split_name()
36+
return {"skill_name": parts[0], "function_name": parts[1]}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Class to hold chat messages."""
2+
from typing import Optional
3+
4+
from semantic_kernel.connectors.ai.open_ai.models.chat.function_call import (
5+
FunctionCall,
6+
)
7+
from semantic_kernel.models.chat.chat_message import ChatMessage
8+
9+
10+
class OpenAIChatMessage(ChatMessage):
11+
"""Class to hold openai chat messages, which might include name and function_call fields."""
12+
13+
name: Optional[str] = None
14+
function_call: Optional[FunctionCall] = None

0 commit comments

Comments
 (0)