Spaces:
Runtime error
Runtime error
bug fix.
Browse files
app.py
CHANGED
|
@@ -48,19 +48,25 @@ agent = Agent(
|
|
| 48 |
markdown=True,
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# β
Chat function
|
| 52 |
def ask_agent(messages, history):
|
| 53 |
print("====== DEBUG: ask_agent START ======")
|
| 54 |
print("π₯ Incoming messages:", messages)
|
| 55 |
print("π Chat history:", history)
|
| 56 |
|
|
|
|
| 57 |
if isinstance(messages, str):
|
| 58 |
-
print("π§ͺ Detected string instead of message list. Wrapping as message.")
|
| 59 |
messages = [{"role": "user", "content": messages}]
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
try:
|
| 66 |
latest_user_message = messages[-1]["content"]
|
|
@@ -72,13 +78,10 @@ def ask_agent(messages, history):
|
|
| 72 |
try:
|
| 73 |
response = agent.run(latest_user_message)
|
| 74 |
print(f"β
Agent response:\n{response}")
|
| 75 |
-
return
|
| 76 |
except Exception as e:
|
| 77 |
print(f"β Agent failed: {e}")
|
| 78 |
-
return
|
| 79 |
-
|
| 80 |
-
print("====== DEBUG: ask_agent END ======")
|
| 81 |
-
|
| 82 |
|
| 83 |
def yes(message, history):
|
| 84 |
return "yes"
|
|
|
|
| 48 |
markdown=True,
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
def ask_agent(messages, history):
|
| 52 |
print("====== DEBUG: ask_agent START ======")
|
| 53 |
print("π₯ Incoming messages:", messages)
|
| 54 |
print("π Chat history:", history)
|
| 55 |
|
| 56 |
+
# β
Handle Hugging Face's tendency to send just a string
|
| 57 |
if isinstance(messages, str):
|
|
|
|
| 58 |
messages = [{"role": "user", "content": messages}]
|
| 59 |
+
elif isinstance(messages, list):
|
| 60 |
+
# Defensive fix: if list of strings, wrap each
|
| 61 |
+
if all(isinstance(m, str) for m in messages):
|
| 62 |
+
messages = [{"role": "user", "content": m} for m in messages]
|
| 63 |
+
# If it's a list but not valid messages, throw a descriptive error
|
| 64 |
+
elif not all(isinstance(m, dict) and "role" in m and "content" in m for m in messages):
|
| 65 |
+
print("β Malformed message list")
|
| 66 |
+
return {"role": "assistant", "content": "Sorry, I couldn't understand the message format."}
|
| 67 |
+
else:
|
| 68 |
+
print("β Completely invalid input type")
|
| 69 |
+
return {"role": "assistant", "content": "Sorry, I didn't understand that input."}
|
| 70 |
|
| 71 |
try:
|
| 72 |
latest_user_message = messages[-1]["content"]
|
|
|
|
| 78 |
try:
|
| 79 |
response = agent.run(latest_user_message)
|
| 80 |
print(f"β
Agent response:\n{response}")
|
| 81 |
+
return {"role": "assistant", "content": response.content}
|
| 82 |
except Exception as e:
|
| 83 |
print(f"β Agent failed: {e}")
|
| 84 |
+
return {"role": "assistant", "content": "Something went wrong on my end. Try again?"}
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def yes(message, history):
|
| 87 |
return "yes"
|