Spaces:
Runtime error
Runtime error
Enrique Cardoza commited on
Commit ·
a4e67d9
1
Parent(s): 8e2a9d7
fix(chat): resolve errors in Claude API integration
Browse files- Remove extraneous fields from message objects sent to Anthropic API
- Fix history processing to handle Gradio's ChatInterface format correctly
- Add debug logging to troubleshoot message structure
- Simplify message formatting to only include required 'role' and 'content' fields
- Fix 'invalid_request_error' caused by unsupported metadata fields
app.py
CHANGED
|
@@ -16,15 +16,24 @@ def respond(message, history):
|
|
| 16 |
# Format conversation history for Claude
|
| 17 |
messages = []
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Add the current message
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
|
|
|
|
|
|
| 28 |
# Call the Claude API
|
| 29 |
response = client.messages.create(
|
| 30 |
model="claude-3-7-sonnet-latest",
|
|
|
|
| 16 |
# Format conversation history for Claude
|
| 17 |
messages = []
|
| 18 |
|
| 19 |
+
# Debug: Print the structure of history
|
| 20 |
+
print(f"History: {history}")
|
| 21 |
+
|
| 22 |
+
# Add chat history - clean up message format to only include role and content
|
| 23 |
+
if history:
|
| 24 |
+
for item in history:
|
| 25 |
+
if isinstance(item, dict) and 'role' in item and 'content' in item:
|
| 26 |
+
# Only include role and content fields that Claude API expects
|
| 27 |
+
messages.append({
|
| 28 |
+
"role": item["role"],
|
| 29 |
+
"content": item["content"]
|
| 30 |
+
})
|
| 31 |
|
| 32 |
# Add the current message
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
+
print(f"Messages to send: {messages}")
|
| 36 |
+
|
| 37 |
# Call the Claude API
|
| 38 |
response = client.messages.create(
|
| 39 |
model="claude-3-7-sonnet-latest",
|