from openai import OpenAI
import json
client = OpenAI(
base_url="https://app.empower.dev/api/v1",
api_key="YOUR_API_KEY"
)
# Function definitions
def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
if "tokyo" in location.lower():
return json.dumps({"location": "Tokyo", "temperature": "10", "unit": unit})
elif "san francisco" in location.lower():
return json.dumps({"location": "San Francisco", "temperature": "72", "unit": unit})
elif "paris" in location.lower():
return json.dumps({"location": "Paris", "temperature": "22", "unit": unit})
else:
return json.dumps({"location": location, "temperature": "22"})
def get_capital(country):
"""Get the capital city of a given country"""
capitals = {
"japan": "Tokyo",
"united states": "Washington D.C.",
"france": "Paris",
"united kingdom": "London",
"germany": "Berlin",
"india": "New Delhi"
}
country_lower = country.lower()
if country_lower in capitals:
capital = capitals[country_lower]
return json.dumps({"country": country, "capital": capital})
else:
return json.dumps({"country": country, "capital": "Unknown"})
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
},
},
{
"type": "function",
"function": {
"name": "get_capital",
"description": "Get the capital city of a given country",
"parameters": {
"type": "object",
"properties": {
"country": {
"type": "string",
"description": "Name of the country",
}
},
"required": ["country"],
},
},
}
]
# End function definitions
# #1 Ask the model for the function to call
messages = [
{"role": "user", "content": "What's the current weather in San Francisco, Paris and Beijing? Also can you help me check the capital of Germany and India?"},
]
# #2 Model responds with the function to call the the arguments to use
# In this case it should respond with 5 tool_calls
response = client.chat.completions.create(
model="empower-functions",
messages=messages,
temperature=0.0,
tools=tools,
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
messages.append(response_message)
# #3 Execute all the functions based on the model response
for tool_call in tool_calls:
function = tool_call.function
function_name = function.name
function_arguments = json.loads(function.arguments)
function_response = globals()[function_name](**function_arguments)
print(f"function name: {function_name}, arguments: {function_arguments}")
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(function_response)
})
# #4 Invoke the model again including the first model response and the response function executions
response = client.chat.completions.create(
model="empower-functions",
messages=messages,
temperature=0.0,
tools=tools,
)
print(response.choices[0].message.content)