[Feature] Function Calling (#2544)

Co-authored-by: Haoyu Wang <120358163+HaoyuWang4188@users.noreply.github.com>
This commit is contained in:
Tanjiro
2024-12-29 11:28:52 +05:30
committed by GitHub
parent fd28640dc5
commit 8ee9a8501a
5 changed files with 368 additions and 2 deletions

View File

@@ -622,6 +622,58 @@ class TestOpenAIServerEBNF(unittest.TestCase):
text, pattern, f"Text '{text}' not matching the EBNF strict JSON shape"
)
def test_function_calling_format(self):
client = openai.Client(api_key=self.api_key, base_url=self.base_url)
tools = [
{
"type": "function",
"function": {
"name": "add",
"description": "Compute the sum of two numbers",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "int",
"description": "A number",
},
"b": {
"type": "int",
"description": "A number",
},
},
"required": ["a", "b"],
},
},
}
]
messages = [{"role": "user", "content": "Compute (3+5)"}]
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=0.8,
top_p=0.8,
stream=False,
tools=tools,
)
content = response.choices[0].message.content
tool_calls = response.choices[0].message.tool_calls
assert (
content is None
), "When tools provided by the response, content should be None"
assert (
isinstance(tool_calls, list) and len(tool_calls) > 0
), "Format not matched, tool_calls should be a list"
function_name = tool_calls[0].function.name
assert (
function_name == "add"
), "Function name should be add for the above response"
if __name__ == "__main__":
unittest.main()