-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (150 loc) · 5.58 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json
import ollama
import asyncio
def get_antonyms(word: str) -> str:
"Get the antonyms of the any given word"
words = {
"hot": "cold",
"small": "big",
"weak": "strong",
"light": "dark",
"lighten": "darken",
"dark": "bright",
}
return json.dumps(words.get(word, "Not available in database"))
# In a real application, this would fetch data from a live database or API
def get_flight_times(departure: str, arrival: str) -> str:
flights = {
"NYC-LAX": {
"departure": "08:00 AM",
"arrival": "11:30 AM",
"duration": "5h 30m",
},
"LAX-NYC": {
"departure": "02:00 PM",
"arrival": "10:30 PM",
"duration": "5h 30m",
},
"LHR-JFK": {
"departure": "10:00 AM",
"arrival": "01:00 PM",
"duration": "8h 00m",
},
"JFK-LHR": {
"departure": "09:00 PM",
"arrival": "09:00 AM",
"duration": "7h 00m",
},
"CDG-DXB": {
"departure": "11:00 AM",
"arrival": "08:00 PM",
"duration": "6h 00m",
},
"DXB-CDG": {
"departure": "03:00 AM",
"arrival": "07:30 AM",
"duration": "7h 30m",
},
}
key = f"{departure}-{arrival}".upper()
return json.dumps(flights.get(key, {"error": "Flight not found"}))
async def run(model: str, user_input: str):
client = ollama.AsyncClient()
# Initialize conversation with a user query
messages = [
{
"role": "user",
"content": user_input,
# "content": "What is the capital of India?",
}
]
# First API call: Send the query and function description to the model
response = await client.chat(
model=model,
messages=messages,
tools=[
{
"type": "function",
"function": {
"name": "get_flight_times",
"description": "Get the flight times between two cities",
"parameters": {
"type": "object",
"properties": {
"departure": {
"type": "string",
"description": "The departure city (airport code)",
},
"arrival": {
"type": "string",
"description": "The arrival city (airport code)",
},
},
"required": ["departure", "arrival"],
},
},
},
{
"type": "function",
"function": {
"name": "get_antonyms",
"description": "Get the antonyms of any given words",
"parameters": {
"type": "object",
"properties": {
"word": {
"type": "string",
"description": "The word for which the opposite is required.",
},
},
"required": ["word"],
},
},
},
],
)
# print(f"Response: {response}")
# Add the model's response to the conversation history
messages.append(response["message"])
# print(f"Conversation history:\n{messages}")
# Check if the model decided to use the provided function
if not response["message"].get("tool_calls"):
print("\nThe model didn't use the function. Its response was:")
print(response["message"]["content"])
return
if response["message"].get("tool_calls"):
# print(f"\nThe model used some tools")
available_functions = {
"get_flight_times": get_flight_times,
"get_antonyms": get_antonyms,
}
# print(f"\navailable_function: {available_functions}")
for tool in response["message"]["tool_calls"]:
# print(f"available tools: {tool}")
# tool: {'function': {'name': 'get_flight_times', 'arguments': {'arrival': 'LAX', 'departure': 'NYC'}}}
function_to_call = available_functions[tool["function"]["name"]]
print(f"function to call: {function_to_call}")
if function_to_call == get_flight_times:
function_response = function_to_call(
tool["function"]["arguments"]["departure"],
tool["function"]["arguments"]["arrival"],
)
print(f"function response: {function_response}")
elif function_to_call == get_antonyms:
function_response = function_to_call(
tool["function"]["arguments"]["word"],
)
print(f"function response: {function_response}")
messages.append(
{
"role": "tool",
"content": function_response,
}
)
while True:
user_input = input("\n Please ask=> ")
if not user_input:
user_input = "What is the flight time from NYC to LAX?"
if user_input.lower() == "exit":
break
asyncio.run(run("llama3.1", user_input))