{"id":22,"date":"2026-11-08T13:15:55","date_gmt":"2026-11-08T13:15:55","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=22"},"modified":"2026-11-08T13:15:55","modified_gmt":"2026-11-08T13:15:55","slug":"%e7%94%a8-openai-%e5%85%bc%e5%ae%b9%e6%8e%a5%e5%8f%a3%e8%b0%83%e7%94%a8-deepseek-%e9%80%9a%e4%b9%89%e7%ad%89%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=22","title":{"rendered":"\u7528 OpenAI \u517c\u5bb9\u63a5\u53e3\u8c03\u7528 DeepSeek \/ \u901a\u4e49\u7b49\u6a21\u578b"},"content":{"rendered":"<p>\u73b0\u5728\u5f88\u591a\u56fd\u4ea7\u6a21\u578b\u90fd\u63d0\u4f9b<strong>OpenAI \u517c\u5bb9<\/strong>\u7684 Chat Completions \u63a5\u53e3\u3002\u4e00\u5957\u4ee3\u7801\uff0c\u6362 base_url \u548c model \u5c31\u80fd\u5207\u6362\u4f9b\u5e94\u5546\u3002<\/p>\n<h2>\u901a\u7528\u8c03\u7528\u6a21\u677f<\/h2>\n<pre><code>import os\nimport requests\n\nAPI_URL = os.getenv(\"LLM_API_URL\", \"https:\/\/api.deepseek.com\/chat\/completions\")\nAPI_KEY = os.getenv(\"LLM_API_KEY\")\nMODEL = os.getenv(\"LLM_MODEL\", \"deepseek-chat\")\n\ndef chat(messages, temperature=0.2):\n    resp = requests.post(\n        API_URL,\n        headers={\n            \"Authorization\": f\"Bearer {API_KEY}\",\n            \"Content-Type\": \"application\/json\",\n        },\n        json={\n            \"model\": MODEL,\n            \"messages\": messages,\n            \"temperature\": temperature,\n        },\n        timeout=60,\n    )\n    resp.raise_for_status()\n    return resp.json()[\"choices\"][0][\"message\"][\"content\"]\n\nanswer = chat([\n    {\"role\": \"system\", \"content\": \"\u4f60\u662f\u7b80\u6d01\u7684\u4e2d\u6587\u6280\u672f\u52a9\u6559\"},\n    {\"role\": \"user\", \"content\": \"\u7528\u4e09\u53e5\u8bdd\u89e3\u91ca\u4ec0\u4e48\u662f\u5411\u91cf\u6570\u636e\u5e93\"},\n])\nprint(answer)\n<\/code><\/pre>\n<h2>\u5de5\u7a0b\u6ce8\u610f\u70b9<\/h2>\n<ul>\n<li>\u5bc6\u94a5\u53ea\u653e\u73af\u5883\u53d8\u91cf \/ \u5bc6\u94a5\u7ba1\u7406\uff0c\u4e0d\u8fdb Git<\/li>\n<li>\u8bbe\u7f6e\u8d85\u65f6\u3001\u91cd\u8bd5\u3001\u9650\u6d41<\/li>\n<li>\u8bb0\u5f55 token \u6d88\u8017\uff0c\u65b9\u4fbf\u6210\u672c\u6838\u7b97<\/li>\n<li>\u5bf9\u7528\u6237\u8f93\u5165\u505a\u957f\u5ea6\u9650\u5236\uff0c\u9632\u8d85\u4e0a\u4e0b\u6587<\/li>\n<\/ul>\n<h2>\u6d41\u5f0f\u8f93\u51fa\uff08SSE\uff09\u573a\u666f<\/h2>\n<p>\u804a\u5929\u4ea7\u54c1\u5e38\u7528 stream \u6a21\u5f0f\u8fb9\u751f\u6210\u8fb9\u5c55\u793a\u3002\u5148\u628a\u975e\u6d41\u5f0f\u8dd1\u7a33\uff0c\u518d\u52a0\u6d41\u5f0f\uff0c\u6392\u9519\u6210\u672c\u66f4\u4f4e\u3002<\/p>\n<h2>\u5207\u6362\u6a21\u578b\u65f6\u6d4b\u4ec0\u4e48<\/h2>\n<ol>\n<li>\u540c\u6837 Prompt \u7684\u683c\u5f0f\u9075\u5b88\u7387<\/li>\n<li>\u4e2d\u6587\u672f\u8bed\u51c6\u786e\u6027<\/li>\n<li>\u957f\u4e0a\u4e0b\u6587\u662f\u5426\u622a\u65ad<\/li>\n<li>\u4ef7\u683c\u4e0e\u5ef6\u8fdf<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u5728\u5f88\u591a\u56fd\u4ea7\u6a21\u578b\u90fd\u63d0\u4f9bOpenAI \u517c\u5bb9\u7684 Chat Completions \u63a5\u53e3\u3002\u4e00\u5957\u4ee3\u7801\uff0c\u6362 base_u [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-llm"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}