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 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| import json import asyncio import logging import traceback from http import HTTPStatus
import uvicorn from fastapi import FastAPI, HTTPException from fastapi.responses import StreamingResponse
from llm import LLMClient from memory import RedisClient from session import SessionManager, SessionStatus, SessionCacheTime from model import ChatCompletionRequest, ChatCancelRequest, ChatCancelResponse, ChatStatusResponse
logger = logging.getLogger(__name__)
BASE_URL = 'xxx' API_KEY = 'xxx' HISTORY_MSG_LIMIT = 10
app = FastAPI() session_manager = SessionManager()
def _gen_session_response(session_id: str, curr_session: dict, status: SessionStatus, message: str | None = None) -> dict: curr_session['status'] = status if status != SessionStatus.RUNNING: message = None
session_msg = {"session_id": session_id, "status": status, "message": message}
return session_msg
@app.post("/api/v1/chat/completion") async def chat_completion_stream(chat: ChatCompletionRequest): session_id = chat.session_id if not session_id or session_id not in session_manager.sessions: session_id = session_manager.create_session(chat.session_id)
curr_session = session_manager.get_session(session_id)
redis_client = RedisClient() key_session_messages = f"key_session_messages_{session_id}" messages = await redis_client.get(key_session_messages) if messages: try: messages = json.loads(messages) except json.JSONDecodeError: messages = curr_session.get('messages', []) if messages is None: messages = []
messages.append({"role": "user", "content": chat.prompt}) messages = messages[-HISTORY_MSG_LIMIT:] curr_session['messages'] = messages
llm_client = LLMClient(base_url=BASE_URL, api_key=API_KEY) chat_args = { "model": chat.model, "messages": messages }
async def generate(): try: full_content = ''
session_msg = _gen_session_response(session_id, curr_session, SessionStatus.RUNNING)
async for resp in await llm_client.chat_stream(**chat_args): if curr_session['cancel_event'].is_set(): session_msg = _gen_session_response(session_id, curr_session, SessionStatus.CANCELLED) yield f"data: {session_msg}\n\n" break
if resp.status_code == HTTPStatus.OK and resp.output.choices: choices = resp.output.choices if not choices: continue
curr_content = choices[0].message.content full_content += curr_content session_msg = _gen_session_response(session_id, curr_session, SessionStatus.RUNNING, curr_content) yield f"data: {session_msg}\n\n"
if not curr_session['cancel_event'].is_set(): curr_session['messages'].append({"role": "assistant", "content": full_content}) await redis_client.set(key_session_messages, json.dumps(curr_session['messages'], ensure_ascii=False), int(SessionCacheTime.MSG_CACHE_TIME))
session_msg = _gen_session_response(session_id, curr_session, SessionStatus.COMPLETED) yield f"data: {session_msg}\n\n" except asyncio.CancelledError: session_msg = _gen_session_response(session_id, curr_session, SessionStatus.CANCELLED) yield f"data: {session_msg}\n\n" except Exception as e: logger.error(traceback.format_exc()) session_msg = _gen_session_response(session_id, curr_session, SessionStatus.ERROR) yield f"data: {session_msg}\n\n"
return StreamingResponse( generate(), media_type="text/event-stream", headers={ "Cache-Control": "no-cache", "Connection": "keep-alive", } )
@app.post("/api/v1/chat/cancel", response_model=ChatCancelResponse) async def cancel_chat(chat: ChatCancelRequest): """取消会话 """ session_id = chat.session_id curr_session = session_manager.sessions.get(session_id) if not curr_session: logger.info(f"会话 {session_id} 不存在") raise HTTPException( status_code=404, detail=f"会话 {session_id} 不存在" )
if curr_session['status'] != SessionStatus.RUNNING: logger.info(f"会话 {session_id} 当前状态是: {curr_session['status']}, 不支持取消") raise HTTPException( status_code=400, detail=f"会话 {session_id} 当前状态是: {curr_session['status']}, 不支持取消" )
logger.info(f"会话 {session_id} 正在取消") await session_manager.cancel_session(session_id)
return ChatCancelResponse( session_id=session_id, status=SessionStatus.CANCELLED, message=f"会话 {session_id} 已经取消" )
@app.get("/api/v1/chat/status/{session_id}", response_model=ChatStatusResponse) async def chat_status(session_id: str): """查询会话状态 """ curr_session = session_manager.sessions.get(session_id) if not curr_session: logger.info(f"会话 {session_id} 不存在") raise HTTPException( status_code=404, detail=f"会话 {session_id} 不存在" )
return ChatStatusResponse( session_id=session_id, status=curr_session['status'] )
if __name__ == "__main__": uvicorn.run( app, host="0.0.0.0", port=8011, log_level="info" )
|