Update app.py
Browse files
app.py
CHANGED
|
@@ -201,39 +201,39 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 201 |
elif msg["role"] in ["user", "assistant"]:
|
| 202 |
# Handle multipart messages (for images)
|
| 203 |
if "type" in msg and msg["type"] == "multipart" and "content" in msg:
|
| 204 |
-
|
| 205 |
for part in msg["content"]:
|
| 206 |
if part["type"] == "text":
|
| 207 |
-
|
| 208 |
"type": "text",
|
| 209 |
"text": part["text"]
|
| 210 |
})
|
| 211 |
elif part["type"] == "image":
|
| 212 |
-
|
| 213 |
-
"type": "
|
| 214 |
"image_url": {
|
| 215 |
"url": part["url"]
|
| 216 |
}
|
| 217 |
})
|
| 218 |
messages.append({
|
| 219 |
"role": msg["role"],
|
| 220 |
-
"content":
|
| 221 |
})
|
| 222 |
# Handle regular text messages
|
| 223 |
else:
|
|
|
|
| 224 |
messages.append({
|
| 225 |
"role": msg["role"],
|
| 226 |
-
"content": msg["content"]
|
| 227 |
-
"type": "text"
|
| 228 |
})
|
| 229 |
|
| 230 |
# Add the latest user input if not already in chat history
|
| 231 |
# Only add if it's a simple text input (not multipart)
|
| 232 |
-
if user_input and isinstance(user_input, str) and (not messages or messages[-1]["role"] != "user" or
|
|
|
|
| 233 |
messages.append({
|
| 234 |
"role": "user",
|
| 235 |
-
"content": user_input
|
| 236 |
-
"type": "text"
|
| 237 |
})
|
| 238 |
|
| 239 |
# Prepare the payload for the API
|
|
@@ -258,6 +258,10 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 258 |
max_retries = 3
|
| 259 |
timeout = 120 # Increased timeout for long messages
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
# Attempt to send the request
|
| 262 |
for attempt in range(max_retries):
|
| 263 |
try:
|
|
|
|
| 201 |
elif msg["role"] in ["user", "assistant"]:
|
| 202 |
# Handle multipart messages (for images)
|
| 203 |
if "type" in msg and msg["type"] == "multipart" and "content" in msg:
|
| 204 |
+
content_array = []
|
| 205 |
for part in msg["content"]:
|
| 206 |
if part["type"] == "text":
|
| 207 |
+
content_array.append({
|
| 208 |
"type": "text",
|
| 209 |
"text": part["text"]
|
| 210 |
})
|
| 211 |
elif part["type"] == "image":
|
| 212 |
+
content_array.append({
|
| 213 |
+
"type": "image_url",
|
| 214 |
"image_url": {
|
| 215 |
"url": part["url"]
|
| 216 |
}
|
| 217 |
})
|
| 218 |
messages.append({
|
| 219 |
"role": msg["role"],
|
| 220 |
+
"content": content_array
|
| 221 |
})
|
| 222 |
# Handle regular text messages
|
| 223 |
else:
|
| 224 |
+
# For text messages, content must be a string without a type field
|
| 225 |
messages.append({
|
| 226 |
"role": msg["role"],
|
| 227 |
+
"content": msg["content"]
|
|
|
|
| 228 |
})
|
| 229 |
|
| 230 |
# Add the latest user input if not already in chat history
|
| 231 |
# Only add if it's a simple text input (not multipart)
|
| 232 |
+
if user_input and isinstance(user_input, str) and (not messages or messages[-1]["role"] != "user" or
|
| 233 |
+
(isinstance(messages[-1]["content"], str) and messages[-1]["content"] != user_input)):
|
| 234 |
messages.append({
|
| 235 |
"role": "user",
|
| 236 |
+
"content": user_input
|
|
|
|
| 237 |
})
|
| 238 |
|
| 239 |
# Prepare the payload for the API
|
|
|
|
| 258 |
max_retries = 3
|
| 259 |
timeout = 120 # Increased timeout for long messages
|
| 260 |
|
| 261 |
+
# For debugging
|
| 262 |
+
print("Payload being sent to Botpress:")
|
| 263 |
+
print(json.dumps(payload, indent=2))
|
| 264 |
+
|
| 265 |
# Attempt to send the request
|
| 266 |
for attempt in range(max_retries):
|
| 267 |
try:
|