Hello! I need help using OpenAI’s assistants. I have an existing assistant with tools like function calls, code interpreter, and retrieval. I have a function called get_current_weather in the OpenAI assistant, and the code looks like this:
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and country code, e.g. Defaults to some_location, US if not specified."
},
"unit": {
"type": "string",
"enum": [
"celsius",
"fahrenheit"
],
"default": "fahrenheit"
}
},
"required": []
}
}
I was wondering how I can use my OpenAI assistant’s function from the assistants API into my Python code:
def get_current_weather(city="some_location", country_code="us"):
base_url = "the url"
response = requests.get(complete_url)
weather_data = response.json()
if weather_data['cod'] == 200:
main = weather_data['main']
temperature = main['temp']
humidity = main['humidity']
clouds = weather_data['clouds']['all']
weather_description = weather_data['weather'][0]['description']
chance_of_rain = weather_data.get('rain', {}).get('1h', 0)
weather_report = [
f"Temperature: {temperature}°F",
f"Humidity: {humidity}%",
f"Cloudiness: {clouds}%",
f"Description: {weather_description}",
f"Chance of Rain (next hour): {chance_of_rain}%"
]
return "\n".join([f"- {item}" for item in weather_report])
else:
return "Weather data not found."
I can use it only when I have to do this and not use my assistant:
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
tools=tools,
tool_choice="auto",
and I also have to put the functions in the Python code instead of putting the functions in an existing OpenAI Assistant