-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
128 lines (119 loc) · 4.67 KB
/
sample.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import ollama
import asyncio
def get_spouse_name(name: str) -> str:
"Get the name of a person's spouse"
names = {
"Ranveer": "Deepika",
"Aalia": "Ranbir",
"Raja": "Rani"
}
return json.dumps(names.get(name.lower(), "NA"))
def order_pizza(arguments: object) -> str:
return ("A " + arguments["size"] + " pizza has been ordered")
async def run(model: str, user_input: str):
client = ollama.AsyncClient()
# Initialize conversation with a user query
messages = [
{
"role": "system",
"content": ' \
You are a helpful assistant with access to the following functions. Use them if required. \
If a function call returns "NA", then attempt answering the same question using your knowledge. \
{\
"name": "order_pizza",\
"description": "Order a pizza with custom toppings",\
"parameters": {\
"type": "object",\
"properties": {\
"size": {\
"type": "string",\
"description": "Size of the pizza (small, medium, large)"\
},\
"crust": {\
"type": "string",\
"description": "Type of crust (thin, regular, thick)"\
},\
"toppings": {\
"type": "array",\
"items": {\
"type": "string"\
},\
"description": "List of toppings for the pizza"\
},\
"delivery_address": {\
"type": "string",\
"description": "Address where the pizza should be delivered"\
}\
},\
"required": [\
"size",\
"crust",\
"toppings",\
"delivery_address"\
]\
}\
}, \
{ \
"name": "get_spouse_name", \
"description": "Get the name of the spouse of a person", \
"parameters": { \
"type": "object", \
"properties": { \
"name": { \
"type": "string", \
"description": "The name of a person whose spouse needs to be identified.", \
}, \
},\
"required": ["name"]\
}\
}'
},
{
"role": "user",
"content": user_input
}
]
# First API call: Send the query and function description to the model
response = await client.chat(
model=model,
messages=messages
)
# Add the model's response to the conversation history
messages.append(response["message"])
# Check if the model decided to use the provided function
response_content = response["message"].content
if "<functioncall>" in response_content:
print("Function called!")
function_obj = response_content[14:]
corrected_data = function_obj.replace("\'", "").replace("'", '"').replace('"{"', '{"').replace('"}"', '}')
function_def = json.loads(corrected_data)
if function_def["name"] == "order_pizza":
result = order_pizza(function_def["arguments"])
print(result)
messages.append(
{
"role": "tool",
"content": result,
}
)
elif function_def["name"] == "get_spouse_name":
result = get_spouse_name(function_def["arguments"]["name"])
print(result)
messages.append({
"role": "tool",
"content": result
})
else:
print("Unknown function call detected!")
else:
print("\nThe model didn't use the function. Its response was:")
print(response["message"]["content"])
return
while True:
user_input = input("\n Please ask=> ")
if not user_input:
user_input = "What is the capital of India?"
if user_input.lower() == "/bye":
break
asyncio.run(run("calebfahlgren/natural-functions", user_input))