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

Add tools to text chat #196

Merged
merged 11 commits into from
May 15, 2024
Merged

Add tools to text chat #196

merged 11 commits into from
May 15, 2024

Conversation

KyrianC
Copy link
Collaborator

@KyrianC KyrianC commented May 15, 2024

flow

1. Define tools according to your own functions

Tools must be defined accoding to json-schema definition

function get_weather(location: str, units: Literal["Celsius", "Fahrenheit"] = "Celsius"):
  ...

tools = [
    {
        "name": "get_weather",
        "description": "Get The weather data from a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "description": "The geographical location for which weather information is requested.",
                    "type": "string",
                },
                "units": {
                    "description": "The units of measurement for the temperature.",
                    "type": "string",
                    "enum": ["Celsius", "Fahrenheit"],
                    "default": "Celsius",
                },
            },
            "required": ["location"],
        },
    }
]

map_tool_to_func = {
  "get_weather": get_weather
}

2. Ask chat for question related with tool, and pass tool_definitions

tool_call_result = chat(
    text="What is the weather in Brest?",
    chatbot_global_action=None,
    previous_history=history,
    temperature=1,
    max_tokens=1000,
    model=model,
    available_tools=tools,
)

messages = tool_call_result.standardized_response.message
tool_calls = messages[1].tool_calls

# IMPORTANT: providers needs you to add to history otherwise it won't work
history = [msg.model_dump() for msg in messages]

print(tool_calls)

output:
[{"id": "8KS3Mz2wmYGqUcfncF0ymjAUxozUH", "name": "get_weather", "arguments": '{"location": "Brest"}'}]

3. Call your own tool with passed arguments

tool_results = []
for tool_call in tool_calls 
    arguments = json.loads(tool_call['arguments'])
    tool_call_result = map_tool_to_func[tool["name"]](**arguments)
    # {"temperature": 10, 'units': "Celsius", "weather": "rainy"}
    tool_results.append({"id": tool_call["id"], "result": tool_call_result})

4. Get text response back from Chat

chat_result = chat(
    text="",
    chatbot_global_action=None,
    previous_history=history,
    temperature=1,
    max_tokens=1000,
    model=model,
    tool_results=tool_results,
)

print(chat_result.standardized_response.generated_text)

output:
The Weather in Brest is currently rainy with a temperature of 10 degrees Celsius.

@KyrianC KyrianC requested a review from Daggx May 15, 2024 09:19
@KyrianC
Copy link
Collaborator Author

KyrianC commented May 15, 2024

Don't hesitate to tell me if you think the flow is not simple enough.
like we must add previous messages to history for it to work.

Another way would be to make the user repeat the first request, and just add tool_resultsto it. so I could reconstruct a history from that. That's what cohere is doing.

@Daggx
Copy link
Contributor

Daggx commented May 15, 2024

Don't hesitate to tell me if you think the flow is not simple enough. like we must add previous messages to history for it to work.

Another way would be to make the user repeat the first request, and just add tool_resultsto it. so I could reconstruct a history from that. That's what cohere is doing.

The flow may not be simple but if the users are familiar with how function tooling works with other providers than its seems clear

if user doesn't pass the right history, or if he send the wrong tool id
in `tool_results`
@KyrianC KyrianC merged commit 7f3a948 into master May 15, 2024
2 checks passed
@KyrianC KyrianC deleted the feat/chat-tools branch May 15, 2024 14:03
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.

2 participants