{"id":459,"date":"2023-03-20T14:55:38","date_gmt":"2023-03-20T06:55:38","guid":{"rendered":"https:\/\/fugary.com\/?p=459"},"modified":"2023-04-15T12:59:28","modified_gmt":"2023-04-15T04:59:28","slug":"%e7%94%b3%e8%af%b7chatgpt%e7%9a%84api-key%e4%bb%a5%e5%8f%8a%e7%ae%80%e5%8d%95%e8%af%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=459","title":{"rendered":"\u7533\u8bf7ChatGPT\u7684API key\u4ee5\u53ca\u7b80\u5355\u8bd5\u7528"},"content":{"rendered":"<p>\u9996\u5148\u5df2\u7ecf\u6709\u4e86<code>ChatGPT<\/code>\u7684\u8d26\u53f7\uff0c\u5e76\u4e14\u767b\u5f55\u8fdb\u53bb\uff0c\u7136\u540e\u624d\u80fd\u7533\u8bf7<code>API key<\/code>\uff0c\u5982\u679c\u8fd8\u6ca1\u6709\u53ef\u4ee5\u53c2\u8003\u3010<a href=\"https:\/\/fugary.com\/?p=454\">\u56fd\u5185\u6ce8\u518cChatGPT\u8be6\u7ec6\u6d41\u7a0b<\/a>\u3011<\/p>\n<p>\u6d4b\u8bd5\u4e0b\u6765\u9700\u8981\u79d1\u5b66\u4e0a\u7f51\u73af\u5883\uff0c\u800c\u4e14\u9700\u8981\u9650\u5236\u56fd\u5bb6\uff0c\u5426\u5219\u51fa\u73b0\u5982\u4e0b\u4fe1\u606f\uff0c\u9700\u8981\u81ea\u5df1\u5207\u6362\u4e0b\u81ea\u5df1\u7684\u79d1\u5b66\u4e0a\u7f51\u7684\u7ebf\u8def\uff1a<\/p>\n<blockquote>\n<p>Not available<\/p>\n<p>OpenAI's services are not available in your country.<\/p>\n<\/blockquote>\n<h3>\u6570\u636e\u51c6\u5907<\/h3>\n<p><code>ChatGPT<\/code>\u7684<code>API<\/code>\u7528\u5230<code>\u7ec4\u7ec7ID<\/code>\u548c<code>API key<\/code>\u4e24\u4e2a\u503c\uff0c\u4e0d\u8fc7<code>Organization ID<\/code>\u4e0d\u662f\u5fc5\u586b\u9879\uff0c\u53ef\u4ee5\u4e0d\u63d0\u4f9b\u3002<\/p>\n<h4>Organization ID<\/h4>\n<p><strong>Organization ID<\/strong>\u76f4\u63a5\u4ece\u8bbe\u7f6e\u4e2d\u83b7\u53d6\uff0c\u590d\u5236\u51fa\u6765\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/e6c472f75c22a4240ddca38aa1ccbbfa\/image-20230320140530922.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/e6c472f75c22a4240ddca38aa1ccbbfa\/image-20230320140530922.png\" alt=\"image-20230320140530922\" \/><\/div><\/p>\n<h4>\u7533\u8bf7API key<\/h4>\n<p>\u8fdb\u5165\u7f51\u5740\uff1a<a href=\"https:\/\/platform.openai.com\/\">https:\/\/platform.openai.com\/<\/a><\/p>\n<p>\u53f3\u4e0a\u89d2\u8fdb\u5165\u3010View API keys\u3011\uff1a<a href=\"https:\/\/platform.openai.com\/account\/api-keys\">https:\/\/platform.openai.com\/account\/api-keys<\/a><\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/21df87197a2390366f61bd3c4d398cda\/image-20230320134027001.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/21df87197a2390366f61bd3c4d398cda\/image-20230320134027001.png\" alt=\"image-20230320134027001\" \/><\/div><\/p>\n<p>\u70b9\u51fb\u3010Create new secret key\u3011\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/6988bdd15d3f73f94c5f6dd72c624336\/image-20230320134316261.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/6988bdd15d3f73f94c5f6dd72c624336\/image-20230320134316261.png\" alt=\"image-20230320134316261\" \/><\/div><\/p>\n<p>\u751f\u6210\u6210\u529f\uff0c\u81ea\u5df1\u590d\u5236\u51fa\u6765\u5e76\u4fdd\u5b58\u597d\uff0c\u53ea\u4f1a\u770b\u5230\u4e00\u6b21\uff0c\u5982\u679c\u5fd8\u4e86\u53ea\u80fd\u5220\u9664\u5e76\u91cd\u65b0\u751f\u6210\u4e00\u4e2a\u3002<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/22ea07be57c485d91d23a1cee79846a2\/image-20230320134430612.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/22ea07be57c485d91d23a1cee79846a2\/image-20230320134430612.png\" alt=\"image-20230320134430612\" \/><\/div><\/p>\n<p>\u9ed8\u8ba4\u663e\u793a\u670918\u7f8e\u5143\u4f59\u989d\uff0c\u6700\u65b0\u6ce8\u518c\u7684\u4f3c\u4e4e\u53ea\u67095\u7f8e\u5143\u4e86\u3002<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/22b8a7c34fd27ea0bf50805bee1b5245\/image-20230320143216170.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/22b8a7c34fd27ea0bf50805bee1b5245\/image-20230320143216170.png\" alt=\"image-20230320143216170\" \/><\/div><\/p>\n<p><code>ChatGPT<\/code>\u6709\u81ea\u5df1\u4e00\u5957\u8ba1\u8d39\u89c4\u5219\uff0c\u6bcf\u6b21\u54cd\u5e94\u4f1a\u63d0\u793a\u591a\u5c11<code>Token<\/code>\uff0c\u6309\u7167<code>Token<\/code>\u6570\u91cf\u6536\u8d39\u3002<\/p>\n<h3>\u7528Python\u5f00\u53d1<\/h3>\n<p>\u53c2\u8003\u6587\u6863\uff1a<a href=\"https:\/\/platform.openai.com\/docs\/api-reference\">https:\/\/platform.openai.com\/docs\/api-reference<\/a><\/p>\n<p>\u9996\u5148\u5df2\u7ecf\u6709<code>Python<\/code>\u73af\u5883\u4e86\uff0c\u5982\u679c\u73af\u5883\u90fd\u6ca1\u6709\u5c31\u6ca1\u6cd5\u8fd0\u884c\u4e86\u3002<\/p>\n<h4>\u7528Python\u6d4b\u8bd5\u8c03\u7528<\/h4>\n<p>\u5b89\u88c5\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-shell\">pip install openai<\/code><\/pre>\n<p>\u5f00\u53d1\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport openai\n\nif __name__ == &quot;__main__&quot;:\n    openai.organization = os.getenv(&quot;OPENAI_ORGANIZATION_ID&quot;)\n    openai.api_key = os.getenv(&quot;OPENAI_API_KEY&quot;)\n    models = openai.Model.list()\n    for model in models.data:\n        print(model.id)\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u4e00\u4e9b\u652f\u6301\u7684\u6a21\u578b\u3002<\/p>\n<p>\u4e0d\u540c\u7684\u6a21\u578b\u529f\u80fd\u4e0d\u540c\uff0c\u6709\u8bed\u97f3\u8f6c\u6587\u672c\u3001\u4ee3\u7801\u8865\u5168\u3001\u6587\u672c\u8865\u5168\u3001\u804a\u5929\u7b49\u3002<\/p>\n<h4>\u6d4b\u8bd5\u5bf9\u8bdd<\/h4>\n<p>\u4e0a\u9762\u6d4b\u8bd5\u53ef\u4ee5\u770b\u5230\u80fd\u4f7f\u7528\u7684\u6a21\u578b\uff0c\u8fd9\u91cc\u9009\u7528<code>gpt-3.5-turbo<\/code>\u804a\u5929\u6a21\u578b\u6765\u5b9e\u73b0\u7b80\u5355\u5bf9\u8bdd\u3002<\/p>\n<p>\u6a21\u578b\u8bf4\u6587\u6863\uff1a<a href=\"https:\/\/platform.openai.com\/docs\/models\/overview\">https:\/\/platform.openai.com\/docs\/models\/overview<\/a><\/p>\n<pre><code class=\"language-python\">import os\nimport openai\n\nif __name__ == &quot;__main__&quot;:\n    openai.organization = os.getenv(&quot;OPENAI_ORGANIZATION_ID&quot;)\n    openai.api_key = os.getenv(&quot;OPENAI_API_KEY&quot;)\n    result = openai.ChatCompletion.create(\n        model=&quot;gpt-3.5-turbo&quot;,\n        messages=[\n            {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;\u7b80\u8981\u4ecb\u7ecd\u4e0bPython\u9b54\u6cd5\u51fd\u6570&quot;}\n        ]\n    )\n    print(result)\n    if result.choices:\n        print(result.choices[0].message.content)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4f1a\u8f93\u51fa\u76f8\u5173\u4fe1\u606f\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/da9e84c172584086b901514d0eac3e5f\/image-20230320145003014.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/\/fugary\/blogpic\/uploads\/da9e84c172584086b901514d0eac3e5f\/image-20230320145003014.png\" alt=\"image-20230320145003014\" \/><\/div><\/p>\n<p>\u66f4\u591a\u4f7f\u7528\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53c2\u8003\u6587\u6863\uff1a<\/p>\n<p><a href=\"https:\/\/platform.openai.com\/docs\/guides\/chat\/introduction\">https:\/\/platform.openai.com\/docs\/guides\/chat\/introduction<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u5df2\u7ecf\u6709\u4e86ChatGPT\u7684\u8d26\u53f7\uff0c\u5e76\u4e14\u767b\u5f55\u8fdb\u53bb\uff0c\u7136\u540e\u624d\u80fd\u7533\u8bf7API key\uff0c\u5982\u679c\u8fd8\u6ca1\u6709\u53ef\u4ee5\u53c2\u8003\u3010\u56fd\u5185\u6ce8\u518cCha [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[107,4],"tags":[102,96,101],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/459"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=459"}],"version-history":[{"count":3,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":463,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions\/463"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}