Update app.py
Browse files
app.py
CHANGED
|
@@ -198,15 +198,38 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 198 |
for msg in chat_history:
|
| 199 |
if msg["role"] == "system":
|
| 200 |
system_prompt = msg["content"]
|
| 201 |
-
|
| 202 |
-
messages
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Add the latest user input if not already in chat history
|
| 209 |
-
|
|
|
|
| 210 |
messages.append({
|
| 211 |
"role": "user",
|
| 212 |
"content": user_input,
|
|
@@ -223,7 +246,6 @@ def chat_with_assistant(user_input, chat_history, bot_id, workspace_id, temperat
|
|
| 223 |
"systemPrompt": system_prompt,
|
| 224 |
"messages": messages,
|
| 225 |
"temperature": temperature,
|
| 226 |
-
|
| 227 |
"debug": False,
|
| 228 |
}
|
| 229 |
}
|
|
@@ -359,10 +381,15 @@ def chat_endpoint():
|
|
| 359 |
"""
|
| 360 |
Expects JSON with:
|
| 361 |
{
|
| 362 |
-
"user_input": "string",
|
| 363 |
"chat_history": [
|
| 364 |
{"role": "system", "content": "..."},
|
| 365 |
{"role": "user", "content": "..."},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
...
|
| 367 |
],
|
| 368 |
"temperature": 0.9, // Optional, defaults to 0.9
|
|
|
|
| 198 |
for msg in chat_history:
|
| 199 |
if msg["role"] == "system":
|
| 200 |
system_prompt = msg["content"]
|
| 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 |
+
message_parts = []
|
| 205 |
+
for part in msg["content"]:
|
| 206 |
+
if part["type"] == "text":
|
| 207 |
+
message_parts.append({
|
| 208 |
+
"type": "text",
|
| 209 |
+
"text": part["text"]
|
| 210 |
+
})
|
| 211 |
+
elif part["type"] == "image":
|
| 212 |
+
message_parts.append({
|
| 213 |
+
"type": "image",
|
| 214 |
+
"image_url": {
|
| 215 |
+
"url": part["url"]
|
| 216 |
+
}
|
| 217 |
+
})
|
| 218 |
+
messages.append({
|
| 219 |
+
"role": msg["role"],
|
| 220 |
+
"content": message_parts
|
| 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 messages[-1]["content"] != user_input):
|
| 233 |
messages.append({
|
| 234 |
"role": "user",
|
| 235 |
"content": user_input,
|
|
|
|
| 246 |
"systemPrompt": system_prompt,
|
| 247 |
"messages": messages,
|
| 248 |
"temperature": temperature,
|
|
|
|
| 249 |
"debug": False,
|
| 250 |
}
|
| 251 |
}
|
|
|
|
| 381 |
"""
|
| 382 |
Expects JSON with:
|
| 383 |
{
|
| 384 |
+
"user_input": "string", // Can be null if multipart message is in chat_history
|
| 385 |
"chat_history": [
|
| 386 |
{"role": "system", "content": "..."},
|
| 387 |
{"role": "user", "content": "..."},
|
| 388 |
+
// Or for images:
|
| 389 |
+
{"role": "user", "type": "multipart", "content": [
|
| 390 |
+
{"type": "image", "url": "https://example.com/image.jpg"},
|
| 391 |
+
{"type": "text", "text": "What's in this image?"}
|
| 392 |
+
]},
|
| 393 |
...
|
| 394 |
],
|
| 395 |
"temperature": 0.9, // Optional, defaults to 0.9
|