{"id":28560,"date":"2025-03-14T00:04:27","date_gmt":"2025-03-13T16:04:27","guid":{"rendered":"https:\/\/www.aisharenet.com\/?p=28560"},"modified":"2025-03-14T00:04:27","modified_gmt":"2025-03-13T16:04:27","slug":"fastapi-bushu-ollam","status":"publish","type":"post","link":"https:\/\/www.kdjingpai.com\/de\/fastapi-bushu-ollam\/","title":{"rendered":"FastAPI \u90e8\u7f72 Ollama \u53ef\u89c6\u5316\u5bf9\u8bdd\u754c\u9762"},"content":{"rendered":"<h2>\u4e00\u3001\u76ee\u5f55\u7ed3\u6784<\/h2>\n<p>\u4ed3\u5e93 notebook \u7684 C6 \u6587\u4ef6\u5939\u4e0b\uff1a<\/p>\n<pre><code>fastapi_chat_app\/\r\n\u2502\r\n\u251c\u2500\u2500 app.py\r\n\u251c\u2500\u2500 websocket_handler.py\r\n\u251c\u2500\u2500 static\/\r\n\u2502   \u2514\u2500\u2500 index.html\r\n\u2514\u2500\u2500 requirements.txt\r\n<\/code><\/pre>\n<ul>\n<li><code>app.py<\/code>\u00a0FastAPI \u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u8981\u8bbe\u7f6e\u548c\u8def\u7531\u3002<\/li>\n<li><code>websocket_handler.py<\/code>\u00a0\u5904\u7406 WebSocket \u8fde\u63a5\u548c\u6d88\u606f\u6d41\u3002<\/li>\n<li><code>static\/index.html<\/code>\u00a0HTML\u9875\u9762\u3002<\/li>\n<li><code>requirements.txt<\/code>\u00a0\u6240\u9700\u4f9d\u8d56\uff0c\u901a\u8fc7 pip install -r requirements.txt \u5b89\u88c5\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4e8c\u3001\u514b\u9686\u672c\u4ed3\u5e93<\/h2>\n<pre><code>git clone https:\/\/github.com\/AXYZdong\/handy-ollama\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u4e09\u3001\u5b89\u88c5\u4f9d\u8d56<\/h2>\n<pre><code>pip install -r requirements.txt\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u56db\u3001\u6838\u5fc3\u4ee3\u7801<\/h2>\n<p><code>app.py<\/code>\u00a0\u6587\u4ef6\u4e2d\u7684\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>import <a href=\"https:\/\/www.kdjingpai.com\/de\/ollama\/\">ollama<\/a>\r\nfrom fastapi import WebSocket\r\nasync def websocket_endpoint(websocket: WebSocket):\r\nawait websocket.accept()  # \u63a5\u53d7WebSocket\u8fde\u63a5\r\nuser_input = await websocket.receive_text()  # \u63a5\u6536\u7528\u6237\u8f93\u5165\u7684\u6587\u672c\u6d88\u606f\r\nstream = ollama.chat(  # \u4f7f\u7528ollama\u5e93\u4e0e\u6307\u5b9a\u6a21\u578b\u8fdb\u884c\u5bf9\u8bdd\r\nmodel='llama3.1',  # \u6307\u5b9a\u4f7f\u7528\u7684\u6a21\u578b\u4e3allama3.1\r\nmessages=[{'role': 'user', 'content': user_input}],  # \u4f20\u5165\u7528\u6237\u7684\u8f93\u5165\u6d88\u606f\r\nstream=True  # \u542f\u7528\u6d41\u5f0f\u4f20\u8f93\r\n)\r\ntry:\r\nfor chunk in stream:  # \u904d\u5386\u6d41\u5f0f\u4f20\u8f93\u7684\u7ed3\u679c\r\nmodel_output = chunk['message']['content']  # \u83b7\u53d6\u6a21\u578b\u8f93\u51fa\u7684\u5185\u5bb9\r\nawait websocket.send_text(model_output)  # \u901a\u8fc7WebSocket\u53d1\u9001\u6a21\u578b\u8f93\u51fa\u7684\u5185\u5bb9\r\nexcept Exception as e:  # \u6355\u83b7\u5f02\u5e38\r\nawait websocket.send_text(f\"Error: {e}\")  # \u901a\u8fc7WebSocket\u53d1\u9001\u9519\u8bef\u4fe1\u606f\r\nfinally:\r\nawait websocket.close()  # \u5173\u95edWebSocket\u8fde\u63a5\r\n<\/code><\/pre>\n<p><strong>\u63a5\u53d7 WebSocket \u8fde\u63a5\uff1a<\/strong><\/p>\n<ul>\n<li><code>await websocket.accept()<\/code>\uff1a\u9996\u5148\uff0c\u51fd\u6570\u63a5\u53d7\u6765\u81ea\u5ba2\u6237\u7aef\u7684 WebSocket \u8fde\u63a5\u8bf7\u6c42\uff0c\u5efa\u7acb\u4e0e\u5ba2\u6237\u7aef\u7684\u901a\u4fe1\u901a\u9053\u3002<\/li>\n<\/ul>\n<p><strong>\u63a5\u6536\u7528\u6237\u8f93\u5165\uff1a<\/strong><\/p>\n<ul>\n<li><code>user_input = await websocket.receive_text()<\/code>\uff1a\u901a\u8fc7 WebSocket \u4ece\u5ba2\u6237\u7aef\u63a5\u6536\u4e00\u6761\u6587\u672c\u6d88\u606f\uff0c\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<p><strong>\u521d\u59cb\u5316\u5bf9\u8bdd\u6d41\uff1a<\/strong><\/p>\n<ul>\n<li><code>stream = ollama.chat(...)<\/code>\uff1a\u8c03\u7528 ollama \u5e93\u4e2d\u7684 chat \u65b9\u6cd5\uff0c\u6307\u5b9a\u4f7f\u7528\u7684\u6a21\u578b\u4e3a llama3.1\u3002\u5c06\u7528\u6237\u7684\u8f93\u5165\u4f5c\u4e3a\u6d88\u606f\u4f20\u9012\u7ed9\u6a21\u578b\uff0c\u5e76\u542f\u7528\u6d41\u5f0f\u4f20\u8f93\uff08stream=True\uff09\uff0c\u4ee5\u4fbf\u9010\u6b65\u83b7\u53d6\u6a21\u578b\u751f\u6210\u7684\u56de\u590d\u3002<\/li>\n<\/ul>\n<p><strong>\u5904\u7406\u6a21\u578b\u8f93\u51fa\uff1a<\/strong><\/p>\n<ul>\n<li><code>for chunk in stream<\/code>\uff1a\u904d\u5386\u4ece\u6a21\u578b\u4e2d\u6d41\u5f0f\u4f20\u8f93\u8fc7\u6765\u7684\u6570\u636e\u5757\u3002<\/li>\n<li><code>model_output = chunk['message']['content']<\/code>\uff1a\u4ece\u6bcf\u4e2a\u6570\u636e\u5757\u4e2d\u63d0\u53d6\u51fa\u6a21\u578b\u751f\u6210\u7684\u6587\u672c\u5185\u5bb9\u3002<\/li>\n<li><code>await websocket.send_text(model_output)<\/code>\uff1a\u901a\u8fc7 WebSocket \u5c06\u63d0\u53d6\u51fa\u7684\u6a21\u578b\u56de\u590d\u53d1\u9001\u56de\u5ba2\u6237\u7aef\uff0c\u5b9e\u73b0\u5b9e\u65f6\u5bf9\u8bdd\u3002<\/li>\n<\/ul>\n<p><strong>\u5f02\u5e38\u5904\u7406\uff1a<\/strong><\/p>\n<ul>\n<li><code>except Exception as e<\/code>\uff1a\u5982\u679c\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u4efb\u4f55\u5f02\u5e38\uff08\u4f8b\u5982\uff0c\u7f51\u7edc\u95ee\u9898\u3001\u6a21\u578b\u9519\u8bef\u7b49\uff09\uff0c\u6355\u83b7\u5f02\u5e38\u5e76\u901a\u8fc7 WebSocket \u53d1\u9001\u4e00\u6761\u9519\u8bef\u4fe1\u606f\uff0c\u544a\u77e5\u5ba2\u6237\u7aef\u53d1\u751f\u4e86\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u5173\u95ed WebSocket \u8fde\u63a5\uff1a<\/strong><\/p>\n<ul>\n<li><code>finally<\/code>\uff1a\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u6700\u7ec8\u90fd\u786e\u4fdd\u5173\u95ed WebSocket \u8fde\u63a5\uff0c\u4ee5\u91ca\u653e\u8d44\u6e90\u5e76\u7ed3\u675f\u4f1a\u8bdd\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>\u4e94\u3001\u8fd0\u884capp<\/h2>\n<ol>\n<li>\u5728\u76ee\u5f55\u4e0b (<code>fastapi_chat_app\/<\/code>)\uff1b<\/li>\n<li>\u8fd0\u884c app.py \u6587\u4ef6\u3002<\/li>\n<\/ol>\n<pre><code>uvicorn app:app --reload\r\n<\/code><\/pre>\n<p>\u6253\u5f00\u9875\u9762\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28562\" title=\"FastAPI \u90e8\u7f72 Ollama \u53ef\u89c6\u5316\u5bf9\u8bdd\u754c\u9762-1\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/0ad602ccc534392.png\" alt=\"FastAPI \u90e8\u7f72 Ollama \u53ef\u89c6\u5316\u5bf9\u8bdd\u754c\u9762-1\" width=\"1280\" height=\"700\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/0ad602ccc534392.png 1280w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/0ad602ccc534392-768x420.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/0ad602ccc534392-18x10.png 18w\" sizes=\"auto, (max-width: 1280px) 100vw, 1280px\" \/><\/p>\n<p>\u540e\u53f0\u663e\u793a\u7684\u6b63\u5e38\u8f93\u51fa\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/datawhalechina\/handy-ollama\/blob\/main\/docs\/images\/C6-1-2.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-28561\" title=\"FastAPI \u90e8\u7f72 Ollama \u53ef\u89c6\u5316\u5bf9\u8bdd\u754c\u9762-2\" src=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/ea7c7364efdcd89.png\" alt=\"FastAPI \u90e8\u7f72 Ollama \u53ef\u89c6\u5316\u5bf9\u8bdd\u754c\u9762-2\" width=\"1096\" height=\"232\" srcset=\"https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/ea7c7364efdcd89.png 1096w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/ea7c7364efdcd89-768x163.png 768w, https:\/\/www.kdjingpai.com\/wp-content\/uploads\/2025\/03\/ea7c7364efdcd89-18x4.png 18w\" sizes=\"auto, (max-width: 1096px) 100vw, 1096px\" \/> <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u76ee\u5f55\u7ed3\u6784 \u4ed3\u5e93 notebook \u7684 C6 \u6587\u4ef6\u5939\u4e0b\uff1a fastapi_chat_app\/ \u2502 \u251c\u2500\u2500 app.py \u251c\u2500\u2500 websocket_handler.py \u251c\u2500\u2500 static\/ \u2502 \u2514\u2500\u2500 index.html \u2514\u2500\u2500 r&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182],"tags":[],"class_list":["post-28560","post","type-post","status-publish","format-standard","hentry","category-shicao"],"_links":{"self":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/28560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/comments?post=28560"}],"version-history":[{"count":0,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/posts\/28560\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/media?parent=28560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/categories?post=28560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kdjingpai.com\/de\/wp-json\/wp\/v2\/tags?post=28560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}