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?