Spaces:
Running
Running
File size: 1,783 Bytes
195b940 77eb34e 29b313e 195b940 29b313e 77eb34e 195b940 77eb34e 29b313e 77eb34e 29b313e 195b940 29b313e 195b940 77eb34e 195b940 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from langchain_core.messages import HumanMessage
from chatbot.agents.graphs.chatbot_graph import workflow_chatbot
import logging
import json
from chatbot.models.llm_setup import llm_stream
# --- Cấu hình logging ---
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# # --- Định nghĩa request body ---
class ChatRequest(BaseModel):
user_id: str
thread_id: str
message: str
# --- Tạo router ---
router = APIRouter(
prefix="/chat",
tags=["Chatbot"]
)
try:
chatbot_app = workflow_chatbot()
logger.info("✅ Chatbot Graph compiled successfully!")
except Exception as e:
logger.error(f"❌ Failed to compile Chatbot Graph: {e}")
raise e
async def generate_chat_response(initial_state, config):
async for event in chatbot_app.astream_events(
initial_state,
config=config,
version="v2"
):
if event["event"] == "on_chat_model_stream":
data = event.get("data", {})
chunk = data.get("chunk")
if chunk and hasattr(chunk, "content") and chunk.content:
yield chunk.content
@router.post("/")
async def chat(request: ChatRequest):
logger.info(f"Nhận được tin nhắn chat từ user: {request.user_id}")
config = {"configurable": {"thread_id": request.thread_id}}
initial_state = {
"user_id": request.user_id,
"messages": [HumanMessage(content=request.message)]
}
return StreamingResponse(
generate_chat_response(initial_state, config),
media_type="text/plain"
)
|