{"id":85307,"date":"2026-07-27T11:17:01","date_gmt":"2026-07-27T03:17:01","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/85307.html"},"modified":"2026-07-27T11:17:01","modified_gmt":"2026-07-27T03:17:01","slug":"%e4%bb%8e%e9%9b%b6%e6%9e%84%e5%bb%ba-ai-%e5%af%b9%e8%af%9d%e7%9b%91%e6%8e%a7%e4%bb%aa%e8%a1%a8%e7%9b%98%ef%bc%9alangfuse-langchain-deepseek-fastapi-websocket-%e5%ae%9e%e6%97%b6%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/85307.html","title":{"rendered":"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5"},"content":{"rendered":"<p>\u5f53\u4f60\u7684 AI \u5e94\u7528\u5f00\u59cb\u6709\u771f\u5b9e\u7528\u6237\u65f6&#xff0c;&#034;\u6a21\u578b\u56de\u590d\u4e86\u4ec0\u4e48&#034;\u5df2\u7ecf\u4e0d\u591f\u4e86\u2014\u2014\u4f60\u9700\u8981\u77e5\u9053\u8c01\u5728\u7528\u3001\u7528\u4e86\u591a\u5c11 Token\u3001\u82b1\u4e86\u591a\u5c11\u94b1\u3001\u54cd\u5e94\u6709\u591a\u5feb\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684 Demo \u9879\u76ee&#xff0c;\u5c55\u793a\u5982\u4f55\u7528 FastAPI &#043; DeepSeek &#043; WebSocket \u6784\u5efa\u4e00\u4e2a\u5e26\u5b9e\u65f6\u76d1\u63a7\u4eea\u8868\u76d8\u7684 AI \u5bf9\u8bdd\u670d\u52a1\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u8fd9\u4e2a Demo \u505a\u4e86\u4ec0\u4e48<\/h3>\n<p>\u9879\u76ee\u63d0\u4f9b\u4e24\u4e2a\u670d\u52a1&#xff0c;\u8dd1\u5728\u540c\u4e00\u4e2a FastAPI \u5e94\u7528\u91cc&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u670d\u52a1<\/p>\n<\/td>\n<td>\n<p>\u9762\u5411<\/p>\n<\/td>\n<td>\n<p>\u529f\u80fd<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>AI \u5bf9\u8bdd\u670d\u52a1<\/p>\n<\/td>\n<td>\n<p>\u7ec8\u7aef\u7528\u6237<\/p>\n<\/td>\n<td>\n<p>\u5bf9\u63a5 DeepSeek \u5927\u6a21\u578b&#xff0c;\u652f\u6301\u591a\u8f6e\u5bf9\u8bdd\u3001\u4f1a\u8bdd\u7ba1\u7406<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u5b9e\u65f6\u4eea\u8868\u76d8<\/p>\n<\/td>\n<td>\n<p>\u8fd0\u8425\/\u5f00\u53d1<\/p>\n<\/td>\n<td>\n<p>\u5b9e\u65f6\u67e5\u770b\u7528\u6237\u6d3b\u8dc3\u5ea6\u3001Token \u6d88\u8017\u3001\u8d39\u7528\u7edf\u8ba1\u3001\u54cd\u5e94\u5ef6\u8fdf<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e24\u4e2a\u670d\u52a1\u7684\u6570\u636e\u6d41\u662f\u4e32\u8054\u7684&#xff1a;\u7528\u6237\u6bcf\u53d1\u4e00\u6761\u6d88\u606f&#xff0c;\u4eea\u8868\u76d8\u65e0\u9700\u5237\u65b0\u5c31\u80fd\u770b\u5230\u65b0\u7684\u6d3b\u52a8\u8bb0\u5f55\u548c\u7edf\u8ba1\u53d8\u5316\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"521\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727031700-6a66cdac59de8.png\" width=\"646\" \/><\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u5feb\u901f\u542f\u52a8<\/h3>\n<h4>2.1 \u73af\u5883\u8981\u6c42<\/h4>\n<ul>\n<li>Python 3.11&#043;<\/li>\n<\/ul>\n<ul>\n<li>DeepSeek API Key&#xff08;\u5728 platform.deepseek.com \u83b7\u53d6&#xff09;<\/li>\n<\/ul>\n<h4>2.2 \u5b89\u88c5\u6b65\u9aa4<\/h4>\n<p># 1. \u8fdb\u5165\u9879\u76ee\u76ee\u5f55<br \/>\ncd D:\\\\Dashboard<\/p>\n<p># 2. \u521b\u5efa\u865a\u62df\u73af\u5883<br \/>\npython -m venv venv<br \/>\nvenv\\\\Scripts\\\\activate    # Windows<br \/>\n# source venv\/bin\/activate  # macOS\/Linux<\/p>\n<p># 3. \u5b89\u88c5\u4f9d\u8d56<br \/>\npip install -r requirements.txt<\/p>\n<p># 4. \u914d\u7f6e\u73af\u5883\u53d8\u91cf<br \/>\ncopy .env.example .env <\/p>\n<p>\u7f16\u8f91 .env \u6587\u4ef6&#xff0c;\u586b\u5165\u4f60\u7684 DeepSeek API Key&#xff1a;<\/p>\n<p>DEEPSEEK_API_KEY&#061;sk-your-actual-key-here<br \/>\nDEEPSEEK_MODEL&#061;deepseek-chat <\/p>\n<h4>2.3 \u542f\u52a8\u670d\u52a1<\/h4>\n<p>python run.py <\/p>\n<p>\u542f\u52a8\u540e\u8bbf\u95ee\u4ee5\u4e0b\u5730\u5740&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u5730\u5740<\/p>\n<\/td>\n<td>\n<p>\u8bf4\u660e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>http:\/\/localhost:8000\/chat<\/p>\n<\/td>\n<td>\n<p>\u7528\u6237\u5bf9\u8bdd\u754c\u9762<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>http:\/\/localhost:8000\/dashboard<\/p>\n<\/td>\n<td>\n<p>\u5b9e\u65f6\u76d1\u63a7\u4eea\u8868\u76d8<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>High Performance Web Crawler API &#8211; Swagger UI<\/p>\n<\/td>\n<td>\n<p>API \u4ea4\u4e92\u6587\u6863&#xff08;Swagger UI&#xff09;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>2.4 \u4f7f\u7528\u6d41\u7a0b<\/h4>\n<li>\u6253\u5f00 \/chat \u9875\u9762&#xff0c;\u8f93\u5165\u7528\u6237\u540d&#xff08;\u5982&#034;\u5f20\u4e09&#034;&#xff09;<\/li>\n<li>\u8f93\u5165\u6d88\u606f&#xff0c;\u5982&#034;\u5e2e\u6211\u5199\u4e00\u9996\u5173\u4e8e\u79cb\u5929\u7684\u8bd7&#034;<\/li>\n<li>DeepSeek \u8fd4\u56de\u56de\u590d&#xff0c;\u754c\u9762\u5e95\u90e8\u663e\u793a\u672c\u6b21\u8c03\u7528\u7684 Token \u6570\u548c\u8d39\u7528<\/li>\n<li>\u6253\u5f00 \/dashboard \u9875\u9762&#xff08;\u53ef\u4ee5\u5f00\u5728\u65b0\u6807\u7b7e\u9875&#xff09;<\/li>\n<li>\u5728\u5bf9\u8bdd\u9875\u53d1\u4e00\u6761\u65b0\u6d88\u606f \u2192 \u4eea\u8868\u76d8\u7684&#034;\u5b9e\u65f6\u6d3b\u52a8&#034;\u533a\u57df\u7acb\u5373\u51fa\u73b0\u8fd9\u6761\u8bb0\u5f55<\/li>\n<li>\u7edf\u8ba1\u5361\u7247\u3001Token \u8d8b\u52bf\u56fe\u3001\u8d39\u7528\u8d8b\u52bf\u56fe\u4e5f\u4f1a\u540c\u6b65\u5237\u65b0<\/li>\n<hr \/>\n<h3>\u4e09\u3001\u9879\u76ee\u7ed3\u6784<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"652\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727031700-6a66cdaca4159.png\" width=\"604\" \/><\/p>\n<p>\u6574\u4f53\u91c7\u7528\u5206\u5c42\u67b6\u6784&#xff1a;\u8def\u7531\u5c42&#xff08;routers&#xff09;\u8d1f\u8d23 HTTP \u5165\u53e3&#xff0c;\u670d\u52a1\u5c42&#xff08;services&#xff09;\u5c01\u88c5\u4e1a\u52a1\u903b\u8f91&#xff0c;\u6570\u636e\u5c42&#xff08;models &#043; database&#xff09;\u7ba1\u7406\u6301\u4e45\u5316&#xff0c;WebSocket \u5c42&#xff08;ws&#xff09;\u5904\u7406\u5b9e\u65f6\u901a\u4fe1\u3002\u5404\u5c42\u4e4b\u95f4\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u89e3\u8026\u3002<\/p>\n<hr \/>\n<h3>\u56db\u3001\u5173\u952e\u6280\u672f\u73af\u8282\u8be6\u89e3<\/h3>\n<h4>4.1 DeepSeek API \u96c6\u6210&#xff1a;\u590d\u7528 OpenAI SDK<\/h4>\n<p>DeepSeek \u7684 API \u5b8c\u5168\u517c\u5bb9 OpenAI \u63a5\u53e3\u683c\u5f0f&#xff0c;\u8fd9\u610f\u5473\u7740\u4e0d\u9700\u8981\u5b89\u88c5\u989d\u5916\u7684 SDK\u2014\u2014\u76f4\u63a5\u7528 openai \u5b98\u65b9\u5e93&#xff0c;\u628a base_url \u6307\u5411 DeepSeek \u5373\u53ef\u3002<\/p>\n<p># app\/services\/deepseek.py<\/p>\n<p>from openai import AsyncOpenAI<\/p>\n<p>class DeepSeekService:<br \/>\n    def __init__(self):<br \/>\n        self.client &#061; AsyncOpenAI(<br \/>\n            api_key&#061;settings.deepseek_api_key,<br \/>\n            base_url&#061;&#034;https:\/\/api.deepseek.com&#034;,  # \u5173\u952e&#xff1a;\u6307\u5411 DeepSeek<br \/>\n        )<br \/>\n        self.model &#061; &#034;deepseek-chat&#034;<\/p>\n<p>    async def chat(self, messages: list[dict]) -&gt; DeepSeekResult:<br \/>\n        start &#061; time.monotonic()<\/p>\n<p>        response &#061; await self.client.chat.completions.create(<br \/>\n            model&#061;self.model,<br \/>\n            messages&#061;messages,<br \/>\n        )<\/p>\n<p>        latency_ms &#061; int((time.monotonic() &#8211; start) * 1000)<\/p>\n<p>        return DeepSeekResult(<br \/>\n            content&#061;response.choices[0].message.content,<br \/>\n            model&#061;self.model,<br \/>\n            tokens_input&#061;response.usage.prompt_tokens,      # \u8f93\u5165 Token<br \/>\n            tokens_output&#061;response.usage.completion_tokens,  # \u8f93\u51fa Token<br \/>\n            latency_ms&#061;latency_ms,<br \/>\n        ) <\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f7f\u7528 AsyncOpenAI \u800c\u975e\u540c\u6b65\u5ba2\u6237\u7aef&#xff0c;\u786e\u4fdd API \u8c03\u7528\u4e0d\u963b\u585e\u4e8b\u4ef6\u5faa\u73af<\/li>\n<\/ul>\n<ul>\n<li>time.monotonic() \u800c\u975e time.time() \u6d4b\u91cf\u5ef6\u8fdf\u2014\u2014\u524d\u8005\u4e0d\u53d7\u7cfb\u7edf\u65f6\u949f\u8c03\u6574\u5f71\u54cd<\/li>\n<\/ul>\n<ul>\n<li>\u4ece response.usage \u76f4\u63a5\u63d0\u53d6 Token \u7528\u91cf&#xff0c;\u65e0\u9700\u81ea\u5df1\u5b9e\u73b0\u5206\u8bcd\u8ba1\u6570<\/li>\n<\/ul>\n<ul>\n<li>\u8fd4\u56de\u503c\u7528 dataclass \u5c01\u88c5&#xff0c;\u7c7b\u578b\u6e05\u6670&#xff0c;\u4fbf\u4e8e\u4e0b\u6e38\u6d88\u8d39<\/li>\n<\/ul>\n<h4>4.2 \u5f02\u6b65\u6570\u636e\u5e93&#xff1a;SQLAlchemy 2.0 &#043; aiosqlite<\/h4>\n<p>\u6574\u4e2a\u6570\u636e\u5c42\u57fa\u4e8e\u5f02\u6b65\u5b9e\u73b0&#xff0c;\u907f\u514d I\/O \u963b\u585e FastAPI \u7684\u4e8b\u4ef6\u5faa\u73af\u3002<\/p>\n<p>\u5f15\u64ce\u4e0e\u4f1a\u8bdd\u5de5\u5382&#xff1a;<\/p>\n<p># app\/database.py<\/p>\n<p>from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession<\/p>\n<p>engine &#061; create_async_engine(&#034;sqlite&#043;aiosqlite:\/\/\/.\/dashboard.db&#034;, echo&#061;False)<br \/>\nasync_session &#061; async_sessionmaker(engine, class_&#061;AsyncSession, expire_on_commit&#061;False)<\/p>\n<p>async def get_db() -&gt; AsyncSession:<br \/>\n    &#034;&#034;&#034;FastAPI \u4f9d\u8d56\u6ce8\u5165\u5165\u53e3&#034;&#034;&#034;<br \/>\n    async with async_session() as session:<br \/>\n        yield session <\/p>\n<p>\u5173\u952e\u8bbe\u8ba1&#xff1a;<\/p>\n<ul>\n<li>expire_on_commit&#061;False&#xff1a;commit \u4e4b\u540e ORM \u5bf9\u8c61\u4ecd\u7136\u53ef\u7528&#xff08;\u9ed8\u8ba4\u4e3a True \u4f1a\u5bfc\u81f4 commit \u540e\u8bbf\u95ee\u5c5e\u6027\u89e6\u53d1\u989d\u5916\u67e5\u8be2&#xff09;<\/li>\n<\/ul>\n<ul>\n<li>get_db() \u662f\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668&#xff0c;\u901a\u8fc7 FastAPI \u7684 Depends \u6ce8\u5165\u5230\u6bcf\u4e2a\u8def\u7531\u51fd\u6570\u4e2d&#xff0c;\u8bf7\u6c42\u7ed3\u675f\u81ea\u52a8\u5173\u95ed\u4f1a\u8bdd<\/li>\n<\/ul>\n<ul>\n<li>\u4f7f\u7528 SQLite &#043; aiosqlite \u4f5c\u4e3a\u96f6\u914d\u7f6e\u5b58\u50a8&#xff0c;\u751f\u4ea7\u73af\u5883\u53ef\u65e0\u7f1d\u5207\u6362\u4e3a PostgreSQL&#xff08;\u6539\u8fde\u63a5\u5b57\u7b26\u4e32\u5373\u53ef&#xff09;<\/li>\n<\/ul>\n<p>\u6570\u636e\u6a21\u578b&#xff1a;<\/p>\n<p># app\/models.py&#xff08;\u7cbe\u7b80&#xff09;<\/p>\n<p>class User(Base):<br \/>\n    id: Mapped[int]          # \u81ea\u589e\u4e3b\u952e<br \/>\n    user_id: Mapped[str]     # \u4e1a\u52a1\u7528\u6237ID&#xff08;\u5982 &#034;\u5f20\u4e09&#034;&#xff09;<br \/>\n    name: Mapped[str]<br \/>\n    created_at \/ last_active_at<\/p>\n<p>class ChatSession(Base):<br \/>\n    session_id: Mapped[str]  # \u4e1a\u52a1\u4f1a\u8bddID&#xff08;\u5982 &#034;sess_a1b2c3&#8230;&#034;&#xff09;<br \/>\n    user_id \u2192 ForeignKey(&#034;users.user_id&#034;)<br \/>\n    title: Mapped[str]       # \u53d6\u9996\u6761\u6d88\u606f\u524d30\u5b57\u4f5c\u4e3a\u6807\u9898<\/p>\n<p>class Message(Base):<br \/>\n    session_id \u2192 ForeignKey(&#034;sessions.session_id&#034;)<br \/>\n    role: Mapped[str]        # &#034;user&#034; \u6216 &#034;assistant&#034;<br \/>\n    content: Mapped[str]<br \/>\n    model: Mapped[str]       # \u8bb0\u5f55\u662f\u54ea\u4e2a\u6a21\u578b\u56de\u590d\u7684<br \/>\n    tokens_input \/ tokens_output  # Token \u7528\u91cf<br \/>\n    cost: Mapped[float]      # \u672c\u6b21\u8c03\u7528\u8d39\u7528&#xff08;\u5143&#xff09;<br \/>\n    latency_ms: Mapped[int]  # \u54cd\u5e94\u5ef6\u8fdf&#xff08;\u6beb\u79d2&#xff09;<br \/>\n    created_at: Mapped[datetime] <\/p>\n<p>\u4e09\u5c42\u6a21\u578b\u5f62\u6210 User \u2192 ChatSession \u2192 Message \u7684\u5f52\u5c5e\u94fe\u3002\u5173\u952e\u7d22\u5f15 ix_messages_session_time \u8986\u76d6\u4e86&#034;\u6309\u4f1a\u8bdd\u67e5\u6d88\u606f\u3001\u6309\u65f6\u95f4\u6392\u5e8f&#034;\u7684\u9ad8\u9891\u67e5\u8be2\u8def\u5f84\u3002<\/p>\n<h4>4.3 \u7528\u91cf\u8ffd\u8e2a\u7f16\u6392&#xff1a;track_chat \u65b9\u6cd5<\/h4>\n<p>tracker.track_chat() \u662f\u6574\u4e2a\u7cfb\u7edf\u7684\u6838\u5fc3\u7f16\u6392\u65b9\u6cd5&#xff0c;\u4e00\u6b21\u8c03\u7528\u4e32\u8054\u4e86&#034;\u5efa\u7528\u6237 \u2192 \u5efa\u4f1a\u8bdd \u2192 \u5b58\u7528\u6237\u6d88\u606f \u2192 \u8c03 DeepSeek \u2192 \u5b58 AI \u56de\u590d \u2192 \u63a8\u9001\u4e8b\u4ef6&#034;\u7684\u5b8c\u6574\u6d41\u7a0b&#xff1a;<\/p>\n<p># app\/services\/tracker.py&#xff08;\u7cbe\u7b80&#xff09;<\/p>\n<p>class UsageTracker:<br \/>\n    async def track_chat(self, db, user_id, user_name, session_id,<br \/>\n                         user_message, history) -&gt; dict:<br \/>\n        # 1. \u786e\u4fdd\u7528\u6237\u5b58\u5728&#xff08;\u4e0d\u5b58\u5728\u5219\u521b\u5efa&#xff0c;\u5df2\u5b58\u5728\u5219\u66f4\u65b0\u6d3b\u8dc3\u65f6\u95f4&#xff09;<br \/>\n        await self.ensure_user(db, user_id, user_name)<\/p>\n<p>        # 2. \u786e\u4fdd\u4f1a\u8bdd\u5b58\u5728&#xff08;session_id \u4e3a\u7a7a\u5219\u81ea\u52a8\u521b\u5efa&#xff09;<br \/>\n        session &#061; await self.ensure_session(db, session_id, user_id, title)<\/p>\n<p>        # 3. \u5148\u5b58\u7528\u6237\u6d88\u606f<br \/>\n        await self.record_message(db, session.session_id, &#034;user&#034;, user_message)<\/p>\n<p>        # 4. \u8c03\u7528 DeepSeek&#xff08;\u62fc\u63a5\u5386\u53f2\u4e0a\u4e0b\u6587 &#043; \u5f53\u524d\u6d88\u606f&#xff09;<br \/>\n        messages_for_api &#061; history &#043; [{&#034;role&#034;: &#034;user&#034;, &#034;content&#034;: user_message}]<br \/>\n        result &#061; await deepseek_service.chat(messages_for_api)<br \/>\n        cost &#061; deepseek_service.calculate_cost(result.tokens_input,<br \/>\n                                                result.tokens_output)<\/p>\n<p>        # 5. \u5b58 AI \u56de\u590d&#xff08;\u542b Token\/\u8d39\u7528\/\u5ef6\u8fdf&#xff09;<br \/>\n        await self.record_message(db, session.session_id, &#034;assistant&#034;,<br \/>\n                                  result.content, model&#061;result.model,<br \/>\n                                  tokens_input&#061;result.tokens_input,<br \/>\n                                  tokens_output&#061;result.tokens_output,<br \/>\n                                  cost&#061;cost, latency_ms&#061;result.latency_ms)<br \/>\n        await db.commit()<\/p>\n<p>        # 6. WebSocket \u5e7f\u64ad\u5b9e\u65f6\u4e8b\u4ef6<br \/>\n        await ws_manager.broadcast(&#034;activity&#034;, {<br \/>\n            &#034;user_name&#034;: user_name,<br \/>\n            &#034;message_preview&#034;: user_message[:80],<br \/>\n            &#034;tokens_input&#034;: result.tokens_input,<br \/>\n            &#034;tokens_output&#034;: result.tokens_output,<br \/>\n            &#034;cost&#034;: cost,<br \/>\n            &#034;latency_ms&#034;: result.latency_ms,<br \/>\n            &#8230;<br \/>\n        })<\/p>\n<p>        return {&#034;reply&#034;: result.content, &#034;cost&#034;: cost, &#8230;} <\/p>\n<p>\u5173\u952e\u8bbe\u8ba1&#xff1a;<\/p>\n<ul>\n<li>\u5148\u8bb0\u5f55\u540e\u63d0\u4ea4&#xff1a;\u7528\u6237\u6d88\u606f\u548c AI \u56de\u590d\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u5199\u5165&#xff0c;\u8981\u4e48\u90fd\u6210\u529f&#xff0c;\u8981\u4e48\u90fd\u56de\u6eda<\/li>\n<\/ul>\n<ul>\n<li>commit \u5728 broadcast \u4e4b\u524d&#xff1a;\u786e\u4fdd\u63a8\u9001\u5230\u4eea\u8868\u76d8\u7684\u6570\u636e\u5df2\u7ecf\u6301\u4e45\u5316&#xff0c;\u4e0d\u4f1a\u51fa\u73b0&#034;\u4eea\u8868\u76d8\u663e\u793a\u4e86\u4f46\u6570\u636e\u5e93\u6ca1\u6709&#034;\u7684\u4e0d\u4e00\u81f4<\/li>\n<\/ul>\n<ul>\n<li>broadcast \u5728 commit \u4e4b\u540e&#xff1a;\u5982\u679c\u5e7f\u64ad\u5931\u8d25&#xff08;\u5982 WebSocket \u65ad\u5f00&#xff09;&#xff0c;\u4e0d\u5f71\u54cd\u6570\u636e\u6301\u4e45\u5316<\/li>\n<\/ul>\n<ul>\n<li>\u8d39\u7528\u8ba1\u7b97&#xff1a;\u57fa\u4e8e DeepSeek \u5b98\u65b9\u5b9a\u4ef7&#xff08;\u8f93\u5165 1 \u5143\/\u767e\u4e07 Token&#xff0c;\u8f93\u51fa 2 \u5143\/\u767e\u4e07 Token&#xff09;&#xff0c;\u5728 config.py \u4e2d\u53ef\u914d\u7f6e<\/li>\n<\/ul>\n<h4>4.4 WebSocket \u5b9e\u65f6\u63a8\u9001\u67b6\u6784<\/h4>\n<p>\u8fd9\u662f\u4eea\u8868\u76d8&#034;\u5b9e\u65f6&#034;\u80fd\u529b\u7684\u6838\u5fc3\u3002\u6574\u4e2a\u63a8\u9001\u94fe\u8def\u5206\u4e3a\u4e09\u5c42&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u5c42&#xff1a;\u8fde\u63a5\u7ba1\u7406\u5668&#xff08;ws\/manager.py&#xff09;<\/p>\n<p>class WebSocketManager:<br \/>\n    def __init__(self):<br \/>\n        self._connections: list[WebSocket] &#061; []  # \u6240\u6709\u6d3b\u8dc3\u8fde\u63a5<\/p>\n<p>    async def connect(self, ws: WebSocket):<br \/>\n        await ws.accept()<br \/>\n        self._connections.append(ws)<\/p>\n<p>    def disconnect(self, ws: WebSocket):<br \/>\n        if ws in self._connections:<br \/>\n            self._connections.remove(ws)<\/p>\n<p>    async def broadcast(self, event_type: str, data: dict):<br \/>\n        if not self._connections:<br \/>\n            return  # \u6ca1\u6709\u8fde\u63a5\u65f6\u76f4\u63a5\u8df3\u8fc7&#xff0c;\u96f6\u5f00\u9500<\/p>\n<p>        payload &#061; json.dumps({&#034;type&#034;: event_type, &#034;data&#034;: data}, ensure_ascii&#061;False)<br \/>\n        dead &#061; []<br \/>\n        for ws in self._connections:<br \/>\n            try:<br \/>\n                await ws.send_text(payload)<br \/>\n            except Exception:<br \/>\n                dead.append(ws)       # \u6536\u96c6\u53d1\u9001\u5931\u8d25\u7684\u8fde\u63a5<br \/>\n        for ws in dead:<br \/>\n            self.disconnect(ws)       # \u6e05\u7406\u6b7b\u8fde\u63a5 <\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>broadcast \u5728\u6ca1\u6709\u8fde\u63a5\u65f6\u76f4\u63a5\u8fd4\u56de&#xff0c;\u4e0d\u5f71\u54cd\u5bf9\u8bdd\u670d\u52a1\u6027\u80fd<\/li>\n<\/ul>\n<ul>\n<li>\u53d1\u9001\u5931\u8d25\u7684\u8fde\u63a5\u88ab\u6536\u96c6\u5230 dead \u5217\u8868&#xff0c;\u5e7f\u64ad\u7ed3\u675f\u540e\u7edf\u4e00\u6e05\u7406&#xff0c;\u907f\u514d\u904d\u5386\u8fc7\u7a0b\u4e2d\u4fee\u6539\u5217\u8868<\/li>\n<\/ul>\n<ul>\n<li>ensure_ascii&#061;False \u786e\u4fdd\u4e2d\u6587\u6d88\u606f\u6b63\u5e38\u4f20\u8f93<\/li>\n<\/ul>\n<p>\u7b2c\u4e8c\u5c42&#xff1a;WebSocket \u7aef\u70b9&#xff08;routers\/dashboard.py&#xff09;<\/p>\n<p>&#064;router.websocket(&#034;\/ws&#034;)<br \/>\nasync def dashboard_ws(ws: WebSocket):<br \/>\n    await ws_manager.connect(ws)<br \/>\n    try:<br \/>\n        while True:<br \/>\n            await ws.receive_text()  # \u4fdd\u6301\u8fde\u63a5&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u6d88\u606f<br \/>\n    except WebSocketDisconnect:<br \/>\n        ws_manager.disconnect(ws) <\/p>\n<p>\u8fd9\u4e2a\u7aef\u70b9\u672c\u8eab\u4e0d\u5904\u7406\u5ba2\u6237\u7aef\u53d1\u6765\u7684\u6d88\u606f&#xff08;\u4eea\u8868\u76d8\u53ea\u9700\u8981\u63a5\u6536\u63a8\u9001&#xff09;&#xff0c;receive_text() \u7684\u4f5c\u7528\u662f\u7ef4\u6301\u8fde\u63a5\u5b58\u6d3b\u3002\u5f53\u5ba2\u6237\u7aef\u65ad\u5f00\u65f6\u629b\u51fa WebSocketDisconnect \u5f02\u5e38&#xff0c;\u89e6\u53d1\u6e05\u7406\u3002<\/p>\n<p>\u7b2c\u4e09\u5c42&#xff1a;\u524d\u7aef\u6d88\u8d39&#xff08;dashboard.js&#xff09;<\/p>\n<p>function connectWebSocket() {<br \/>\n    const protocol &#061; location.protocol &#061;&#061;&#061; &#039;https:&#039; ? &#039;wss:&#039; : &#039;ws:&#039;;<br \/>\n    const ws &#061; new WebSocket(&#096;${protocol}\/\/${location.host}\/api\/dashboard\/ws&#096;);<\/p>\n<p>    ws.onopen &#061; () &#061;&gt; {<br \/>\n        document.getElementById(&#039;wsStatus&#039;).textContent &#061; &#039;\u5df2\u8fde\u63a5&#039;;<br \/>\n    };<\/p>\n<p>    ws.onmessage &#061; (event) &#061;&gt; {<br \/>\n        const msg &#061; JSON.parse(event.data);<br \/>\n        if (msg.type &#061;&#061;&#061; &#039;activity&#039;) {<br \/>\n            addActivityItem(msg.data);  \/\/ \u63d2\u5165\u5230\u6d3b\u52a8\u6d41\u9876\u90e8<br \/>\n            loadStats();                \/\/ \u5237\u65b0\u7edf\u8ba1\u5361\u7247<br \/>\n            loadUsers();                \/\/ \u5237\u65b0\u7528\u6237\u6392\u884c<br \/>\n        }<br \/>\n    };<\/p>\n<p>    ws.onclose &#061; () &#061;&gt; {<br \/>\n        document.getElementById(&#039;wsStatus&#039;).textContent &#061; &#039;\u5df2\u65ad\u5f00&#039;;<br \/>\n        setTimeout(connectWebSocket, 5000);  \/\/ 5\u79d2\u540e\u81ea\u52a8\u91cd\u8fde<br \/>\n    };<br \/>\n} <\/p>\n<p>\u524d\u7aef\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u81ea\u52a8\u9009\u62e9 ws:\/\/ \u6216 wss:\/\/ \u534f\u8bae&#xff0c;\u9002\u914d HTTP\/HTTPS \u90e8\u7f72<\/li>\n<\/ul>\n<ul>\n<li>\u6536\u5230\u6d88\u606f\u540e\u9664\u4e86\u63d2\u5165\u6d3b\u52a8\u6d41&#xff0c;\u8fd8\u4e3b\u52a8\u62c9\u53d6\u6700\u65b0\u7684\u7edf\u8ba1\u6570\u636e\u548c\u7528\u6237\u5217\u8868&#xff08;\u56e0\u4e3a WebSocket \u53ea\u63a8\u9001&#034;\u589e\u91cf\u4e8b\u4ef6&#034;&#xff0c;\u805a\u5408\u7edf\u8ba1\u9700\u8981\u91cd\u65b0\u67e5\u8be2&#xff09;<\/li>\n<\/ul>\n<ul>\n<li>\u65ad\u7ebf\u540e 5 \u79d2\u81ea\u52a8\u91cd\u8fde&#xff0c;\u4fdd\u8bc1\u957f\u65f6\u95f4\u6253\u5f00\u4eea\u8868\u76d8\u65f6\u7684\u8fde\u63a5\u53ef\u9760\u6027<\/li>\n<\/ul>\n<h4>4.5 \u591a\u8f6e\u5bf9\u8bdd\u4e0a\u4e0b\u6587\u7ba1\u7406<\/h4>\n<p>\u5bf9\u8bdd\u670d\u52a1\u652f\u6301\u591a\u8f6e\u5bf9\u8bdd&#xff0c;\u5173\u952e\u5728\u4e8e\u5982\u4f55\u6784\u5efa\u4e0a\u4e0b\u6587&#xff1a;<\/p>\n<p># app\/routers\/chat.py<\/p>\n<p>&#064;router.post(&#034;&#034;)<br \/>\nasync def chat(req: ChatRequest, db: AsyncSession &#061; Depends(get_db)):<br \/>\n    history: list[dict] &#061; []<\/p>\n<p>    if req.session_id:<br \/>\n        # \u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u8be5\u4f1a\u8bdd\u7684\u5386\u53f2\u6d88\u606f<br \/>\n        result &#061; await db.execute(<br \/>\n            select(Message)<br \/>\n            .where(Message.session_id &#061;&#061; req.session_id)<br \/>\n            .order_by(Message.id)<br \/>\n        )<br \/>\n        msgs &#061; result.scalars().all()<br \/>\n        # \u53ea\u53d6\u6700\u8fd1 10 \u6761\u4f5c\u4e3a\u4e0a\u4e0b\u6587&#xff0c;\u63a7\u5236 Token \u6d88\u8017<br \/>\n        history &#061; [<br \/>\n            {&#034;role&#034;: m.role, &#034;content&#034;: m.content}<br \/>\n            for m in msgs[-10:]<br \/>\n        ]<\/p>\n<p>    result &#061; await tracker.track_chat(<br \/>\n        db&#061;db, user_id&#061;req.user_id, user_message&#061;req.message,<br \/>\n        session_id&#061;req.session_id, history&#061;history,<br \/>\n    )<br \/>\n    return ChatResponse(**result) <\/p>\n<p>\u5173\u952e\u8bbe\u8ba1&#xff1a;<\/p>\n<ul>\n<li>\u4e0a\u4e0b\u6587\u4ece\u6570\u636e\u5e93\u6301\u4e45\u5316\u52a0\u8f7d&#xff0c;\u800c\u975e\u4f9d\u8d56\u5185\u5b58\u2014\u2014\u670d\u52a1\u91cd\u542f\u540e\u5bf9\u8bdd\u4e0d\u4e22\u5931<\/li>\n<\/ul>\n<ul>\n<li>msgs[-10:] \u53ea\u53d6\u6700\u8fd1 10 \u6761\u6d88\u606f&#xff0c;\u907f\u514d\u4e0a\u4e0b\u6587\u65e0\u9650\u81a8\u80c0\u5bfc\u81f4 Token \u6d88\u8017\u5931\u63a7<\/li>\n<\/ul>\n<ul>\n<li>session_id \u9996\u6b21\u8bf7\u6c42\u65f6\u4e3a None&#xff0c;\u7531 tracker.ensure_session() \u81ea\u52a8\u521b\u5efa\u5e76\u8fd4\u56de&#xff0c;\u540e\u7eed\u8bf7\u6c42\u643a\u5e26\u6b64 ID \u5373\u53ef\u5ef6\u7eed\u5bf9\u8bdd<\/li>\n<\/ul>\n<h4>4.6 \u4eea\u8868\u76d8\u805a\u5408\u67e5\u8be2<\/h4>\n<p>\u4eea\u8868\u76d8\u7684\u7edf\u8ba1\u63a5\u53e3\u5927\u91cf\u4f7f\u7528 SQL \u805a\u5408\u51fd\u6570&#xff0c;\u76f4\u63a5\u5728\u6570\u636e\u5e93\u5c42\u9762\u5b8c\u6210\u8ba1\u7b97&#xff0c;\u907f\u514d\u628a\u5168\u91cf\u6570\u636e\u62c9\u5230 Python \u4e2d\u5904\u7406&#xff1a;<\/p>\n<p># \u603b\u89c8\u7edf\u8ba1 \u2014\u2014 \u4e00\u6b21\u8bf7\u6c42\u62ff\u5230 7 \u4e2a\u6307\u6807<br \/>\n&#064;router.get(&#034;\/stats&#034;)<br \/>\nasync def get_stats(db: AsyncSession &#061; Depends(get_db)):<br \/>\n    total_users &#061; await db.scalar(select(func.count(User.id)))<br \/>\n    total_cost &#061; await db.scalar(<br \/>\n        select(func.coalesce(func.sum(Message.cost), 0.0))<br \/>\n    )<br \/>\n    avg_latency &#061; await db.scalar(<br \/>\n        select(func.coalesce(func.avg(Message.latency_ms), 0.0))<br \/>\n        .where(Message.role &#061;&#061; &#034;assistant&#034;)<br \/>\n    )<br \/>\n    active_24h &#061; await db.scalar(<br \/>\n        select(func.count(func.distinct(ChatSession.user_id)))<br \/>\n        .where(ChatSession.updated_at &gt;&#061; cutoff)<br \/>\n    )<br \/>\n    &#8230;<br \/>\n# \u7528\u6237\u7528\u91cf\u6392\u884c \u2014\u2014 \u4e09\u8868 JOIN &#043; GROUP BY<br \/>\nstmt &#061; (<br \/>\n    select(<br \/>\n        ChatSession.user_id,<br \/>\n        User.name.label(&#034;name&#034;),<br \/>\n        func.count(Message.id).label(&#034;message_count&#034;),<br \/>\n        func.sum(Message.tokens_input).label(&#034;tokens_input&#034;),<br \/>\n        func.sum(Message.cost).label(&#034;total_cost&#034;),<br \/>\n        func.avg(Message.latency_ms).label(&#034;avg_latency_ms&#034;),<br \/>\n    )<br \/>\n    .select_from(Message)<br \/>\n    .join(ChatSession, Message.session_id &#061;&#061; ChatSession.session_id)<br \/>\n    .join(User, ChatSession.user_id &#061;&#061; User.user_id)<br \/>\n    .where(Message.role &#061;&#061; &#034;assistant&#034;)<br \/>\n    .group_by(ChatSession.user_id, User.name)<br \/>\n    .order_by(desc(&#034;total_cost&#034;))<br \/>\n)<br \/>\n# 24\u5c0f\u65f6\u65f6\u95f4\u7ebf \u2014\u2014 \u6309\u5c0f\u65f6\u805a\u5408<br \/>\nstmt &#061; (<br \/>\n    select(<br \/>\n        func.strftime(&#034;%Y-%m-%dT%H:00:00&#034;, Message.created_at).label(&#034;ts&#034;),<br \/>\n        func.count(Message.id).label(&#034;messages&#034;),<br \/>\n        func.sum(Message.tokens_input &#043; Message.tokens_output).label(&#034;tokens&#034;),<br \/>\n        func.sum(Message.cost).label(&#034;cost&#034;),<br \/>\n    )<br \/>\n    .where(Message.created_at &gt;&#061; cutoff)<br \/>\n    .group_by(&#034;ts&#034;)<br \/>\n    .order_by(&#034;ts&#034;)<br \/>\n) <\/p>\n<p>\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>func.coalesce(sum(&#8230;), 0) \u786e\u4fdd\u65e0\u6570\u636e\u65f6\u8fd4\u56de 0 \u800c\u975e None<\/li>\n<\/ul>\n<ul>\n<li>func.distinct() \u7528\u4e8e\u7edf\u8ba1\u53bb\u91cd\u6307\u6807&#xff08;\u5982 24h \u6d3b\u8dc3\u7528\u6237\u6570&#xff09;<\/li>\n<\/ul>\n<ul>\n<li>func.strftime() \u5728 SQLite \u4e2d\u505a\u65f6\u95f4\u622a\u65ad\u805a\u5408&#xff0c;\u6309\u5c0f\u65f6\u5206\u7ec4<\/li>\n<\/ul>\n<ul>\n<li>\u5ef6\u8fdf\u5e73\u5747\u503c\u53ea\u7edf\u8ba1 role &#061;&#061; &#034;assistant&#034; \u7684\u6d88\u606f&#xff0c;\u56e0\u4e3a\u7528\u6237\u6d88\u606f\u6ca1\u6709 API \u8c03\u7528\u5ef6\u8fdf<\/li>\n<\/ul>\n<h4>4.7 \u524d\u7aef\u53ef\u89c6\u5316&#xff1a;Chart.js \u8d8b\u52bf\u56fe<\/h4>\n<p>\u4eea\u8868\u76d8\u4f7f\u7528 Chart.js \u7ed8\u5236 Token \u548c\u8d39\u7528\u7684\u65f6\u95f4\u8d8b\u52bf\u56fe&#xff1a;<\/p>\n<p>async function loadTimeline() {<br \/>\n    const res &#061; await fetch(&#096;${API}\/timeline?hours&#061;24&#096;);<br \/>\n    const points &#061; await res.json();<\/p>\n<p>    const labels &#061; points.map(p &#061;&gt; {<br \/>\n        const d &#061; new Date(p.timestamp);<br \/>\n        return d.getHours().toString().padStart(2, &#039;0&#039;) &#043; &#039;:00&#039;;<br \/>\n    });<\/p>\n<p>    tokenChart &#061; new Chart(tokenCtx, {<br \/>\n        type: &#039;line&#039;,<br \/>\n        data: {<br \/>\n            labels: labels,<br \/>\n            datasets: [{<br \/>\n                label: &#039;Tokens&#039;,<br \/>\n                data: points.map(p &#061;&gt; p.tokens),<br \/>\n                borderColor: &#039;#4f6ef7&#039;,<br \/>\n                backgroundColor: &#039;rgba(79,110,247,0.08)&#039;,<br \/>\n                fill: true,<br \/>\n                tension: 0.3,        \/\/ \u5e73\u6ed1\u66f2\u7ebf<br \/>\n                pointRadius: 2,<br \/>\n            }]<br \/>\n        },<br \/>\n        options: {<br \/>\n            responsive: true,<br \/>\n            maintainAspectRatio: false,<br \/>\n            plugins: { legend: { display: false } },<br \/>\n        }<br \/>\n    });<br \/>\n} <\/p>\n<p>\u65f6\u95f4\u7ebf\u6bcf 60 \u79d2\u81ea\u52a8\u5237\u65b0\u4e00\u6b21&#xff0c;\u914d\u5408 WebSocket \u7684\u5b9e\u65f6\u63a8\u9001&#xff0c;\u5f62\u6210&#034;\u5b9e\u65f6\u4e8b\u4ef6\u6d41 &#043; \u5b9a\u65f6\u8d8b\u52bf\u5237\u65b0&#034;\u7684\u53cc\u5c42\u66f4\u65b0\u673a\u5236\u3002<\/p>\n<h4>4.8 \u914d\u7f6e\u7ba1\u7406\u4e0e\u5b9a\u4ef7\u6a21\u578b<\/h4>\n<p>\u4f7f\u7528 pydantic-settings \u7ba1\u7406\u914d\u7f6e&#xff0c;\u652f\u6301\u73af\u5883\u53d8\u91cf\u6ce8\u5165&#xff1a;<\/p>\n<p># app\/config.py<\/p>\n<p>class Settings(BaseSettings):<br \/>\n    deepseek_api_key: str &#061; os.getenv(&#034;DEEPSEEK_API_KEY&#034;, &#034;&#034;)<br \/>\n    deepseek_base_url: str &#061; &#034;https:\/\/api.deepseek.com&#034;<br \/>\n    deepseek_model: str &#061; &#034;deepseek-chat&#034;<\/p>\n<p>    # DeepSeek \u5b9a\u4ef7&#xff08;\u5143\/\u767e\u4e07 Token&#xff09;<br \/>\n    price_input_per_million: float &#061; 1.0   # \u8f93\u5165<br \/>\n    price_output_per_million: float &#061; 2.0  # \u8f93\u51fa<\/p>\n<p>    database_url: str &#061; &#034;sqlite&#043;aiosqlite:\/\/\/.\/dashboard.db&#034;<br \/>\n    host: str &#061; &#034;0.0.0.0&#034;<br \/>\n    port: int &#061; 8000 <\/p>\n<p>\u8d39\u7528\u8ba1\u7b97\u903b\u8f91&#xff1a;<\/p>\n<p>def calculate_cost(self, tokens_input: int, tokens_output: int) -&gt; float:<br \/>\n    cost_in &#061; tokens_input \/ 1_000_000 * settings.price_input_per_million<br \/>\n    cost_out &#061; tokens_output \/ 1_000_000 * settings.price_output_per_million<br \/>\n    return round(cost_in &#043; cost_out, 6) <\/p>\n<p>\u5207\u6362\u6a21\u578b\u65f6\u53ea\u9700\u4fee\u6539 deepseek_model \u548c\u5b9a\u4ef7\u53c2\u6570&#xff0c;\u65e0\u9700\u6539\u4ee3\u7801\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001API \u63a5\u53e3\u4e00\u89c8<\/h3>\n<h4>\u5bf9\u8bdd\u670d\u52a1<\/h4>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u65b9\u6cd5<\/p>\n<\/td>\n<td>\n<p>\u8def\u5f84<\/p>\n<\/td>\n<td>\n<p>\u8bf4\u660e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>POST<\/p>\n<\/td>\n<td>\n<p>\/api\/chat<\/p>\n<\/td>\n<td>\n<p>\u53d1\u9001\u6d88\u606f&#xff0c;\u83b7\u53d6 AI \u56de\u590d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/chat\/sessions<\/p>\n<\/td>\n<td>\n<p>\u83b7\u53d6\u4f1a\u8bdd\u5217\u8868&#xff08;\u652f\u6301 user_id \u8fc7\u6ee4&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/chat\/sessions\/{id}<\/p>\n<\/td>\n<td>\n<p>\u83b7\u53d6\u4f1a\u8bdd\u8be6\u60c5&#xff08;\u542b\u5b8c\u6574\u6d88\u606f\u5386\u53f2&#xff09;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>POST \/api\/chat \u8bf7\u6c42\u4f53&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;message&#034;: &#034;\u5e2e\u6211\u5199\u4e00\u9996\u5173\u4e8e\u79cb\u5929\u7684\u8bd7&#034;,<br \/>\n  &#034;user_id&#034;: &#034;zhangsan&#034;,<br \/>\n  &#034;user_name&#034;: &#034;\u5f20\u4e09&#034;,<br \/>\n  &#034;session_id&#034;: null<br \/>\n} <\/p>\n<p>\u54cd\u5e94&#xff1a;<\/p>\n<p>{<br \/>\n  &#034;session_id&#034;: &#034;sess_a1b2c3d4e5f6&#034;,<br \/>\n  &#034;reply&#034;: &#034;\u79cb\u98ce\u8d77\u5904\u53f6\u7eb7\u98de&#8230;&#034;,<br \/>\n  &#034;model&#034;: &#034;deepseek-chat&#034;,<br \/>\n  &#034;tokens_input&#034;: 52,<br \/>\n  &#034;tokens_output&#034;: 128,<br \/>\n  &#034;cost&#034;: 0.000308,<br \/>\n  &#034;latency_ms&#034;: 1840<br \/>\n} <\/p>\n<h4>\u4eea\u8868\u76d8\u670d\u52a1<\/h4>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u65b9\u6cd5<\/p>\n<\/td>\n<td>\n<p>\u8def\u5f84<\/p>\n<\/td>\n<td>\n<p>\u8bf4\u660e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/dashboard\/stats<\/p>\n<\/td>\n<td>\n<p>\u603b\u89c8\u7edf\u8ba1&#xff08;7 \u4e2a\u6307\u6807&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/dashboard\/users<\/p>\n<\/td>\n<td>\n<p>\u7528\u6237\u7528\u91cf\u6392\u884c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/dashboard\/timeline<\/p>\n<\/td>\n<td>\n<p>24 \u5c0f\u65f6\u65f6\u95f4\u7ebf&#xff08;\u6309\u5c0f\u65f6\u805a\u5408&#xff09;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>GET<\/p>\n<\/td>\n<td>\n<p>\/api\/dashboard\/recent-activity<\/p>\n<\/td>\n<td>\n<p>\u6700\u8fd1 20 \u6761\u6d3b\u52a8<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>WS<\/p>\n<\/td>\n<td>\n<p>\/api\/dashboard\/ws<\/p>\n<\/td>\n<td>\n<p>WebSocket \u5b9e\u65f6\u63a8\u9001\u7aef\u70b9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u516d\u3001\u6570\u636e\u6d41\u5168\u666f<\/h3>\n<p>\u628a\u6240\u6709\u73af\u8282\u4e32\u8d77\u6765&#xff0c;\u4e00\u6b21\u5b8c\u6574\u7684\u5bf9\u8bdd\u8bf7\u6c42\u7ecf\u8fc7\u4ee5\u4e0b\u6b65\u9aa4&#xff1a;<\/p>\n<p>1. \u7528\u6237\u5728 \/chat \u9875\u9762\u8f93\u5165\u6d88\u606f<br \/>\n2. chat.js \u53d1\u9001 POST \/api\/chat<br \/>\n3. chat.py \u8def\u7531\u6536\u5230\u8bf7\u6c42&#xff0c;\u4ece DB \u52a0\u8f7d\u6700\u8fd1 10 \u6761\u5386\u53f2\u6d88\u606f<br \/>\n4. \u8c03\u7528 tracker.track_chat():<br \/>\n   a. ensure_user()     \u2192 \u521b\u5efa\u6216\u66f4\u65b0\u7528\u6237\u8bb0\u5f55<br \/>\n   b. ensure_session()  \u2192 \u521b\u5efa\u6216\u66f4\u65b0\u4f1a\u8bdd\u8bb0\u5f55<br \/>\n   c. record_message()  \u2192 \u5b58\u50a8\u7528\u6237\u6d88\u606f<br \/>\n   d. deepseek_service.chat()  \u2192 \u5f02\u6b65\u8c03\u7528 DeepSeek API<br \/>\n   e. calculate_cost()  \u2192 \u8ba1\u7b97\u8d39\u7528<br \/>\n   f. record_message()  \u2192 \u5b58\u50a8 AI \u56de\u590d&#xff08;\u542b Token\/\u8d39\u7528\/\u5ef6\u8fdf&#xff09;<br \/>\n   g. db.commit()       \u2192 \u63d0\u4ea4\u4e8b\u52a1<br \/>\n   h. ws_manager.broadcast() \u2192 WebSocket \u63a8\u9001\u4e8b\u4ef6<br \/>\n5. \u8fd4\u56de JSON \u54cd\u5e94\u7ed9 chat.js<br \/>\n6. chat.js \u66f4\u65b0\u5bf9\u8bdd\u754c\u9762&#xff0c;\u663e\u793a\u56de\u590d\u548c\u7528\u91cf<br \/>\n7. dashboard.js \u6536\u5230 WebSocket \u6d88\u606f:<br \/>\n   a. \u63d2\u5165\u6d3b\u52a8\u6d41\u9876\u90e8<br \/>\n   b. \u5237\u65b0\u7edf\u8ba1\u5361\u7247<br \/>\n   c. \u5237\u65b0\u7528\u6237\u6392\u884c <\/p>\n<p>\u6574\u4e2a\u8fc7\u7a0b\u4e2d&#xff0c;DeepSeek API \u8c03\u7528\u662f\u6700\u6162\u7684\u4e00\u73af&#xff08;\u901a\u5e38 1-3 \u79d2&#xff09;&#xff0c;\u5176\u4ed6\u73af\u8282\u90fd\u662f\u6beb\u79d2\u7ea7\u3002\u7531\u4e8e\u5168\u90e8\u5f02\u6b65\u5b9e\u73b0&#xff0c;\u5373\u4f7f\u6709\u591a\u4e2a\u7528\u6237\u540c\u65f6\u5bf9\u8bdd&#xff0c;\u4e5f\u4e0d\u4f1a\u4e92\u76f8\u963b\u585e\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u6269\u5c55\u65b9\u5411<\/h3>\n<p>\u8fd9\u4e2a Demo \u662f\u4e00\u4e2a\u6700\u5c0f\u53ef\u8fd0\u884c\u7248\u672c&#xff0c;\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5b9e\u7528\u7684\u6269\u5c55\u65b9\u5411&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u65b9\u5411<\/p>\n<\/td>\n<td>\n<p>\u505a\u6cd5<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6d41\u5f0f\u8f93\u51fa<\/p>\n<\/td>\n<td>\n<p>\u4f7f\u7528 DeepSeek \u7684 stream&#061;True &#043; FastAPI \u7684 StreamingResponse&#xff0c;\u8ba9\u7528\u6237\u9010\u5b57\u770b\u5230\u56de\u590d<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u7528\u6237\u8ba4\u8bc1<\/p>\n<\/td>\n<td>\n<p>\u52a0\u5165 JWT \u8ba4\u8bc1&#xff0c;user_id \u4ece Token \u4e2d\u63d0\u53d6\u800c\u975e\u524d\u7aef\u4f20\u5165<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6570\u636e\u5e93\u8fc1\u79fb<\/p>\n<\/td>\n<td>\n<p>\u5f15\u5165 Alembic \u7ba1\u7406\u8868\u7ed3\u6784\u53d8\u66f4&#xff0c;\u652f\u6301\u5e73\u6ed1\u5347\u7ea7<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u591a\u6a21\u578b\u652f\u6301<\/p>\n<\/td>\n<td>\n<p>\u5728 config.py \u4e2d\u914d\u7f6e\u591a\u4e2a\u6a21\u578b&#xff0c;\u524d\u7aef\u53ef\u9009\u62e9&#xff0c;\u7edf\u8ba1\u6309\u6a21\u578b\u5206\u7ec4<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u544a\u8b66\u673a\u5236<\/p>\n<\/td>\n<td>\n<p>\u5f53\u67d0\u7528\u6237 Token \u6d88\u8017\u8d85\u8fc7\u9608\u503c\u65f6&#xff0c;\u901a\u8fc7 WebSocket \u63a8\u9001\u544a\u8b66\u4e8b\u4ef6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u6570\u636e\u5bfc\u51fa<\/p>\n<\/td>\n<td>\n<p>\u589e\u52a0\u5bfc\u51fa CSV\/Excel \u63a5\u53e3&#xff0c;\u65b9\u4fbf\u79bb\u7ebf\u5206\u6790<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u90e8\u7f72\u4e0a\u7ebf<\/p>\n<\/td>\n<td>\n<p>\u7528 Gunicorn &#043; Uvicorn worker \u90e8\u7f72&#xff0c;\u6570\u636e\u5e93\u5207\u6362\u4e3a PostgreSQL<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<p>\u8fd9\u4e2a Demo \u5c55\u793a\u4e86\u6784\u5efa AI \u5e94\u7528\u76d1\u63a7\u4f53\u7cfb\u7684\u51e0\u4e2a\u6838\u5fc3\u6a21\u5f0f&#xff1a;<\/p>\n<li>API \u517c\u5bb9\u590d\u7528&#xff1a;DeepSeek \u517c\u5bb9 OpenAI \u63a5\u53e3&#xff0c;\u76f4\u63a5\u7528 openai SDK \u5373\u53ef\u96c6\u6210&#xff0c;\u96f6\u989d\u5916\u5b66\u4e60\u6210\u672c<\/li>\n<li>\u5168\u94fe\u8def\u5f02\u6b65&#xff1a;\u4ece\u6570\u636e\u5e93&#xff08;aiosqlite&#xff09;\u5230 HTTP&#xff08;FastAPI&#xff09;\u5230 LLM \u8c03\u7528&#xff08;AsyncOpenAI&#xff09;&#xff0c;\u5168\u5f02\u6b65\u5b9e\u73b0&#xff0c;\u9ad8\u5e76\u53d1\u53cb\u597d<\/li>\n<li>\u8ffd\u8e2a\u5d4c\u5165\u5f0f\u8bbe\u8ba1&#xff1a;\u7528\u91cf\u8ffd\u8e2a\u903b\u8f91\u5185\u5d4c\u5728\u5bf9\u8bdd\u6d41\u7a0b\u4e2d&#xff08;track_chat \u65b9\u6cd5&#xff09;&#xff0c;\u4e1a\u52a1\u4ee3\u7801\u65e0\u9700\u611f\u77e5<\/li>\n<li>WebSocket \u5b9e\u65f6\u63a8\u9001&#xff1a;\u8f7b\u91cf\u7ea7\u7684\u8fde\u63a5\u7ba1\u7406\u5668 &#043; \u5e7f\u64ad\u6a21\u5f0f&#xff0c;\u5b9e\u73b0\u4eea\u8868\u76d8\u7684\u5b9e\u65f6\u66f4\u65b0<\/li>\n<li>SQL \u805a\u5408\u4e0b\u6c89&#xff1a;\u7edf\u8ba1\u8ba1\u7b97\u76f4\u63a5\u7528 SQL \u805a\u5408\u51fd\u6570\u5b8c\u6210&#xff0c;\u907f\u514d\u628a\u6570\u636e\u62c9\u5230\u5e94\u7528\u5c42\u5904\u7406<\/li>\n<p>\u9879\u76ee\u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e&#xff1a;\u5b83\u628a&#034;AI \u5bf9\u8bdd&#034;\u548c&#034;\u7528\u91cf\u76d1\u63a7&#034;\u8fd9\u4e24\u4ef6\u4e8b\u7528\u6700\u5c0f\u4ee3\u4ef7\u4e32\u8054\u8d77\u6765\u4e86\u3002\u5982\u679c\u4f60\u6b63\u5728\u505a\u4e00\u4e2a\u63a5\u5165\u5927\u6a21\u578b\u7684\u5e94\u7528&#xff0c;\u8fd9\u4e2a\u67b6\u6784\u53ef\u4ee5\u76f4\u63a5\u4f5c\u4e3a\u8d77\u70b9\u3002<\/p>\n<h3>\u4e5d\u3001\u6e90\u4ee3\u7801\u5730\u5740<\/h3>\n<p>Dashboard<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u7684 AI \u5e94\u7528\u5f00\u59cb\u6709\u771f\u5b9e\u7528\u6237\u65f6&#xff0c;\\&#8221;\u6a21\u578b\u56de\u590d\u4e86\u4ec0\u4e48\\&#8221;\u5df2\u7ecf\u4e0d\u591f\u4e86\u2014\u2014\u4f60\u9700\u8981\u77e5\u9053\u8c01\u5728\u7528\u3001\u7528\u4e86\u591a\u5c11 Token\u3001\u82b1\u4e86\u591a\u5c11\u94b1\u3001\u54cd\u5e94\u6709\u591a\u5feb\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684 Demo \u9879\u76ee&#xff0c;\u5c55\u793a\u5982\u4f55\u7528 FastAPI  DeepSeek  WebSocket \u6784\u5efa\u4e00\u4e2a\u5e26\u5b9e\u65f6\u76d1\u63a7\u4eea\u8868\u76d8\u7684 AI \u5bf9\u8bdd\u670d\u52a1\u3002 \u4e00\u3001\u8fd9\u4e2a Demo \u505a\u4e86\u4ec0\u4e48<br \/>\n\u9879\u76ee\u63d0\u4f9b\u4e24\u4e2a\u670d\u52a1&#xff0c;\u8dd1\u5728\u540c\u4e00\u4e2a FastAPI \u5e94\u7528\u91cc&#xff1a; \u670d\u52a1  \u9762\u5411  \u529f\u80fd  AI \u5bf9\u8bdd\u670d\u52a1  \u7ec8\u7aef\u7528\u6237 <\/p>\n","protected":false},"author":2,"featured_media":85305,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[72,947,50],"topic":[],"class_list":["post-85307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-fastapi","tag-websocket","tag-50"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/85307.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f53\u4f60\u7684 AI \u5e94\u7528\u5f00\u59cb\u6709\u771f\u5b9e\u7528\u6237\u65f6&#xff0c;&quot;\u6a21\u578b\u56de\u590d\u4e86\u4ec0\u4e48&quot;\u5df2\u7ecf\u4e0d\u591f\u4e86\u2014\u2014\u4f60\u9700\u8981\u77e5\u9053\u8c01\u5728\u7528\u3001\u7528\u4e86\u591a\u5c11 Token\u3001\u82b1\u4e86\u591a\u5c11\u94b1\u3001\u54cd\u5e94\u6709\u591a\u5feb\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684 Demo \u9879\u76ee&#xff0c;\u5c55\u793a\u5982\u4f55\u7528 FastAPI DeepSeek WebSocket \u6784\u5efa\u4e00\u4e2a\u5e26\u5b9e\u65f6\u76d1\u63a7\u4eea\u8868\u76d8\u7684 AI \u5bf9\u8bdd\u670d\u52a1\u3002 \u4e00\u3001\u8fd9\u4e2a Demo \u505a\u4e86\u4ec0\u4e48 \u9879\u76ee\u63d0\u4f9b\u4e24\u4e2a\u670d\u52a1&#xff0c;\u8dd1\u5728\u540c\u4e00\u4e2a FastAPI \u5e94\u7528\u91cc&#xff1a; \u670d\u52a1 \u9762\u5411 \u529f\u80fd AI \u5bf9\u8bdd\u670d\u52a1 \u7ec8\u7aef\u7528\u6237\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/85307.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-07-27T03:17:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727031700-6a66cdac59de8.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85307.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/85307.html\",\"name\":\"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-07-27T03:17:01+00:00\",\"dateModified\":\"2026-07-27T03:17:01+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/85307.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/85307.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/85307.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/85307.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f53\u4f60\u7684 AI \u5e94\u7528\u5f00\u59cb\u6709\u771f\u5b9e\u7528\u6237\u65f6&#xff0c;\"\u6a21\u578b\u56de\u590d\u4e86\u4ec0\u4e48\"\u5df2\u7ecf\u4e0d\u591f\u4e86\u2014\u2014\u4f60\u9700\u8981\u77e5\u9053\u8c01\u5728\u7528\u3001\u7528\u4e86\u591a\u5c11 Token\u3001\u82b1\u4e86\u591a\u5c11\u94b1\u3001\u54cd\u5e94\u6709\u591a\u5feb\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684 Demo \u9879\u76ee&#xff0c;\u5c55\u793a\u5982\u4f55\u7528 FastAPI DeepSeek WebSocket \u6784\u5efa\u4e00\u4e2a\u5e26\u5b9e\u65f6\u76d1\u63a7\u4eea\u8868\u76d8\u7684 AI \u5bf9\u8bdd\u670d\u52a1\u3002 \u4e00\u3001\u8fd9\u4e2a Demo \u505a\u4e86\u4ec0\u4e48 \u9879\u76ee\u63d0\u4f9b\u4e24\u4e2a\u670d\u52a1&#xff0c;\u8dd1\u5728\u540c\u4e00\u4e2a FastAPI \u5e94\u7528\u91cc&#xff1a; \u670d\u52a1 \u9762\u5411 \u529f\u80fd AI \u5bf9\u8bdd\u670d\u52a1 \u7ec8\u7aef\u7528\u6237","og_url":"https:\/\/www.wsisp.com\/helps\/85307.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-07-27T03:17:01+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/07\/20260727031700-6a66cdac59de8.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/85307.html","url":"https:\/\/www.wsisp.com\/helps\/85307.html","name":"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-07-27T03:17:01+00:00","dateModified":"2026-07-27T03:17:01+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/85307.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/85307.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/85307.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u96f6\u6784\u5efa AI \u5bf9\u8bdd\u76d1\u63a7\u4eea\u8868\u76d8\uff1aLangfuse + Langchain + DeepSeek + FastAPI + WebSocket \u5b9e\u65f6\u67b6\u6784\u5b9e\u8df5"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=85307"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/85307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/85305"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=85307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=85307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=85307"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=85307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}