-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
226 lines (190 loc) · 7.36 KB
/
Copy pathmain.py
File metadata and controls
226 lines (190 loc) · 7.36 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import os
import json
from dotenv import load_dotenv
from openai import OpenAI
from serpapi import Client
load_dotenv()
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
OPENAI_API_ENDPOINT = os.environ.get("OPENAI_API_ENDPOINT")
if OPENAI_API_KEY is None or OPENAI_API_KEY == "":
raise "OPENAI_API_KEY must be set!"
SERPAPI_KEY = os.environ["SERPAPI_KEY"]
if SERPAPI_KEY is None or SERPAPI_KEY == "":
raise "SERPAPI_KEY must be set!"
client = OpenAI(api_key=OPENAI_API_KEY, base_url=OPENAI_API_ENDPOINT)
search_client = Client(api_key=SERPAPI_KEY)
def get_temperature(location):
print("TOOL CALL: Get Temperature")
print(f"TOOL INPUT: {location}")
return f"Temperature for {location}: 29.5°C"
def convert_temperature(value, unit):
print("TOOL CALL: Convert Temperature")
print(f"TOOL INPUT: Value={value}, Unit={unit}")
try:
val = float(value)
if unit.upper() == "C":
converted_value = (val * 9 / 5) + 32
return f"{value}°C is {converted_value:.1f}°F"
elif unit.upper() == "F":
converted_value = (val - 32) * 5 / 9
return f"{value}°F is {converted_value:.1f}°C"
else:
return "Invalid unit. Please specify 'C' for Celsius or 'F' for Fahrenheit."
except ValueError:
return "Invalid temperature value provided."
def search_web(search_term):
print("TOOL CALL: Search Web")
print(f"TOOL INPUT: {search_term}")
params = {
"engine": "google",
"q": search_term,
"hl": "en",
"gl": "us",
"google_domain": "google.com",
"api_key": SERPAPI_KEY,
"num": "10",
}
result = search_client.search(**params)
return json.dumps(result.as_dict()["organic_results"])
def get_ai_response(user_prompt, history):
tools = [
{
"type": "function",
"function": {
"name": "get_temperature",
"description": "Get the temperature for a specified location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "Name of the place you want to get the temperature for.",
}
},
"required": ["location"],
"additionalProperties": False,
},
},
},
{
"type": "function",
"function": {
"name": "convert_temperature",
"description": "Convert temperature between Celsius and Fahrenheit.",
"parameters": {
"type": "object",
"properties": {
"value": {
"type": "number",
"description": "The temperature value to convert.",
},
"unit": {
"type": "string",
"enum": ["C", "F"],
"description": "The unit of the input temperature ('C' for Celsius, 'F' for Fahrenheit).",
},
},
"required": ["value", "unit"],
"additionalProperties": False,
},
},
},
{
"type": "function",
"function": {
"name": "search_web",
"description": "Search the web for a specified search term.",
"parameters": {
"type": "object",
"properties": {
"search_term": {
"type": "string",
"description": "The search term you want to search the web for.",
}
},
"required": ["search_term"],
"additionalProperties": False,
},
},
},
]
messages = history.copy()
response = client.chat.completions.create(
model=os.environ.get("OPENAI_MODEL_NAME"),
messages=messages,
tools=tools,
tool_choice="auto"
)
# Extract the response message
response_message = response.choices[0].message
# Check if there's a tool call
if response_message.tool_calls:
tool_call = response_message.tool_calls[0]
tool_name = tool_call.function.name
tool_result = "Tool use failed or yielded no result."
if tool_name == "get_temperature":
location = json.loads(tool_call.function.arguments)["location"]
tool_result = get_temperature(location)
if tool_name == "search_web":
search_term = json.loads(tool_call.function.arguments)["search_term"]
tool_result = search_web(search_term)
if tool_name == "convert_temperature":
args = json.loads(tool_call.function.arguments)
value = args.get("value")
unit = args.get("unit")
if value is not None and unit:
tool_result = convert_temperature(value, unit)
else:
tool_result = "Missing value or unit for temperature conversion."
history.append(
{
"role": "assistant",
"content": f"Tool call: {tool_name}. Result: {tool_result}.",
}
)
history.append({
"role": "user",
"content": f"Based on the chat history, which may include tool calls & results, please answer this question / prompt: {user_prompt}",
})
return get_ai_response(user_prompt, history)
return response_message.content
def main():
chat_history = []
while True:
user_input = input("Your question: ")
if user_input == "":
break
chat_history.append({"role": "user", "content": user_input})
ai_response = get_ai_response(user_input, chat_history)
chat_history.append({"role": "assistant", "content": ai_response})
print(ai_response)
print("Do you have any other questions?")
print("Goodbye!")
if __name__ == "__main__":
main()
# Your question: what's the weather in New Delhi?
# TOOL CALL: Get Temperature
# TOOL INPUT: New Delhi
# The current temperature in New Delhi is 29.5°C.
# Do you have any other questions?
# Your question: can you convert it to Fahrenheit?
# TOOL CALL: Convert Temperature
# TOOL INPUT: Value=29.5, Unit=C
# The temperature in New Delhi, which is 29.5°C, converts to approximately 85.1°F.
# Do you have any other questions?
# Your question: what can you do for me?
# I can assist you with a variety of tasks, including:
# - Providing current weather information for locations around the world.
# - Converting temperatures between Celsius and Fahrenheit.
# - Searching the web for information on various topics.
# - Answering questions and providing explanations.
# - Assisting with general knowledge inquiries.
# If you have any specific requests or questions, feel free to ask!
# Do you have any other questions?
# Your question: current president of the USA in 2025?
# TOOL CALL: Search Web
# TOOL INPUT: current president of the USA in 2025
# The current president of the USA in 2025 is Donald J. Trump.
# Do you have any other questions?
# Your question:
# Goodbye!