{"id":1444,"date":"2026-01-27T13:24:45","date_gmt":"2026-01-27T05:24:45","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=1444"},"modified":"2026-01-27T13:38:27","modified_gmt":"2026-01-27T05:38:27","slug":"openai%e5%ba%93%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=1444","title":{"rendered":"openai\u5e93\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">1. \u521b\u5efa OpenAI Client \u5bf9\u8c61\u53ca\u53c2\u6570\u8bf4\u660e<\/h1>\n\n\n\n<p>OpenAI \u5b98\u65b9 SDK \u91c7\u7528 <strong>Client \u5bf9\u8c61<\/strong> \u4f5c\u4e3a\u6240\u6709 API \u8c03\u7528\u7684\u5165\u53e3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 \u57fa\u672c\u7528\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from openai import OpenAI\n\nclient = OpenAI()\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0cSDK \u4f1a\u81ea\u52a8\u4ece\u73af\u5883\u53d8\u91cf\u4e2d\u8bfb\u53d6 <code>OPENAI_API_KEY<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 \u663e\u5f0f\u4f20\u53c2\u65b9\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>client = OpenAI(\n    api_key=\"sk-xxx\",\n    base_url=\"https:\/\/api.openai.com\/v1\",\n    timeout=30\n)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u6570\u8bf4\u660e<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570\u540d<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>api_key<\/code><\/td><td>OpenAI API Key\uff0c\u7b49\u540c\u4e8e\u9274\u6743\u51ed\u8bc1<\/td><\/tr><tr><td><code>base_url<\/code><\/td><td>API \u670d\u52a1\u5730\u5740\uff0c\u9ed8\u8ba4\u5b98\u65b9\u5730\u5740\uff0c\u79c1\u6709\u5316\u6216\u4ee3\u7406\u65f6\u53ef\u4fee\u6539<\/td><\/tr><tr><td><code>timeout<\/code><\/td><td>\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff09<\/td><\/tr><tr><td><code>default_headers<\/code><\/td><td>\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\uff08\u5982\u7070\u5ea6\u6807\u8bc6\u3001\u5ba1\u8ba1\u5b57\u6bb5\uff09<\/td><\/tr><tr><td><code>max_retries<\/code><\/td><td>\u8bf7\u6c42\u5931\u8d25\u81ea\u52a8\u91cd\u8bd5\u6b21\u6570<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728\u4f01\u4e1a\u6216\u4e2d\u53f0\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u4f1a<strong>\u5c01\u88c5 Client \u4e3a\u5355\u4f8b Bean<\/strong>\uff0c\u7edf\u4e00\u7ba1\u7406\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2. chat.completions.create \u8c03\u7528\u53ca\u53c2\u6570\u8bf4\u660e<\/h1>\n\n\n\n<p>\u5bf9\u8bdd\u6a21\u578b\u901a\u8fc7 <code>client.chat.completions.create<\/code> \u8c03\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 \u57fa\u7840\u8c03\u7528\u793a\u4f8b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>response = client.chat.completions.create(\n    model=\"gpt-4o-mini\",\n    messages=&#91;\n        {\"role\": \"system\", \"content\": \"\u4f60\u662f\u4e00\u4e2a\u540e\u7aef\u5f00\u53d1\u52a9\u624b\"},\n        {\"role\": \"user\", \"content\": \"\u89e3\u91ca\u4e00\u4e0b\u4ec0\u4e48\u662f\u5e42\u7b49\u6027\"}\n    ],\n    temperature=0.7,\n    max_tokens=512\n)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 \u6838\u5fc3\u53c2\u6570\u8be6\u89e3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u53c2\u6570<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>model<\/code><\/td><td>\u6307\u5b9a\u4f7f\u7528\u7684\u6a21\u578b<\/td><\/tr><tr><td><code>messages<\/code><\/td><td>\u5bf9\u8bdd\u4e0a\u4e0b\u6587\u6570\u7ec4<\/td><\/tr><tr><td><code>temperature<\/code><\/td><td>\u968f\u673a\u6027\u63a7\u5236\uff0c\u503c\u8d8a\u5927\u56de\u7b54\u8d8a\u53d1\u6563<\/td><\/tr><tr><td><code>max_tokens<\/code><\/td><td>\u672c\u6b21\u56de\u590d\u6700\u5927 token \u6570<\/td><\/tr><tr><td><code>top_p<\/code><\/td><td>nucleus sampling\uff0c\u901a\u5e38\u4e0e temperature \u4e8c\u9009\u4e00<\/td><\/tr><tr><td><code>stream<\/code><\/td><td>\u662f\u5426\u542f\u7528\u6d41\u5f0f\u8fd4\u56de<\/td><\/tr><tr><td><code>stop<\/code><\/td><td>\u751f\u6210\u7ec8\u6b62\u7b26<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.3 \u5e38\u7528\u89d2\u8272\uff08role\uff09\u8bf4\u660e<\/h2>\n\n\n\n<p><code>messages<\/code> \u662f\u4e00\u4e2a <strong>\u6709\u5e8f\u6570\u7ec4<\/strong>\uff0c\u6a21\u578b\u4f1a\u4e25\u683c\u6309\u987a\u5e8f\u7406\u89e3\u4e0a\u4e0b\u6587\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"role\": \"user\",\n  \"content\": \"xxx\"\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u89d2\u8272\u7c7b\u578b<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>role<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td><code>system<\/code><\/td><td>\u7cfb\u7edf\u7ea7\u6307\u4ee4\uff0c\u5b9a\u4e49\u6a21\u578b\u8eab\u4efd\u3001\u884c\u4e3a\u8fb9\u754c<\/td><\/tr><tr><td><code>user<\/code><\/td><td>\u7528\u6237\u8f93\u5165<\/td><\/tr><tr><td><code>assistant<\/code><\/td><td>\u6a21\u578b\u5386\u53f2\u56de\u590d\uff08\u7528\u4e8e\u591a\u8f6e\u5bf9\u8bdd\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u591a\u8f6e\u5bf9\u8bdd\u4e0a\u4e0b\u6587<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>messages = &#91;\n    {\"role\": \"system\", \"content\": \"\u4f60\u662fJava\u540e\u7aef\u9762\u8bd5\u5b98\"},\n    {\"role\": \"user\", \"content\": \"\u4ec0\u4e48\u662f\u7ebf\u7a0b\u6c60\uff1f\"},\n    {\"role\": \"assistant\", \"content\": \"\u7ebf\u7a0b\u6c60\u7528\u4e8e\u590d\u7528\u7ebf\u7a0b...\"},\n    {\"role\": \"user\", \"content\": \"\u8bf4\u4e00\u4e0b\u6838\u5fc3\u53c2\u6570\"}\n]\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5173\u952e\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>system<\/code> \u5e76\u975e\u5fc5\u987b\uff0c\u4f46\u5f3a\u70c8\u63a8\u8350<\/li>\n\n\n\n<li>\u6a21\u578b\u4e0d\u4f1a\u201c\u8bb0\u4f4f\u5386\u53f2\u201d\uff0c\u4e0a\u4e0b\u6587\u5b8c\u5168\u7531 messages \u51b3\u5b9a<\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2.4 Response \u5bf9\u8c61\u5b8c\u6574\u7ed3\u6784\u8bf4\u660e<\/h2>\n\n\n\n<p><code>chat.completions.create<\/code> \u8fd4\u56de\u7684\u662f\u4e00\u4e2a <strong>\u7ed3\u6784\u5316\u54cd\u5e94\u5bf9\u8c61<\/strong>\uff0c\u800c\u975e\u7eaf\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.4.1 Response \u793a\u4f8b\u7ed3\u6784\uff08\u7b80\u5316\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"id\": \"chatcmpl-xxx\",\n  \"object\": \"chat.completion\",\n  \"created\": 1700000000,\n  \"model\": \"gpt-4o-mini\",\n  \"choices\": &#91;\n    {\n      \"index\": 0,\n      \"message\": {\n        \"role\": \"assistant\",\n        \"content\": \"\u5e42\u7b49\u6027\u662f\u6307\u591a\u6b21\u8bf7\u6c42\u4ea7\u751f\u76f8\u540c\u7ed3\u679c\"\n      },\n      \"finish_reason\": \"stop\"\n    }\n  ],\n  \"usage\": {\n    \"prompt_tokens\": 32,\n    \"completion_tokens\": 18,\n    \"total_tokens\": 50\n  }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2.4.2 choices \u5b57\u6bb5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>response.choices&#91;0]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>index<\/code><\/td><td>\u5f53\u524d\u5019\u9009\u7ed3\u679c\u7d22\u5f15<\/td><\/tr><tr><td><code>message<\/code><\/td><td>\u6a21\u578b\u751f\u6210\u7684\u6d88\u606f<\/td><\/tr><tr><td><code>finish_reason<\/code><\/td><td>\u751f\u6210\u7ed3\u675f\u539f\u56e0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">finish_reason \u5e38\u89c1\u503c<\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u503c<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td><code>stop<\/code><\/td><td>\u6b63\u5e38\u751f\u6210\u5b8c\u6210<\/td><\/tr><tr><td><code>length<\/code><\/td><td>\u8fbe\u5230 max_tokens<\/td><\/tr><tr><td><code>content_filter<\/code><\/td><td>\u5185\u5bb9\u88ab\u8fc7\u6ee4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2.4.3 message \u5bf9\u8c61<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>response.choices&#91;0].message\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>role<\/code><\/td><td>\u56fa\u5b9a\u4e3a assistant<\/td><\/tr><tr><td><code>content<\/code><\/td><td>\u6a21\u578b\u8f93\u51fa\u5185\u5bb9<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u83b7\u53d6\u6700\u7ec8\u6587\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = response.choices&#91;0].message.content\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2.4.4 usage \u5b57\u6bb5\uff08Token \u7edf\u8ba1\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>response.usage\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5b57\u6bb5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>prompt_tokens<\/code><\/td><td>\u8f93\u5165\u6d88\u8017<\/td><\/tr><tr><td><code>completion_tokens<\/code><\/td><td>\u8f93\u51fa\u6d88\u8017<\/td><\/tr><tr><td><code>total_tokens<\/code><\/td><td>\u603b\u6d88\u8017<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728<strong>\u8ba1\u8d39\u3001\u9650\u6d41\u3001\u4e0a\u4e0b\u6587\u88c1\u526a<\/strong>\u573a\u666f\u4e2d\u975e\u5e38\u5173\u952e\u3002<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\">3 \u4e24\u4e2a\u793a\u4f8b\uff1a<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 \uff1a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nfrom openai import OpenAI\nfrom dotenv import load_dotenv\n\n# \u52a0\u8f7d\u73af\u5883\u53d8\u91cf\nload_dotenv()\n\n# \u521b\u5efa\u5ba2\u6237\u7aef\nclient = OpenAI(\n    api_key=os.getenv('OPENAI_API_KEY'),\n    base_url='https:\/\/api-inference.modelscope.cn\/v1'\n)\n\n\n# \u8c03\u7528API - \u6dfb\u52a0 enable_thinking \u53c2\u6570\nresponse = client.chat.completions.create(\n    model='Qwen\/Qwen3-8B',\n    messages=&#91;\n        {\"role\": \"system\", \"content\": \"\u4f60\u662fAI\u52a9\u7406\uff0c\u7b80\u8981\u56de\u7b54\u4ee5\u4e0b\u95ee\u9898\"},\n        {\"role\": \"user\", \"content\": \"\u5c0f\u660e\u670918\u6761\u72d7\"},\n        {\"role\": \"assistant\", \"content\": \"\u597d\u7684\"},\n        {\"role\": \"user\", \"content\": \"\u5c0f\u660e\u670917\u6761\u732b\"},\n        {\"role\": \"assistant\", \"content\": \"\u597d\u7684\"},\n        {\"role\": \"user\", \"content\": \"\u5c0f\u660e\u6709\u591a\u5c11\u6761\u5ba0\u7269\uff1f\"}\n    ],\n    stream=False,\n    extra_body={  # ModelScope \u7279\u6709\u7684\u53c2\u6570\n        \"enable_thinking\": False\n    }\n)\n\n# \u8f93\u51fa\u7ed3\u679c\nprint(\"\u56de\u7b54:\", response.choices&#91;0].message.content)\nprint(\"\\n\u4f7f\u7528\u91cf\u7edf\u8ba1:\")\nprint(f\"\u603btoken\u6570: {response.usage.total_tokens}\")\nprint(f\"\u63d0\u793atoken\u6570: {response.usage.prompt_tokens}\")\nprint(f\"\u5b8c\u6210token\u6570: {response.usage.completion_tokens}\")\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 \u591a\u8f6e\u5bf9\u8bdd<\/h2>\n\n\n\n<p>\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u4f7f\u7528 LangChain \u7684 Memory \u6a21\u5757\uff08\u5982 ConversationBufferMemory\uff09\u6765\u667a\u80fd\u7ba1\u7406\u4e0a\u4e0b\u6587\uff0c\u800c\u975e\u624b\u52a8\u62fc\u63a5\u6d88\u606f\u5217\u8868\u3002\u5b83\u80fd\u81ea\u52a8\u5904\u7406 token \u622a\u65ad\u3001\u5bf9\u8bdd\u6458\u8981\u548c\u6301\u4e45\u5316\u5b58\u50a8\uff0c\u907f\u514d\u4e0a\u4e0b\u6587\u7206\u70b8\u548c\u6548\u7387\u95ee\u9898\uff0c\u662f\u751f\u4ea7\u73af\u5883\u7684\u6807\u51c6\u505a\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nfrom openai import OpenAI\nfrom dotenv import load_dotenv\n\n# \u52a0\u8f7d\u73af\u5883\u53d8\u91cf\nload_dotenv()\n\n# \u521b\u5efa\u5ba2\u6237\u7aef\nclient = OpenAI(\n    api_key=os.getenv('OPENAI_API_KEY'),\n    base_url='https:\/\/api-inference.modelscope.cn\/v1'\n)\n\nmy_message = &#91;\n    {\"role\": \"system\", \"content\": \"\u4f60\u662f\u4e00\u4e2aJava\u540e\u7aef\u9762\u8bd5\u5b98\"}\n]\n\nwhile True:\n    user_input = input(\"\u6211\uff1a\")\n    my_message.append({\"role\": \"user\", \"content\": user_input})\n\n    response = client.chat.completions.create(\n        model=\"Qwen\/Qwen3-8B\",\n        messages=my_message,\n        stream=False,\n        extra_body={  # ModelScope \u7279\u6709\u7684\u53c2\u6570\n            \"enable_thinking\": False\n        }\n    )\n\n    reply = response.choices&#91;0].message.content\n    print(\"AI\uff1a\", reply)\n    print(\"token\uff1a\",response.usage)\n\n    my_message.append({\"role\": \"assistant\", \"content\": reply})\n\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u521b\u5efa OpenAI Client \u5bf9\u8c61\u53ca\u53c2\u6570\u8bf4\u660e OpenAI \u5b98\u65b9 SDK \u91c7\u7528 Client \u5bf9\u8c61  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"class_list":["post-1444","post","type-post","status-publish","format-standard","hentry","category-82"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1444"}],"version-history":[{"count":3,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1444\/revisions"}],"predecessor-version":[{"id":1451,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/1444\/revisions\/1451"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1444"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}