-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathtool_calling.py
More file actions
51 lines (39 loc) · 1.1 KB
/
tool_calling.py
File metadata and controls
51 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import math
from ollama import chat
# Define a tool as a Python function
def square_root(number: float) -> float:
"""Calculate the square root of a number.
Args:
number: The number to calculate the square root for.
Returns:
The square root of the number.
"""
return math.sqrt(number)
messages = [
{
"role": "user",
"content": "What is the square root of 36?",
}
]
response = chat(
model="llama3.2:latest",
messages=messages,
tools=[square_root], # Pass the tools along with the prompt
)
# Append the response for context
messages.append(response.message)
if response.message.tool_calls:
tool = response.message.tool_calls[0]
# Call the tool
result = square_root(float(tool.function.arguments["number"]))
# Append the tool result
messages.append(
{
"role": "tool",
"tool_name": tool.function.name,
"content": str(result),
}
)
# Obtain the final answer
final_response = chat(model="llama3.2:latest", messages=messages)
print(final_response.message.content)