Skip to content
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

provide tools for multiple function calling #122

Merged
merged 5 commits into from
Jan 16, 2024
Merged

Conversation

rgbkrk
Copy link
Owner

@rgbkrk rgbkrk commented Jan 16, 2024

Partially closes #118. I went after just making it easy to export the listing of tools from a FunctionRegistry. Follow on in ChatLab's Chat can come next.

An example chat loop using tools:

async def chatloop(initial_messages):
    buffer = initial_messages.copy()

    resp = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=initial_messages,

        # Pass in the tools from the function registry. The model will choose
        # whether it uses 0, 1, 2, or N many tools.
        tools=fr.tools,
        tool_choice="auto"
    )

    message = resp.choices[0].message
    buffer.append(message)

    yield message

    # call each of the tools
    if message.tool_calls is not None:
        for tool in message.tool_calls:
            result = await fr.call(tool.function.name, tool.function.arguments)

            # An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'.
            tool_call_response = {
                "tool_call_id": tool.id,
                "role": "tool",
                "name": tool.function.name,
                "content": str(result)
            }
            yield tool_call_response
            buffer.append(tool_call_response)
        
        # Once all tools have been called, call the model again
        async for m in chatloop(buffer):
            yield m

In use:

image image

@rgbkrk
Copy link
Owner Author

rgbkrk commented Jan 16, 2024

Ran across this openai/openai-python#1078

@rgbkrk rgbkrk merged commit 93b2a11 into main Jan 16, 2024
8 checks passed
@rgbkrk rgbkrk deleted the parallel-tool-calling branch January 16, 2024 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support multiple function calling
1 participant