Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -100,18 +100,39 @@ async def ask(request: Request):
|
|
100 |
answer = response.choices[0].message.content.strip()
|
101 |
print("β
GPT Answer:", answer)
|
102 |
|
103 |
-
# β
Extract and send token usage to Slack
|
104 |
usage = response.usage
|
105 |
if usage:
|
106 |
prompt_tokens = usage.prompt_tokens
|
107 |
completion_tokens = usage.completion_tokens
|
108 |
total_tokens = usage.total_tokens
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
slack_msg = (
|
111 |
-
f"π
|
112 |
-
f"
|
113 |
-
f"
|
114 |
-
f"
|
|
|
|
|
|
|
|
|
115 |
)
|
116 |
await send_to_slack(slack_msg)
|
117 |
else:
|
|
|
100 |
answer = response.choices[0].message.content.strip()
|
101 |
print("β
GPT Answer:", answer)
|
102 |
|
103 |
+
# β
Extract and send token usage to Slack with conversation history
|
104 |
usage = response.usage
|
105 |
if usage:
|
106 |
prompt_tokens = usage.prompt_tokens
|
107 |
completion_tokens = usage.completion_tokens
|
108 |
total_tokens = usage.total_tokens
|
109 |
+
|
110 |
+
# Get conversation history (last 3 messages)
|
111 |
+
messages = payload.get('conversation', {}).get('messages', [])[-6:] # Get last 3 exchanges (6 messages)
|
112 |
+
conversation_history = []
|
113 |
+
|
114 |
+
for msg in messages:
|
115 |
+
role = "π€ User" if msg.get('message_type') == 'incoming' else "π€ AI"
|
116 |
+
content = msg.get('content', '').strip()
|
117 |
+
if content:
|
118 |
+
conversation_history.append(f"{role}: {content}")
|
119 |
+
|
120 |
+
# Add current exchange
|
121 |
+
conversation_history.append(f"π€ User: {message_content}")
|
122 |
+
conversation_history.append(f"π€ AI: {answer}")
|
123 |
+
|
124 |
+
# Format conversation history with code blocks for better readability
|
125 |
+
formatted_history = "\n".join(conversation_history)
|
126 |
+
|
127 |
slack_msg = (
|
128 |
+
f"π *Conversation Update* - `{conversation_id}`\n\n"
|
129 |
+
f"*π Conversation History:*\n```\n{formatted_history}\n```\n\n"
|
130 |
+
f"*π Token Usage:*\n```\n"
|
131 |
+
f"Prompt: {prompt_tokens} tokens\n"
|
132 |
+
f"Completion: {completion_tokens} tokens\n"
|
133 |
+
f"Total: {total_tokens} tokens\n"
|
134 |
+
f"Model: {response.model or 'N/A'}\n"
|
135 |
+
f"```"
|
136 |
)
|
137 |
await send_to_slack(slack_msg)
|
138 |
else:
|