-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_calling.py
50 lines (43 loc) · 1.17 KB
/
function_calling.py
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
import ollama
def print_price(p):
# Simulate API call to get stock price
print(p)
funcs = {
"print_price": print_price
}
tools = [
{
"type": "function",
"function": {
"name": "print_price",
"description": "print price on the console log",
"parameters": {
"type": "object",
"properties": {
"CNY": {
"type": "float",
"description": "人民币"
},
"DOLLAR": {
"type": "float",
"description": "美元"
}
},
"required": ["CNY", "DOLLAR"]
}
}
}
]
response = ollama.chat(model='llama3.1', messages=[
{
'role': 'user',
'content': '一部手机的平均价格',
},
], tools=tools)
tool_calls = response['message']['tool_calls']
for tool_call in tool_calls:
print(tool_call)
if tool_call['function']:
params = tool_call['function']['arguments']
func_name = tool_call['function']['name']
funcs[func_name](params)