I want to forward user question to openai assistant api to my restapi endpoint. As far as I understand, I need to use so called Functions of openai assistant. But where should I specify my restapi endpoint URL?
This is an approach I have done to fetch data from an API using assistants.
I hope it helps
1 Like
I guess my brain will soon blow up because of this confusing docs and articles. They all tell the same except for simple fact: where the f.ck should I put the exact function definitions to make my assistant aware about it?
it doesnt accept them right here
Refer to the docs:
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant.id,
)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
else:
print(run.status)
# Define the list to store tool outputs
tool_outputs = []
# Loop through each tool in the required action section
for tool in run.required_action.submit_tool_outputs.tool_calls:
if tool.function.name == "get_current_temperature":
# Call your API/restapi endpoint here using the "tool.function.arguments" and put result to tool_outputs
tool_outputs.append({
"tool_call_id": tool.id,
"output": "57"
})
elif tool.function.name == "get_rain_probability":
# Call your API/restapi endpoint here using the "tool.function.arguments" and put result to tool_outputs
tool_outputs.append({
"tool_call_id": tool.id,
"output": "0.06"
})
Then submit the tool outputs
# Submit all tool outputs at once after collecting them in a list
if tool_outputs:
try:
run = client.beta.threads.runs.submit_tool_outputs_and_poll(
thread_id=thread.id,
run_id=run.id,
tool_outputs=tool_outputs
)
print("Tool outputs submitted successfully.")
except Exception as e:
print("Failed to submit tool outputs:", e)
else:
print("No tool outputs to submit.")
1 Like
Thanks to you finally i have got it. So openai requires our code to help if right during its run. That was not obvious for me.