I am at a loss. Building 2 different versions of a bot using assistant API, I cannot for the life of me get it to interact with chat container on an HTML page while using Flask to to handle the requests. Either the logic falls apart, or it has errors parsing JSONs. Or where with threads. If I upload both revisions of my current project, would someone be able to assist?
This shouldnāt be difficult to setup. I imagine your Flask API is adding the webchat-submitted text to a thread and then you run the assistant with that thread. What problems do you have with this?
*Iāve botched the logic and the retrieval when it comes to displaying the response. Iāve added a few print statements in the logic, when following the thread I am in fact getting the response properly, and it is building off each response (as far as I can tell). *
However the chat window itself shows something like:
```
User: Hi
Assistant: undefined
(in VSCode console) ā¦ content=[MessageContentText(text=Text(annotations=[], value=āHi! How may I assist you todayā), type=ātextā)] ā¦
User: Iām having trouble with my car window
Assistant: undefined
(in VSCode console) ā¦ content=[MessageContentText(text=Text(annotations=[], value='It could be your window regulator, etc, etc), type=ātextā)] ā¦
```
Iāve put in demo responses, however from what I can gather, is that on the HTML side it either isnāt waiting long enough to get a response, or itās an issue with grabbing the right element from the response. I mainly do UX design or light automation so all this is pretty new to me.
^^^ ignore ^^^
Iāve gone ahead and made some fixes to an earlier version that checks the run to fix the timing issue. The logic for looking for the Assistant response is:
fig 1
for msg in messages.data:
if msg.role == 'assistant':
for content in msg.content:
if content.type == 'text':
assistant_response = content.text.value
print('\nassistant output: ' + str(assistant_response) + '\n')
return jsonify({'response': assistant_response})
So it only ever outputs the first response, even though when looking at the readout from listing the Thread:
fig 2
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print('\nresponse message from threads: \n:' + str(messages))
Shows an updated response in the MessageContentText field.
So Iām assuming the logic is broken in fig 1 and thatās why it keeps returning the initial assistant response. Thereās some other issues but this is the big boy one that Iām working on just to get it functional.
To anyone else running into this issue, I fixed it by reading the API doc a little better. Under list messages you can order by the ācreate_atā timestamp to properly order the thread when grabbing the assistant response.
messages = client.beta.threads.messages.list(
thread_id=thread.id,
# Orders it by ascending order, so the latest response is at the top
order='asc'
)
Because this is how I was retrieving the assistant response:
for msg in messages.data:
if msg.role == 'assistant':
for content in msg.content:
if content.type == 'text':
assistant_response = content.text.value
Edit
āāā-
Huge brain fart, but Iām pretty sure I could have just done this
# I havenāt tested this but Iām pretty sure just doing this works
assistant_response = messages.data[0]
return assistant_response.content[0].text.value
I have a similar issue where I can only get it to display the first response in the browser although I can see in the terminal that it made 5 more responses after that. Were you able to solve this issue?
Try changing the order or change how you grab the index of responses. Personally I just kept the āorder=ascā for my messages variable.