Hi I have started learning about Chat GPT APIs recently and I am very new in this, Currently I am trying 1 example where the issue is I am not getting the response from assistant rather inspite of sending the response ,assistant is asking the question again and again. I am attaching the code below ,can kindly someone let me know where I am going wrong.
If you see the o/p you can see that in message stack no response is there oly the same question is being asked, I faced similar issue with another function calling scenario as well , any help is appreciable.TIA.
Attached the screenshots
herewidth.
def president_home_state(president,state_options):
‘’’
INPUTS:
president str = The string name of a US President
state_options list[str] = A list of potential states that the President was born in, with one of them being correct!
OUTPUTS:
response str = The response the user chose as the correct birthplace state.
'''
print("Hello! Let's test your knowledge of the home states of US Presidents!")
print(f"In what state was this president born: {president}\n")
for num,option in enumerate(state_options):
print('\n')
print(f"Definition #{num} is: {option}")
print('\n')
num_choice = input("What is your choice? (Return the single number option)")
return state_options[int(num_choice)]
function_json = {'type':'function',
'function':{
'name': 'president_home_state',
'parameters':{
"type":"object",
"properties":{
"president": {'type':'string','description':"The name of a random US President"},
"state_options":{'type':'array',
'items':{'type':'string'},
'description':"A Python list of strings of US States, where one of the states is the birthplace state of the US President."}
},
'required' : ["president","state_options"]
}
}
}
from openai import OpenAI
import time
client = OpenAI(
api_key=“key”, # Replace with your actual OpenAI API key
)
assistant = client.beta.assistants.create(
name=‘US Presidents Quiz Bot’,
instructions=“You help create a quiz where you give a US President and a list of birthplace states, where only one is the correct birthplace state of the president. Later you check if answers returned are correct.”,
model=“gpt-3.5-turbo”,
tools=[function_json]
)
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id)
import time
def wait_on_run(run, thread):
while run.status == “queued” or run.status == “in_progress”:
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(1)
return run
run = wait_on_run(run,thread)
run.status
printing run status showing requires_action
‘requires_action’
import json
Extract single tool call
tool_call = run.required_action.submit_tool_outputs.tool_calls[0]
name = tool_call.function.name
arguments = json.loads(tool_call.function.arguments)
print(“Function Name:”, name)
print(“Function Arguments:”)
arguments
o/p:
Function Name: president_home_state Function Arguments:
{‘president’: ‘Barack Obama’, ‘state_options’: [‘Hawaii’, ‘California’, ‘Texas’, ‘Florida’]}
response = president_home_state(arguments[‘president’],arguments[‘state_options’])
on calling the function i was asked with prompt ,
Hello! Let’s test your knowledge of the home states of US Presidents! In what state was this president born: Barack Obama Definition #0 is: Hawaii Definition #1 is: California Definition #2 is: Texas Definition #3 is: Florida
What is your choice? (Return the single number option) 1
[13]:
response
[13]:
‘California’
run = client.beta.threads.runs.submit_tool_outputs(
thread_id=thread.id,
run_id=run.id,
tool_outputs=[
{
“tool_call_id”: tool_call.id,
“output”: json.dumps(response),
}
],
)
run = wait_on_run(run, thread)
#printing run status will show completed only
run.status
messages = client.beta.threads.messages.list(thread.id)
messages
for thread_message in messages:
print(thread_message.content[0].text.value)
print(‘\n’)
Now I wil get the o/p again as
**Which state was Barack Obama born in? **
**A) Hawaii **
**B) California **
**C) Texas **
D) Florida
