Stream chat completions from model providers
This commit is contained in:
@@ -21,6 +21,7 @@ from app.schemas.chat import (
|
||||
)
|
||||
from app.services.chat_service import ChatService
|
||||
from app.services.chat_queue_service import chat_queue_manager, load_chat_queue_config
|
||||
from app.services.chat_stream_service import ChatStreamService
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -124,8 +125,8 @@ def _chat_stream(payload: ChatCompletionRequest, db: Session, current_user: User
|
||||
activeCount=queue_request.active_count,
|
||||
waitingCount=queue_request.waiting_count,
|
||||
)
|
||||
answer = ChatService.create_answer(db, current_user, payload.sessionId, payload.message)
|
||||
yield from _sse_chunks(answer)
|
||||
for chunk in ChatStreamService.stream_answer(db, current_user, payload.sessionId, payload.message):
|
||||
yield _sse_event("content", content=chunk)
|
||||
except HTTPException as exc:
|
||||
yield _sse_event("error", message=str(exc.detail))
|
||||
except Exception:
|
||||
@@ -138,14 +139,6 @@ def _chat_stream(payload: ChatCompletionRequest, db: Session, current_user: User
|
||||
|
||||
yield _sse_done()
|
||||
|
||||
|
||||
def _sse_chunks(answer: str) -> Iterator[str]:
|
||||
chunk_size = 12
|
||||
for index in range(0, len(answer), chunk_size):
|
||||
chunk = answer[index : index + chunk_size]
|
||||
yield _sse_event("content", content=chunk)
|
||||
|
||||
|
||||
def _sse_event(event_type: str, **payload: object) -> str:
|
||||
return f"data: {json.dumps({'type': event_type, **payload}, ensure_ascii=False)}\n\n"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user