Using Flask to interface a chat window with Assistant API

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?

1 Like

*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
2 Likes

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.