Hi Guys,
I am playing with the assistant API example. I have two functions calls and I added the code interpreter because if I don’t, the assistant is not very friendly. When I add the code interpreter the final text I get as a response is doubled. I probably stream something twice, and I tried to comment some of the print statements but can’t figure it out. Would appreciate any help.
Here is the source:
import ast
from openai import OpenAI, AssistantEventHandler
from functions import get_rain_probability, get_current_temperature
client = OpenAI()
class EventHandler(AssistantEventHandler):
def on_text_created(self, text) -> None:
# print(f"\nassistant > ", end="", flush=True)
pass
def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True)
pass
def on_tool_call_created(self, tool_call):
# print(f"\nassistant > {tool_call.type}\n", flush=True)
pass
def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter':
if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs:
if output.type == "logs":
print(f"\n{output.logs}", flush=True)
# @override
def on_event(self, event):
# Retrieve events that are denoted with 'requires_action'
# since these will have our tool_calls
if event.event == 'thread.run.requires_action':
run_id = event.data.id # Retrieve the run ID from the event data
self.handle_requires_action(event.data, run_id)
def handle_requires_action(self, data, run_id):
tool_outputs = []
for tool in data.required_action.submit_tool_outputs.tool_calls:
if tool.function.name == "get_current_temperature":
data_for_function = ast.literal_eval(tool.function.arguments)["location"]
if data_for_function:
temperature_unit = ast.literal_eval(tool.function.arguments)["unit"]
current_temperature = get_current_temperature(data_for_function, temperature_unit)
tool_outputs.append({"tool_call_id": tool.id, "output": current_temperature})
elif tool.function.name == "get_rain_probability":
data_for_function = ast.literal_eval(tool.function.arguments)["location"]
if data_for_function:
rain_probability = get_rain_probability(data_for_function)
tool_outputs.append({"tool_call_id": tool.id, "output": rain_probability})
# Submit all tool_outputs at the same time
self.submit_tool_outputs(tool_outputs, run_id)
def submit_tool_outputs(self, tool_outputs, run_id):
# Use the submit_tool_outputs_stream helper
with client.beta.threads.runs.submit_tool_outputs_stream(
thread_id=self.current_run.thread_id,
run_id=self.current_run.id,
tool_outputs=tool_outputs,
event_handler=EventHandler(),
) as stream:
for text in stream.text_deltas:
print(text, end="", flush=True)
print()
assistant = client.beta.assistants.create(
instructions=“You are a weather bot. Use the provided functions to answer questions. Answer in a friendly and informative way. Be polite and helpful.”,
model=“gpt-4o”,
tools=[
{“type”: “code_interpreter”},
{
“type”: “function”,
“function”: {
“name”: “get_current_temperature”,
“description”: “Get the current temperature for a specific location”,
“parameters”: {
“type”: “object”,
“properties”: {
“location”: {
“type”: “string”,
“description”: “The city and state, e.g., San Francisco, CA”
},
“unit”: {
“type”: “string”,
“enum”: [“Celsius”, “Fahrenheit”],
“description”: “The temperature unit to use. Infer this from the user’s location.”
}
},
“required”: [“location”, “unit”],
“additionalProperties”: False
},
“strict”: True
}
},
{
“type”: “function”,
“function”: {
“name”: “get_rain_probability”,
“description”: “Get the probability of rain for a specific location”,
“parameters”: {
“type”: “object”,
“properties”: {
“location”: {
“type”: “string”,
“description”: “The city and state, e.g., San Francisco, CA”
}
},
“required”: [“location”],
“additionalProperties”: False
},
“strict”: True
}
}
]
)
query = “”
while query != ‘q’:
query = input("\n\nEnter your weather query: (q to quit): ")
thread = client.beta.threads.create()
message = client.beta.threads.messages.create(
thread_id=thread.id,
role=“user”,
content=query
)
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
event_handler=EventHandler()
) as stream:
stream.until_done()
And here is an example of an output:
Enter your weather query: (q to quit): Hi
Hello! How can I assist you today?
Enter your weather query: (q to quit): What is the Weather in London?
InIn London London right right now now, the the temperature temperature is is ** 2121…11°C°C,**, and and there there is is an an ** 8686%% chance chance of of rain rain**… It It’s’s quite quite warm warm but but seems seems like like you you might might want want to to carry carry an an umbrella umbrella just just in in case case… Stay Stay prepared prepared!!