-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tool/function calling for chat and chat_async #44
Conversation
Added a bunch more features to this! Significant refactoring to prevent code duplicationWe were previously duplicating a lot of code between the OpenAI is an slight exception to this, where we have a bit of duplication across Simplified way to call toolsWe can now simplify pass a list of functions to For example, we can now do from pydantic import BaseModel
class Numbers(BaseModel):
a: int = 0
b: int = 0
def numsum(input: Numbers):
"""
This function return the sum of two numbers
"""
return input.a + input.b
def numprod(input: Numbers):
"""
This function return the product of two numbers
"""
return input.a * input.b
tools = [numsum, numprod]
result = await chat_async(
model="gpt-4o",
messages=[
{
"role": "user",
"content": "What is the product of 31283 and 2323, added to 5? Return only the final answer, nothing else.",},
],
tools=tools,
)
print(result.content) # '72670414' Under the hood, we are converting the function to OpenAI's function definition spec, using Support for tool execution and chainingWe can now execute tools (see above) directly in Additionally, we set Specifically, this is done by first passing the entire tool call as a message, followed by the result of the tool. Like so. # Append the tool calls as an assistant response
request_params["messages"].append({
"role": "assistant",
"tool_calls": message.tool_calls,
})
# Append the tool message
request_params["messages"].append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": str(result),
}
) Tests for tool callsWe have now tool calls to our tests (and Github CI) as well. To run these tests, you can just run There are 3 tests here:
|
Merging this since Gemini seems to be blocking the server for Github Actions rn, hence the failed action 🙄 |
chat
andchat_async
and their respective OpenAI and Anthropic functions.tools
takes in a list of function schemas whiletool_choice
indicates whether the model is forced to use a tool.