{"id":1293,"date":"2023-03-21T15:07:25","date_gmt":"2023-03-21T07:07:25","guid":{"rendered":"http:\/\/qssjx.com\/?p=1293"},"modified":"2023-03-21T15:07:25","modified_gmt":"2023-03-21T07:07:25","slug":"%e4%bd%bf%e7%94%a8-openai-chatgpt","status":"publish","type":"post","link":"https:\/\/qssjx.com\/?p=1293","title":{"rendered":"\u4f7f\u7528 OpenAI ChatGPT"},"content":{"rendered":"<h2 id=\"a1\">\u8a3b\u518a OpenAI<\/h2>\n<p>\u524d\u5f80 OpenAI \u7684\u7db2\u7ad9\u3002<\/p>\n<blockquote>\n<ul>\n<li>OpenAI \u7db2\u7ad9\uff1a<a title=\"https:\/\/openai.com\/\" href=\"https:\/\/openai.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/openai.com\/<\/a><\/li>\n<li>\u9ad4\u9a57 ChatGPT\uff1a<a title=\"https:\/\/chat.openai.com\/\" href=\"https:\/\/chat.openai.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/chat.openai.com\/<\/a><\/li>\n<li>OpenAI \u958b\u767c\u8005\u5e73\u53f0\uff1a<a title=\"https:\/\/platform.openai.com\/\" href=\"https:\/\/platform.openai.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/platform.openai.com\/<\/a><\/li>\n<\/ul>\n<\/blockquote>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-01.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u524d\u5f80 OpenAI\" \/><\/p>\n<p>\u9ede\u64ca\u6700\u4e0a\u65b9\u7684\u300cTry\u300d\uff0c\u6703\u958b\u555f\u8a3b\u518a\u6216\u767b\u5165\u7684\u9801\u9762 ( \u9ede\u64ca\u300cAPI\u300d\u6309\u9215\uff0c\u4e5f\u6703\u958b\u555f\u8a3b\u518a\u6216\u767b\u5165\u7684\u9801\u9762 )\uff0c\u9ede\u64ca Sign up \u6309\u9215\u9032\u884c\u8a3b\u518a\uff0c\u8a3b\u518a\u6642\u9700\u8981\u4f7f\u7528\u53ef\u4ee5\u63a5\u6536\u7c21\u8a0a\u8a8d\u8b49\u78bc\u7684\u624b\u6a5f\u96fb\u8a71\u865f\u78bc\u3002<\/p>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-02.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u8a3b\u518a OpenAI\" \/><\/p>\n<h2 id=\"a2\">\u900f\u904e ChatGPT \u548c AI \u804a\u5929<\/h2>\n<p>\u8a3b\u518a OpenAI \u4e26\u767b\u5165\u5b8c\u6210\u5f8c\uff0c\u5728\u9801\u9762\u7684\u4e0b\u65b9\u6703\u51fa\u73fe\u80fd\u300c\u8f38\u5165\u6587\u5b57\u300d\u7684\u6b04\u4f4d\u3002<\/p>\n<blockquote><p>\u9ad4\u9a57 ChatGPT\uff1a<a title=\"https:\/\/chat.openai.com\/\" href=\"https:\/\/chat.openai.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/chat.openai.com\/<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-03.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u8f38\u5165\u6587\u5b57\" \/><\/p>\n<p>\u8f38\u5165\u6587\u5b57\u5f8c\u6309\u4e0b\u9001\u51fa\uff0c\u5c31\u80fd\u958b\u59cb\u8207 OpenAI \u7684 AI \u6a5f\u5668\u4eba\u804a\u5929\u3002<\/p>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-04.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u900f\u904e ChatGPT \u548c AI \u804a\u5929\" \/><\/p>\n<p>\u56e0\u70ba\u6bcf\u500b\u8a3b\u518a\u7684\u5e33\u865f\u90fd\u53ef\u4ee5\u548c AI \u6a5f\u5668\u4eba\u804a\u5929\uff0c\u4e5f\u80fd\u5920\u900f\u904e OpenAI \u7684 API \u547c\u53eb\u6a5f\u5668\u4eba\uff0c\u56e0\u6b64\u6709\u6642\u5019\u6703\u9047\u5230 OpenAI \u4f3a\u670d\u5668\u7121\u6cd5\u56de\u61c9\u7684\u72c0\u6cc1\uff0c\u9019\u6642\u5019\u53ea\u80fd\u50bb\u50bb\u7684\u7b49\u5f85\uff0c\u6216\u5728\u6bd4\u8f03\u4e0d\u71b1\u9580\u7684\u6642\u6bb5\u548c AI \u6a5f\u5668\u4eba\u804a\u5929\u3002<\/p>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-05.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u5728\u6bd4\u8f03\u4e0d\u71b1\u9580\u7684\u6642\u6bb5\u548c AI \u6a5f\u5668\u4eba\u804a\u5929\" \/><\/p>\n<h2 id=\"quota\">OpenAI \u7684\u4f7f\u7528\u984d\u5ea6<\/h2>\n<p>\u76ee\u524d OpenAI \u63d0\u4f9b\u300c18 \u7f8e\u91d1\u6216\u4e09\u500b\u6708\u300d\u7684\u4f7f\u7528\u984d\u5ea6\uff0c\u6703\u6839\u64da\u4f7f\u7528\u7684\u5b57\u8a5e\u91cf\u591a\u5be1\uff0c\u6216\u547c\u53eb\u6b21\u6578&#8230;\u7b49\u4f5c\u696d\u9032\u884c\u8cbb\u7528\u7684\u6263\u9664\uff0c\u4f7f\u7528\u7bc4\u570d\u5305\u542b\u4e86\u5728\u7db2\u7ad9\u4e0a\u8207 AI \u4e92\u52d5\uff0c\u6216\u900f\u904e\u7a0b\u5f0f\u547c\u53eb API\uff0c\u7576\u5373\u5c07\u8d85\u904e\u8cbb\u7528\u6216\u662f\u5230\u9054\u671f\u9650\uff0c\u5c31\u6703\u5148\u66ab\u505c\u4f7f\u7528\u6b0a\u9650\u4e26\u8981\u6c42\u4ed8\u8cbb\uff0c\u521d\u671f\u9069\u5408\u5690\u9bae\u6216\u4f5c\u70ba\u958b\u767c\u8005\u4f7f\u7528 ( \u4e0b\u5716\u662f 2022 \u5e74 12 \u6708\u64b0\u5beb\u9019\u7bc7\u6587\u7ae0\u6642\u7684\u7528\u91cf )\u3002<\/p>\n<blockquote><p>OpenAI \u958b\u767c\u8005\u5e73\u53f0\uff1a<a title=\"https:\/\/platform.openai.com\/\" href=\"https:\/\/platform.openai.com\/\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/platform.openai.com\/<\/a><\/p><\/blockquote>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-10.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - OpenAI \u7684\u4f7f\u7528\u984d\u5ea6\" \/><\/p>\n<h2 id=\"a3\">\u5efa\u7acb OpenAI API Key<\/h2>\n<p>\u5982\u679c\u8981\u6bd4\u8f03\u6df1\u5165\u7684\u4f7f\u7528 OpenAI ( \u4f8b\u5982\u4f7f\u7528 Python \u4e32\u63a5\u3001\u7d50\u5408 LINE BOT&#8230;\u7b49 )\uff0c\u5c31\u5fc5\u9808\u8981\u900f\u904e OpenAI \u7684 API \u4f86\u5be6\u73fe\uff0c\u5f9e\u53f3\u4e0a\u65b9\u300cPersonal\u300d\u7684\u9078\u55ae\u88e1\uff0c\u9078\u64c7\u300cView API Keys\u300d\u3002<\/p>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-06.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - View API Keys\" \/><\/p>\n<p>\u9078\u64c7\u5f8c\u9032\u5165\u5efa\u7acb API Keys \u9801\u9762\uff0c\u9ede\u64ca\u300cCreate new secret key\u300d\u6309\u9215\uff0c\u5c31\u80fd\u5efa\u7acb API Keys\u3002<\/p>\n<blockquote><p><em>\u6ce8\u610f\uff01\u53ea\u6709\u5efa\u7acb\u5b8c\u6210\u7684\u7576\u4e0b\u53ef\u4ee5\u8907\u88fd API Keys\uff0c\u8acb\u4f7f\u7528\u8a18\u4e8b\u672c\u6216\u5176\u4ed6\u5de5\u5177\u5132\u5b58 API Key\uff0c\u5982\u679c\u8981\u522a\u9664 API Key\uff0c\u5fc5\u9808\u518d\u6b21\u5efa\u7acb\u65b0\u7684 API Key \u624d\u80fd\u522a\u9664\u820a\u7684<\/em>\u3002<\/p><\/blockquote>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-07.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u5efa\u7acb OpenAI API Key\" \/><\/p>\n<h2 id=\"a4\">\u4f7f\u7528 Python \u4e32\u63a5 OpenAI API<\/h2>\n<p>\u8f38\u5165\u4e0b\u5217\u6307\u4ee4\u5b89\u88dd openai \u51fd\u5f0f\u5eab ( \u672c\u6a5f\u74b0\u5883\u6839\u64da\u500b\u4eba\u72c0\u6cc1\u4f7f\u7528 pip \u6216 pip3 )\uff0c\u5982\u679c\u5b89\u88dd\u5931\u6557\uff0c\u53ef\u4ee5\u4f7f\u7528\u00a0<code>pip install --upgrade pip<\/code>\u00a0\u66f4\u65b0 pip \u5617\u8a66\u89e3\u6c7a\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;!pip install openai\r\n&lt;\/code&gt;<\/pre>\n<p>\u5b89\u88dd\u5f8c\uff0c\u4f7f\u7528\u4e0b\u5217\u7684\u7a0b\u5f0f\u78bc\uff0c\u5c31\u80fd\u5920\u5411 OpenAI \u7684 AI \u6a5f\u5668\u4eba\u767c\u9001\u5c0d\u8a71\u8a0a\u606f\uff0c\u4e26\u63a5\u6536 AI \u6a5f\u5668\u4eba\u6240\u56de\u50b3\u7684\u8a0a\u606f\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;\/span&gt; openai\r\nopenai.api_key = &lt;span class=&quot;hljs-string&quot;&gt;&#039;\u4f60\u7684 API Key&#039;&lt;\/span&gt;\r\n\r\nresponse = openai.Completion.create(\r\n    model=&lt;span class=&quot;hljs-string&quot;&gt;&quot;text-davinci-003&quot;&lt;\/span&gt;,\r\n    prompt=&lt;span class=&quot;hljs-string&quot;&gt;&quot;\u8b1b\u500b\u7b11\u8a71\u4f86\u807d\u807d&quot;&lt;\/span&gt;,\r\n    max_tokens=&lt;span class=&quot;hljs-number&quot;&gt;128&lt;\/span&gt;,\r\n    temperature=&lt;span class=&quot;hljs-number&quot;&gt;0.5&lt;\/span&gt;,\r\n)\r\n\r\ncompleted_text = response[&lt;span class=&quot;hljs-string&quot;&gt;&quot;choices&quot;&lt;\/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;][&lt;span class=&quot;hljs-string&quot;&gt;&quot;text&quot;&lt;\/span&gt;]\r\nprint(completed_text)\r\n&lt;\/code&gt;<\/pre>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-08.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u5efa\u7acb OpenAI API Key\" \/><\/p>\n<p>\u4e0a\u8ff0\u7684\u7a0b\u5f0f\u78bc\u4e2d\u4f7f\u7528\u4e86\u00a0<code>openai.Completion.create<\/code>\u00a0\u65b9\u6cd5\uff0c\u65b9\u6cd5\u7684\u53c3\u6578\u8aaa\u660e\u5982\u4e0b ( \u8a73\u7d30\u53c3\u6578\u53c3\u8003\uff1a<a title=\"Create completion\" href=\"https:\/\/beta.openai.com\/docs\/api-reference\/completions\/create\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >Create completion<\/a>\u00a0)\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u53c3\u6578<\/th>\n<th>\u8aaa\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>model<\/td>\n<td>AI \u6240\u4f7f\u7528\u7684\u5f15\u64ce\u6a21\u7d44\uff0c\u5982\u679c\u662f\u4ee5\u81ea\u7136\u8a9e\u610f\u70ba\u4e3b\uff0c\u9810\u8a2d\u4f7f\u7528 text-davinci-003 ( \u66f4\u591a\u5f15\u64ce\u53c3\u8003\uff1a<a title=\"https:\/\/beta.openai.com\/docs\/models\/gpt-3\" href=\"https:\/\/beta.openai.com\/docs\/models\/gpt-3\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >https:\/\/beta.openai.com\/docs\/models\/gpt-3<\/a>\u00a0)\u3002<\/td>\n<\/tr>\n<tr>\n<td>prompt<\/td>\n<td>\u8981\u50b3\u9001\u7684\u53e5\u5b50\u6703\u8a5e\u5f59\u3002<\/td>\n<\/tr>\n<tr>\n<td>max_tokens<\/td>\n<td>\u5e0c\u671b AI \u56de\u50b3\u7684\u6700\u5927\u5b57\u6578\uff0c\u9810\u8a2d 128\uff0c\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u56de\u50b3\u7684\u5b57\u6578\u6703\u5927\u65bc\u9019\u500b\u6578\u5b57\u3002<\/td>\n<\/tr>\n<tr>\n<td>temperature<\/td>\n<td>\u96a8\u6a5f\u6587\u5b57\u7d44\u5408\uff0c\u7bc4\u570d 0\uff5e1\uff0c\u9810\u8a2d 0.5\uff0c0 \u8868\u793a\u4e0d\u96a8\u6a5f\uff0c1 \u8868\u793a\u5b8c\u5168\u96a8\u6a5f\u3002<\/td>\n<\/tr>\n<tr>\n<td>language<\/td>\n<td>\u6a5f\u5668\u4eba\u8655\u7406\u7684\u300c\u7a0b\u5f0f\u8a9e\u8a00\u300d\uff0c\u9810\u8a2d python\uff0c\u53ef\u8a2d\u5b9a javascript\u3001java\u3001csharp\u3001golang\u3001ruby\u3001php\u3001cpp\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e5f\u56e0\u70ba OpenAI \u7684 AI \u6a5f\u5668\u4eba\u662f\u4e00\u500b\u5927\u578b\u8a9e\u8a00\u6a21\u578b\uff0c\u80fd\u5920\u88ab\u8a13\u7df4\u6210\u7406\u89e3\u548c\u56de\u8986\u591a\u7a2e\u8a9e\u8a00\uff0c\u56e0\u6b64<em>\u56de\u8986\u8a0a\u606f\u6240\u4f7f\u7528\u7684\u8a9e\u8a00\uff0c\u662f\u6839\u64da\u8f38\u5165\u7684\u6587\u5b57\u8a0a\u606f\u5167\u5bb9\u6c7a\u5b9a<\/em>\uff0c\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\u70ba\u4f8b\uff0c\u57f7\u884c\u5f8c\u8f38\u5165\u300c\u4e2d\u6587\u7b11\u8a71\u300d\uff0c\u56de\u8986\u7684\u8a0a\u606f\u5c31\u662f\u4e2d\u6587\u7b11\u8a71\uff0c\u800c\u5982\u679c\u662f\u8f38\u5165\u300cenglish joke\u300d\uff0c\u5c31\u6703\u56de\u61c9\u4e00\u500b\u82f1\u6587\u7684\u7b11\u8a71\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;\/span&gt; openai\r\nopenai.api_key = &lt;span class=&quot;hljs-string&quot;&gt;&#039;\u4f60\u7684 API Key&#039;&lt;\/span&gt;\r\n\r\n&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;True&lt;\/span&gt;:\r\n    msg = input()\r\n    response = openai.Completion.create(\r\n        model=&lt;span class=&quot;hljs-string&quot;&gt;&#039;text-davinci-003&#039;&lt;\/span&gt;,\r\n        prompt=msg,\r\n        max_tokens=&lt;span class=&quot;hljs-number&quot;&gt;128&lt;\/span&gt;,\r\n        temperature=&lt;span class=&quot;hljs-number&quot;&gt;0.5&lt;\/span&gt;\r\n    )\r\n\r\n    completed_text = response[&lt;span class=&quot;hljs-string&quot;&gt;&#039;choices&#039;&lt;\/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;][&lt;span class=&quot;hljs-string&quot;&gt;&#039;text&#039;&lt;\/span&gt;]\r\n    print(completed_text)\r\n&lt;\/code&gt;<\/pre>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-09.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u5efa\u7acb OpenAI API Key\" \/><\/p>\n<p>\u5982\u679c\u662f\u4f7f\u7528 gpt-3.5-turbo \u5f15\u64ce\u6a21\u7d44\uff0c<em>\u5247\u8981\u628a\u00a0<code>openai.ChatCompletion<\/code>\u00a0\u6539\u6210\u00a0<code>openai.ChatCompletion<\/code>\uff0c\u4e26\u5c07 prompt \u53c3\u6578\u6539\u6210 messages \u53c3\u6578\uff0c\u53c3\u6578\u5167\u5bb9\u4f7f\u7528\u4e32\u5217\u548c\u5b57\u5178\u7684\u683c\u5f0f<\/em>\uff0c\u900f\u904e\u5b57\u5178\u683c\u5f0f\u8868\u73fe\u4e0a\u4e0b\u6587\u8a9e\u53e5\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\uff0cChatGPT \u5c31\u6703\u77e5\u9053\u4f7f\u7528\u8005\u7684\u540d\u5b57\u662f oxxo\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;\/span&gt; openai\r\nopenai.api_key = &lt;span class=&quot;hljs-string&quot;&gt;&#039;\u4f60\u7684 API KEY&#039;&lt;\/span&gt;\r\n\r\nresponse = openai.ChatCompletion.create(\r\n  model=&lt;span class=&quot;hljs-string&quot;&gt;&quot;gpt-3.5-turbo&quot;&lt;\/span&gt;,\r\n  max_tokens=&lt;span class=&quot;hljs-number&quot;&gt;128&lt;\/span&gt;,\r\n  temperature=&lt;span class=&quot;hljs-number&quot;&gt;0.5&lt;\/span&gt;,\r\n  messages=[\r\n        {&lt;span class=&quot;hljs-string&quot;&gt;&quot;role&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;user&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;content&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;\u6211\u53eb\u505a oxxo&quot;&lt;\/span&gt;},\r\n        {&lt;span class=&quot;hljs-string&quot;&gt;&quot;role&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;assistant&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;content&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;\u539f\u4f86\u4f60\u662f oxxo \u5440&quot;&lt;\/span&gt;},\r\n        {&lt;span class=&quot;hljs-string&quot;&gt;&quot;role&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;user&quot;&lt;\/span&gt;, &lt;span class=&quot;hljs-string&quot;&gt;&quot;content&quot;&lt;\/span&gt;: &lt;span class=&quot;hljs-string&quot;&gt;&quot;\u8acb\u554f\u6211\u53eb\u4ec0\u9ebc\u540d\u5b57\uff1f&quot;&lt;\/span&gt;}\r\n    ]\r\n)\r\nprint(response.choices[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;].message.content)\r\n&lt;\/code&gt;<\/pre>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-11.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - gpt-3.5-turbo \u5f15\u64ce\u6a21\u7d44\" \/><\/p>\n<h2 id=\"a5\">\u4e32\u9023\u4e0a\u4e0b\u6587\u8a9e\u53e5<\/h2>\n<p>\u5982\u679c\u4f7f\u7528 text-davinci-003 \u6a21\u578b\uff0c\u5728\u50b3\u9001\u8a9e\u53e5\u6642\uff0c\u5c07\u4e4b\u524d\u5c0d\u8a71\u7684\u5167\u5bb9\uff0c\u4f7f\u7528\u300c\\n\\n\u300d\u5169\u500b\u63db\u884c\u7b26\u865f\u5206\u9694 ( \u5be6\u6e2c\u9019\u6a23\u5b50\u7684\u6e96\u78ba\u5ea6\u6700\u9ad8 )\uff0c\u4e26\u9023\u63a5\u76ee\u524d\u8981\u8b1b\u7684\u8a9e\u53e5\uff0c\u50b3\u9001\u51fa\u53bb\u4e4b\u5f8c\u5c31\u53ef\u4ee5\u8b93 ChatGPT \u4e86\u89e3\u904e\u53bb\u7684\u4ea4\u8ac7\u5167\u5bb9\uff0c\u5be6\u73fe\u4e32\u9023\u4e0a\u4e0b\u8a9e\u53e5\u7684\u529f\u80fd\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;\/span&gt; openai\r\nopenai.api_key = &lt;span class=&quot;hljs-string&quot;&gt;&#039;\u4f60\u7684 API Key&#039;&lt;\/span&gt;\r\n\r\nmessages = &lt;span class=&quot;hljs-string&quot;&gt;&#039;&#039;&lt;\/span&gt;\r\n&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;True&lt;\/span&gt;:\r\n    msg = input(&lt;span class=&quot;hljs-string&quot;&gt;&#039;me &gt; &#039;&lt;\/span&gt;)\r\n    messages = &lt;span class=&quot;hljs-string&quot;&gt;f&#039;&lt;span class=&quot;hljs-subst&quot;&gt;{messages}&lt;\/span&gt;&lt;span class=&quot;hljs-subst&quot;&gt;{msg}&lt;\/span&gt;\\n&#039;&lt;\/span&gt;   &lt;span class=&quot;hljs-comment&quot;&gt;# \u5c07\u904e\u53bb\u7684\u8a9e\u53e5\u9023\u63a5\u76ee\u524d\u7684\u5c0d\u8a71\uff0c\u5f8c\u65b9\u52a0\u4e0a \\n \u53ef\u4ee5\u907f\u514d\u6a19\u9ede\u7b26\u865f\u7d50\u5c3e\u554f\u984c&lt;\/span&gt;\r\n    response = openai.Completion.create(\r\n        model=&lt;span class=&quot;hljs-string&quot;&gt;&#039;text-davinci-003&#039;&lt;\/span&gt;,\r\n        prompt=messages,\r\n        max_tokens=&lt;span class=&quot;hljs-number&quot;&gt;128&lt;\/span&gt;,\r\n        temperature=&lt;span class=&quot;hljs-number&quot;&gt;0.5&lt;\/span&gt;\r\n    )\r\n\r\n    ai_msg = response[&lt;span class=&quot;hljs-string&quot;&gt;&#039;choices&#039;&lt;\/span&gt;][&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;][&lt;span class=&quot;hljs-string&quot;&gt;&#039;text&#039;&lt;\/span&gt;].replace(&lt;span class=&quot;hljs-string&quot;&gt;&#039;\\n&#039;&lt;\/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#039;&#039;&lt;\/span&gt;)\r\n    print(&lt;span class=&quot;hljs-string&quot;&gt;&#039;ai &gt; &#039;&lt;\/span&gt;+ai_msg)\r\n    messages = &lt;span class=&quot;hljs-string&quot;&gt;f&#039;&lt;span class=&quot;hljs-subst&quot;&gt;{messages}&lt;\/span&gt;\\n&lt;span class=&quot;hljs-subst&quot;&gt;{ai_msg}&lt;\/span&gt;\\n\\n&#039;&lt;\/span&gt;  &lt;span class=&quot;hljs-comment&quot;&gt;# \u5408\u4f75 AI \u56de\u61c9\u7684\u8a71&lt;\/span&gt;\r\n&lt;\/code&gt;<\/pre>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-12.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - text-davinci-003 \u6a21\u578b\u4e32\u9023\u4e0a\u4e0b\u6587\u8a9e\u53e5\" \/><\/p>\n<p>\u5982\u679c\u4f7f\u7528 gpt-3.5-turbo \u6a21\u578b\uff0c\u76f4\u63a5\u5c07\u56de\u61c9\u8a9e\u53e5\u4f7f\u7528\u5b57\u5178\u7684\u683c\u5f0f\uff0c\u6dfb\u52a0\u5230\u56de\u61c9\u7684 messages \u4e32\u5217\u4e2d\uff0cChatGPT \u5c31\u80fd\u81ea\u52d5\u8b58\u5225\u4e0a\u4e0b\u8a9e\u53e5\u3002<\/p>\n<pre class=\"prettyprint\">&lt;code class=&quot;language-py hljs python&quot;&gt;&lt;span class=&quot;hljs-keyword&quot;&gt;import&lt;\/span&gt; openai\r\nopenai.api_key = &lt;span class=&quot;hljs-string&quot;&gt;&#039;\u4f60\u7684 API Key&#039;&lt;\/span&gt;\r\n\r\nmessages = []\r\n&lt;span class=&quot;hljs-keyword&quot;&gt;while&lt;\/span&gt; &lt;span class=&quot;hljs-literal&quot;&gt;True&lt;\/span&gt;:\r\n    msg = input(&lt;span class=&quot;hljs-string&quot;&gt;&#039;me &gt; &#039;&lt;\/span&gt;)\r\n    messages.append({&lt;span class=&quot;hljs-string&quot;&gt;&quot;role&quot;&lt;\/span&gt;:&lt;span class=&quot;hljs-string&quot;&gt;&quot;user&quot;&lt;\/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&quot;content&quot;&lt;\/span&gt;:msg})   &lt;span class=&quot;hljs-comment&quot;&gt;# \u6dfb\u52a0 user \u56de\u61c9&lt;\/span&gt;\r\n    response = openai.ChatCompletion.create(\r\n        model=&lt;span class=&quot;hljs-string&quot;&gt;&quot;gpt-3.5-turbo&quot;&lt;\/span&gt;,\r\n        max_tokens=&lt;span class=&quot;hljs-number&quot;&gt;128&lt;\/span&gt;,\r\n        temperature=&lt;span class=&quot;hljs-number&quot;&gt;0.5&lt;\/span&gt;,\r\n        messages=messages\r\n    )\r\n    ai_msg = response.choices[&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;].message.content.replace(&lt;span class=&quot;hljs-string&quot;&gt;&#039;\\n&#039;&lt;\/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&#039;&#039;&lt;\/span&gt;)\r\n    messages.append({&lt;span class=&quot;hljs-string&quot;&gt;&quot;role&quot;&lt;\/span&gt;:&lt;span class=&quot;hljs-string&quot;&gt;&quot;assistant&quot;&lt;\/span&gt;,&lt;span class=&quot;hljs-string&quot;&gt;&quot;content&quot;&lt;\/span&gt;:ai_msg})   &lt;span class=&quot;hljs-comment&quot;&gt;# \u6dfb\u52a0 ChatGPT \u56de\u61c9&lt;\/span&gt;\r\n    print(&lt;span class=&quot;hljs-string&quot;&gt;f&#039;ai &gt; &lt;span class=&quot;hljs-subst&quot;&gt;{ai_msg}&lt;\/span&gt;&#039;&lt;\/span&gt;)\r\n&lt;\/code&gt;<\/pre>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-13.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - text-davinci-003 \u6a21\u578b\u4e32\u9023\u4e0a\u4e0b\u6587\u8a9e\u53e5\" \/><\/p>\n<p>\u4e0d\u904e\u8981\u6ce8\u610f\u7684\u662f\uff0c<em>\u50b3\u9001\u6b77\u53f2\u7d00\u9304\u6703\u9020\u6210\u6574\u9ad4 token \u66b4\u589e\uff01<\/em>\u5fc5\u9808\u8981\u8a55\u4f30\u81ea\u5df1\u7684\u53e3\u888b\u6df1\u5ea6 ( \u6709\u6c92\u6709\u628a\u63e1\u53ef\u4ee5\u652f\u4ed8\u8cbb\u7528 )\uff0c\u518d\u6c7a\u5b9a\u662f\u5426\u8981\u9032\u884c\u6b77\u53f2\u7d00\u9304\u7684\u52d5\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" class=\"b1\" src=\"https:\/\/steam.oxxostudio.tw\/webp\/python\/example\/openai-14.webp\" alt=\"Python \u6559\u5b78 - \u4f7f\u7528 OpenAI - \u50b3\u9001\u6b77\u53f2\u7d00\u9304\u6703\u9020\u6210\u6574\u9ad4 token \u6578\u66b4\u589e\" \/><\/p>\n<h2 id=\"more\">\u5ef6\u4f38\u95b1\u8b80<\/h2>\n<p>\u5982\u679c\u5df2\u7d93\u53ef\u4ee5\u4f7f\u7528 Python \u4e32\u63a5 OpenAI\uff0c\u5c31\u53ef\u4ee5\u53c3\u8003\u4e0b\u65b9\u7684\u6587\u7ae0\uff0c\u5c07 OpenAI \u8207 LINE BOT \u4e32\u9023\uff0c\u505a\u51fa\u4e00\u500b\u5177\u6709\u4eba\u5de5\u667a\u6167\u7684 LINE \u804a\u5929\u6a5f\u5668\u4eba\u56c9\uff01<\/p>\n<blockquote>\n<ul>\n<li><a title=\"LINE BOT \u4e32\u63a5 OpenAI ( \u8b93 AI \u56de\u8986\u8a0a\u606f )\" href=\"https:\/\/steam.oxxostudio.tw\/category\/python\/example\/line-bot-openai-1.html\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >LINE BOT \u4e32\u63a5 OpenAI ( \u8b93 AI \u56de\u8986\u8a0a\u606f )<\/a><\/li>\n<li><a title=\"LINE BOT \u4e32\u63a5 OpenAI ( \u7ffb\u8b6f\u6a5f\u5668\u4eba )\" href=\"https:\/\/steam.oxxostudio.tw\/category\/python\/example\/line-bot-openai-2.html\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >LINE BOT \u4e32\u63a5 OpenAI ( \u7ffb\u8b6f\u6a5f\u5668\u4eba )<\/a><\/li>\n<\/ul>\n<\/blockquote>\n<h2 id=\"end\">\u5c0f\u7d50<\/h2>\n<p>\u900f\u904e Python \u4e32\u63a5 OpenAI \u7684 API \u5176\u5be6\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u7533\u8acb API Key \u5c31\u80fd\u4e32\u63a5\uff0c\u7531\u65bc\u662f\u514d\u8cbb\u7248\u672c\uff0c\u5728\u4f7f\u7528\u4e0a\u4ecd\u7136\u6703\u6709\u6bcf\u500b\u6708 18 \u7f8e\u5143\u7684\u300c\u984d\u5ea6\u300d\u9650\u5236 ( \u53c3\u8003\u00a0<a title=\"OpenAI Usage\" href=\"https:\/\/beta.openai.com\/account\/usage\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" >OpenAI Usage<\/a>\u00a0)\uff0c\u4f46\u5c0d\u65bc\u4e00\u822c\u958b\u767c\u8005\u4f86\u8aaa\u61c9\u8a72\u662f\u5f88\u8db3\u5920\u4e86\uff0c\u76f8\u4fe1\u672a\u4f86\u7684\u751f\u6d3b\u88e1\uff0c\u61c9\u8a72\u6703\u5145\u65a5\u8457\u8a31\u591a AI \u4ee3\u66ff\u771f\u4eba\u7684\u8a0a\u606f\u6216\u8a9e\u97f3\u56de\u8986\uff0c\u81f3\u65bc\u5982\u4f55\u5224\u65b7\u8a0a\u606f\u7684\u597d\u58de\u6216\u662f\u8655\u7406\u884d\u751f\u7684\u9053\u5fb7\u502b\u7406\u554f\u984c\uff0c\u5c31\u6709\u5f85\u4eba\u985e\u667a\u5546\u4f86\u89e3\u6c7a\u4e86\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8a3b\u518a OpenAI \u524d\u5f80 OpenAI \u7684\u7db2\u7ad9\u3002 OpenAI \u7db2\u7ad9\uff1ahttps:\/\/openai.com\/  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,39,3],"tags":[55,56],"_links":{"self":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/1293"}],"collection":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1293"}],"version-history":[{"count":1,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/1293\/revisions"}],"predecessor-version":[{"id":1294,"href":"https:\/\/qssjx.com\/index.php?rest_route=\/wp\/v2\/posts\/1293\/revisions\/1294"}],"wp:attachment":[{"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qssjx.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}