Skip to content

Commit

Permalink
Handle case where the Assistant tried to pass back an image
Browse files Browse the repository at this point in the history
  • Loading branch information
logankilpatrick committed Feb 27, 2024
1 parent 77712bd commit 75edcdf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
19 changes: 15 additions & 4 deletions examples/assistant-flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,21 @@ def chat():

response = client.beta.threads.messages.list(thread_id).data[0]

chat_history.append(
{"role": "assistant", "content": response.content[0].text.value}
)
return jsonify(success=True, message=response.content[0].text.value)
text_content = None

# Iterate through the content objects to find the first text content
for content in response.content:
if content.type == "text":
text_content = content.text.value
break # Exit the loop once the first text content is found

# Check if text content was found
if text_content:
chat_history.append({"role": "assistant", "content": text_content})
return jsonify(success=True, message=text_content)
else:
# Handle the case where no text content is found
return jsonify(success=False, message="No text content found")


@app.route("/reset", methods=["POST"])
Expand Down
6 changes: 4 additions & 2 deletions examples/assistant-flask/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
</div>
</div>
<script>
function TestTest(fileId) {
function deleteFiles(fileId) {
fetch("/delete_files", {
method: "POST",
headers: {
Expand Down Expand Up @@ -111,6 +111,7 @@

// If there are files, show the placeholder by removing the 'hidden' class
if (assistant_files.length > 0) {
document.getElementById("filesList").style.display = "block";
filesPlaceholder.classList.remove("hidden");

document.querySelector(".centered-text").style.display = "none";
Expand All @@ -122,6 +123,7 @@
}
} else {
document.querySelector(".centered-text").style.display = "block";
document.getElementById("filesList").style.display = "none";
filesPlaceholder.classList.add("hidden");
return;
}
Expand Down Expand Up @@ -149,7 +151,7 @@
</svg>
`;
svgIconRight.addEventListener("click", () =>
TestTest(fileIdSpan.textContent)
deleteFiles(fileIdSpan.textContent)
);

fileEntry.appendChild(svgIconLeft);
Expand Down

0 comments on commit 75edcdf

Please sign in to comment.