Spaces:
Running
Running
Update agent_chat_stream.py
Browse files- agent_chat_stream.py +100 -99
agent_chat_stream.py
CHANGED
|
@@ -1,99 +1,100 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Agent Chat Streaming Endpoint
|
| 3 |
-
SSE-based real-time streaming for Sales & Feedback agents
|
| 4 |
-
"""
|
| 5 |
-
from typing import AsyncGenerator
|
| 6 |
-
from stream_utils import format_sse, EVENT_STATUS, EVENT_TOKEN, EVENT_DONE, EVENT_ERROR, EVENT_METADATA
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
async def agent_chat_stream(
|
| 11 |
-
request,
|
| 12 |
-
agent_service,
|
| 13 |
-
conversation_service
|
| 14 |
-
) -> AsyncGenerator[str, None]:
|
| 15 |
-
"""
|
| 16 |
-
Stream agent responses in real-time (SSE format)
|
| 17 |
-
|
| 18 |
-
Args:
|
| 19 |
-
request: ChatRequest with message, session_id, mode, user_id
|
| 20 |
-
agent_service: AgentService instance
|
| 21 |
-
conversation_service: ConversationService instance
|
| 22 |
-
|
| 23 |
-
Yields SSE events:
|
| 24 |
-
- status: Processing updates
|
| 25 |
-
- token: Text chunks
|
| 26 |
-
- metadata: Session info
|
| 27 |
-
- done: Completion signal
|
| 28 |
-
- error: Error messages
|
| 29 |
-
"""
|
| 30 |
-
try:
|
| 31 |
-
# === SESSION MANAGEMENT ===
|
| 32 |
-
session_id = request.session_id
|
| 33 |
-
if not session_id:
|
| 34 |
-
session_id = conversation_service.create_session(
|
| 35 |
-
metadata={"user_agent": "api", "created_via": "agent_stream"},
|
| 36 |
-
user_id=request.user_id
|
| 37 |
-
)
|
| 38 |
-
yield format_sse(EVENT_METADATA, {"session_id": session_id})
|
| 39 |
-
|
| 40 |
-
# Get conversation history
|
| 41 |
-
history = conversation_service.
|
| 42 |
-
|
| 43 |
-
# Convert to messages format
|
| 44 |
-
messages = []
|
| 45 |
-
for h in history:
|
| 46 |
-
messages.append({"role": h["role"], "content": h["content"]})
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent Chat Streaming Endpoint
|
| 3 |
+
SSE-based real-time streaming for Sales & Feedback agents
|
| 4 |
+
"""
|
| 5 |
+
from typing import AsyncGenerator
|
| 6 |
+
from stream_utils import format_sse, EVENT_STATUS, EVENT_TOKEN, EVENT_DONE, EVENT_ERROR, EVENT_METADATA
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
async def agent_chat_stream(
|
| 11 |
+
request,
|
| 12 |
+
agent_service,
|
| 13 |
+
conversation_service
|
| 14 |
+
) -> AsyncGenerator[str, None]:
|
| 15 |
+
"""
|
| 16 |
+
Stream agent responses in real-time (SSE format)
|
| 17 |
+
|
| 18 |
+
Args:
|
| 19 |
+
request: ChatRequest with message, session_id, mode, user_id
|
| 20 |
+
agent_service: AgentService instance
|
| 21 |
+
conversation_service: ConversationService instance
|
| 22 |
+
|
| 23 |
+
Yields SSE events:
|
| 24 |
+
- status: Processing updates
|
| 25 |
+
- token: Text chunks
|
| 26 |
+
- metadata: Session info
|
| 27 |
+
- done: Completion signal
|
| 28 |
+
- error: Error messages
|
| 29 |
+
"""
|
| 30 |
+
try:
|
| 31 |
+
# === SESSION MANAGEMENT ===
|
| 32 |
+
session_id = request.session_id
|
| 33 |
+
if not session_id:
|
| 34 |
+
session_id = conversation_service.create_session(
|
| 35 |
+
metadata={"user_agent": "api", "created_via": "agent_stream"},
|
| 36 |
+
user_id=request.user_id
|
| 37 |
+
)
|
| 38 |
+
yield format_sse(EVENT_METADATA, {"session_id": session_id})
|
| 39 |
+
|
| 40 |
+
# Get conversation history
|
| 41 |
+
history = conversation_service.get_conversation_history(session_id)
|
| 42 |
+
|
| 43 |
+
# Convert to messages format
|
| 44 |
+
messages = []
|
| 45 |
+
for h in history:
|
| 46 |
+
messages.append({"role": h["role"], "content": h["content"]})
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# Determine mode
|
| 50 |
+
mode = getattr(request, 'mode', 'sales') # Default to sales
|
| 51 |
+
|
| 52 |
+
# === STATUS UPDATE ===
|
| 53 |
+
if mode == 'feedback':
|
| 54 |
+
yield format_sse(EVENT_STATUS, "Đang kiểm tra lịch sử sự kiện của bạn...")
|
| 55 |
+
else:
|
| 56 |
+
yield format_sse(EVENT_STATUS, "Đang tư vấn...")
|
| 57 |
+
|
| 58 |
+
# === CALL AGENT ===
|
| 59 |
+
result = await agent_service.chat(
|
| 60 |
+
user_message=request.message,
|
| 61 |
+
conversation_history=messages,
|
| 62 |
+
mode=mode,
|
| 63 |
+
user_id=request.user_id
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
agent_response = result["message"]
|
| 67 |
+
|
| 68 |
+
# === STREAM RESPONSE TOKEN BY TOKEN ===
|
| 69 |
+
# Simple character-by-character streaming
|
| 70 |
+
chunk_size = 5 # Characters per chunk
|
| 71 |
+
for i in range(0, len(agent_response), chunk_size):
|
| 72 |
+
chunk = agent_response[i:i+chunk_size]
|
| 73 |
+
yield format_sse(EVENT_TOKEN, chunk)
|
| 74 |
+
# Small delay for smoother streaming
|
| 75 |
+
import asyncio
|
| 76 |
+
await asyncio.sleep(0.02)
|
| 77 |
+
|
| 78 |
+
# === SAVE HISTORY ===
|
| 79 |
+
conversation_service.add_message(
|
| 80 |
+
session_id=session_id,
|
| 81 |
+
role="user",
|
| 82 |
+
content=request.message
|
| 83 |
+
)
|
| 84 |
+
conversation_service.add_message(
|
| 85 |
+
session_id=session_id,
|
| 86 |
+
role="assistant",
|
| 87 |
+
content=agent_response
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# === DONE ===
|
| 91 |
+
yield format_sse(EVENT_DONE, {
|
| 92 |
+
"session_id": session_id,
|
| 93 |
+
"timestamp": datetime.utcnow().isoformat(),
|
| 94 |
+
"mode": mode,
|
| 95 |
+
"tool_calls": len(result.get("tool_calls", []))
|
| 96 |
+
})
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"⚠️ Agent Stream Error: {e}")
|
| 100 |
+
yield format_sse(EVENT_ERROR, str(e))
|