{"id":7150,"date":"2023-05-02T23:32:00","date_gmt":"2023-05-02T15:32:00","guid":{"rendered":"https:\/\/vip.fxxkpython.com\/?p=7150"},"modified":"2023-05-02T23:32:00","modified_gmt":"2023-05-02T15:32:00","slug":"%e4%bd%bf%e7%94%a8-python-%e6%8e%a5%e5%85%a5-chatgpt-%e7%9a%84%e6%ad%a3%e7%a1%ae%e5%a7%bf%e5%8a%bf","status":"publish","type":"post","link":"https:\/\/vip.fxxkpython.com\/?p=7150","title":{"rendered":"\u4f7f\u7528 Python \u63a5\u5165 ChatGPT \u7684\u6b63\u786e\u59ff\u52bf"},"content":{"rendered":"\n<p>\u76f8\u4fe1\u4f60\u5e94\u8be5\u6709\u4e86 ChatGPT \u8d26\u6237<\/p>\n\n\n\n<p>\u5e76\u4e14\u5e38\u5728\u5b83\u7684\u7f51\u9875\u7248\u4e0a\u8fdb\u884c\u4f7f\u7528<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pica.zhimg.com\/80\/v2-57b33d28de091c205c6fac43547315a9_720w.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u4f46\u4e5f\u8bb8\u4f60\u9700\u8981\u901a\u8fc7 Python \u5bf9\u5b83\u8fdb\u884c\u64cd\u4f5c<\/p>\n\n\n\n<p>\u4e0d\u7ba1\u662f\u57fa\u4e8e ChatGPT \u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u4e5f\u597d<\/p>\n\n\n\n<p>\u4ea6\u6216\u8005\u662f\u5355\u7eaf\u4e0d\u60f3\u6d6a\u8d39\u4e86\u81ea\u5df1\u7684 API<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-570c2af3203f16351e2a8540ecec2f17_720w.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c0f\u5e05b\u7ed9\u4f60\u4ecb\u7ecd\u4e00\u4e0b<\/p>\n\n\n\n<p>\u5982\u4f55\u4f7f\u7528 Python \u641e ChatGPT<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5728\u4ee5\u4e0b\u94fe\u63a5\u67e5\u770b\u4f60\u7684 API_KEY<\/p>\n\n\n\n<p><a href=\"https:\/\/platform.openai.com\/account\/api-keys\">https:\/\/platform.openai.com\/account\/api-keys<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-f26f02aa728c08914663798283b281ef_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u5728\u4ee5\u4e0b\u94fe\u63a5\u67e5\u770b\u4f60 API \u7684\u4f7f\u7528\u60c5\u51b5<\/p>\n\n\n\n<p><a href=\"https:\/\/platform.openai.com\/account\/usage\">https:\/\/platform.openai.com\/account\/usage<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-471fa2d6b8b7bd1900496918537632ff_720w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-96be14995cd1566e535cb8185c0ab867_720w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u5728\u4f7f\u7528 ChatGPT API \u4e4b\u524d<\/p>\n\n\n\n<p>\u4f60\u8981\u77e5\u9053\u51e0\u4e2a\u8981\u70b9\uff1a<\/p>\n\n\n\n<p><strong>1\u3001 Prompt<\/strong><\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u5bf9\u5b83\u7684\u8f93\u5165\uff0c\u4e5f\u5c31\u662f\u4f60\u7ecf\u5e38\u95ee ChatGPT \u65f6\u5728\u952e\u76d8\u4e0a\u6240\u6572\u6253\u7684\u5185\u5bb9\uff0c\u6bd4\u5982\u4e00\u4e9b\u6307\u4ee4\uff0c\u95ee\u9898\u7b49\u3002<\/p>\n\n\n\n<p><strong>2\u3001Token<\/strong><\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u7406\u89e3\u4e3a\u4f60\u5728\u548c ChatGPT \u4ea4\u4e92\u7684\u8fc7\u7a0b\u4e2d\uff0c\u751f\u6210\u7684\u4e00\u4e2a\u4e2a\u5355\u8bcd\uff0c\u6709\u65f6\u5019\u4e5f\u53ef\u80fd\u662f\u8fde\u7eed\u7684\u4e00\u90e8\u5206\u5b57\u7b26\uff0c\u4e00\u822c\u6bcf\u4e00\u4e2a\u7a7a\u683c\u540e\u7b97\u4e00\u4e2a token\u3002<\/p>\n\n\n\n<p>\u4f60\u5728\u4f7f\u7528 GPT-3.5 \u7684\u65f6\u5019\uff0c\u4f1a\u8bdd\u6700\u591a\u7684 token \u4e3a 4096 \u4e2a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-191404e252fd6ec383fb086334776056_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>3\u3001Model<\/strong><\/p>\n\n\n\n<p>\u4e0a\u56fe\u4e2d\u7684 gpt-3.5-turbo \u5c31\u662f Model\uff0c\u4e5f\u5c31\u662f\u7531 OPEN API \u63d0\u4f9b\u7684\u6a21\u578b\u4e4b\u4e00\uff0c\u4e0d\u540c\u7684\u6a21\u578b\u63d0\u4f9b\u7684\u529f\u80fd\u548c\u4ef7\u683c\u90fd\u4e0d\u540c\uff0c\u6bd4\u5982 GPT-4 \u662f OpenAI API \u7684\u6700\u65b0\u3001\u6700\u5f3a\u5927\u7684\u6a21\u578b\uff0c\u800c GPT-3.5-Turbo \u662f ChatGPT \u7684\u57fa\u7840\u6a21\u578b\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-93db885de4852b483ff342a53da3d1d2_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-78e438eec741d670a660f43df73a291f_720w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u6211\u4eec\u6700\u5e38\u7528\u7684\u6a21\u578b\u5c31\u662f gpt-3.5-turbo\u3002<\/p>\n\n\n\n<p>\u60f3\u8981\u4f7f\u7528 Python \u64cd\u4f5c ChatGPT \u7684 API<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5728\u4ee5\u4e0b\u94fe\u63a5\u627e\u5230\u5b83\u7684\u8bf7\u6c42\u53c2\u6570<\/p>\n\n\n\n<p><a href=\"https:\/\/platform.openai.com\/docs\/api-reference\/chat\/create\">https:\/\/platform.openai.com\/docs\/api-reference\/chat\/create<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-c1345449e151e2d75e1eb02033d46bcd_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4ee5\u4e0b\u8fd9\u4e9b\u53c2\u6570\u662f\u5fc5\u987b\u63d0\u4f9b\u7684\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pica.zhimg.com\/80\/v2-c948889a0f29d187ecd3dab48bb1cca3_720w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u8fd4\u56de\u7ed3\u679c\u5728 choices[0].message.content \u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-cfbf08537e799046a5db70019b71ab3e_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 request \u6765\u8c03\u7528\u5b83<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pica.zhimg.com\/80\/v2-9e0f5049c601ff1c137394600e6708cb_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd0\u884c\u8d77\u6765\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-06dc7ab450e7330f4065e4dd0f1f83e0_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4f60\u901a\u8fc7 Json \u89e3\u6790\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd4\u56de\u7684\u7b54\u6848\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-236c94bc8101fce43cddd5a1d94d0588_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-4fd36296f3e03b5684c0912e93287e42_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-0137e4b9fb1fd4292de4063c834c2929_720w.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">\u6dfb\u52a0\u56fe\u7247\u6ce8\u91ca\uff0c\u4e0d\u8d85\u8fc7 140 \u5b57\uff08\u53ef\u9009\uff09<\/figcaption><\/figure>\n\n\n\n<p>\u4f7f\u7528 Python \u64cd\u4f5c ChatGPT \u66f4\u597d\u7684\u65b9\u5f0f<\/p>\n\n\n\n<p>\u662f\u4f7f\u7528 openai \u8fd9\u4e2a\u5e93<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5148\u5b89\u88c5\u4e00\u6ce2<\/p>\n\n\n\n<p>python3 -m pip install openai<\/p>\n\n\n\n<p>\u63a5\u7740\u51e0\u884c\u4ee3\u7801\u5c31\u53ef\u4ee5\u8c03\u7528\u5b83\u7684 API<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-6cd94211c60fec4b899d139477ae5409_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd0\u884c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pica.zhimg.com\/80\/v2-3302e6a9a46b916b3c5b2b7621151883_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a while \u5faa\u73af<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pica.zhimg.com\/80\/v2-71005f4db540d9c0d1d7e41ba8d8208e_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5728\u7ec8\u7aef\u548c ChatGPT \u804a\u5929\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-eaf69828453889f4be12a511f4975f96_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u5f53\u7136\uff0c\u8fd9\u6837\u5b83\u53ea\u80fd\u7eaf\u56de\u7b54<\/p>\n\n\n\n<p>\u4e0d\u5177\u5907\u4e0a\u4e0b\u6587\u7406\u89e3\u80fd\u529b<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4fdd\u7559\u4e0a\u4e0b\u6587\u540e\u518d\u8bf7\u6c42 ChatGPT<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/picx.zhimg.com\/80\/v2-b64c3f8dbfa117b2ba620f79fcd300e1_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u6709\u6a21\u6709\u6837\u7684\u804a\u5929\u4e86\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-9ab34fe0b566140f2b7a9eedfbe0510d_720w.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>OK\uff0c \u4ee5\u4e0a\u5c31\u662f\u5c0f\u5e05b\u4eca\u5929\u7ed9\u4f60\u5e26\u6765\u7684\u5206\u4eab\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a ChatGPT \u548c Python \u7684\u4f7f\u7528\u79c1\u5bc6\u6559\u7a0b\uff0c\u53ef\u4ee5\u52a0\u5165<a target=\"_blank\" href=\"http:\/\/mp.weixin.qq.com\/s?__biz=Mzg2NzYyNjg2Nw==&amp;mid=2247490010&amp;idx=1&amp;sn=ae6843b25a5e112eac3df0c4aaeced49&amp;chksm=ceb9e3c6f9ce6ad04fc8f05b10d0b5a2b31944c20c77ea794bfc1d07d190f0d73885af2182eb&amp;scene=21#wechat_redirect\" rel=\"noreferrer noopener\">\u5c0f\u5e05b\u7684 VIP\u3002<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u4f60\u5e94\u8be5\u6709\u4e86 ChatGPT \u8d26\u6237 \u5e76\u4e14\u5e38\u5728\u5b83\u7684\u7f51\u9875\u7248\u4e0a\u8fdb\u884c\u4f7f\u7528 \u4f46\u4e5f\u8bb8\u4f60\u9700\u8981\u901a\u8fc7 Python \u5bf9\u5b83\u8fdb\u884c\u64cd\u4f5c \u4e0d\u7ba1\u662f\u57fa\u4e8e ChatGPT \u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u4e5f\u597d \u4ea6\u6216\u8005\u662f\u5355\u7eaf\u4e0d\u60f3\u6d6a\u8d39\u4e86\u81ea\u5df1\u7684 API \u63a5\u4e0b\u6765\u5c0f\u5e05b\u7ed9\u4f60\u4ecb\u7ecd\u4e00\u4e0b \u5982\u4f55\u4f7f\u7528 Python \u641e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":7151,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[644],"tags":[643,19],"_links":{"self":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/7150"}],"collection":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7150"}],"version-history":[{"count":1,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/7150\/revisions"}],"predecessor-version":[{"id":7152,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/posts\/7150\/revisions\/7152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=\/wp\/v2\/media\/7151"}],"wp:attachment":[{"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vip.fxxkpython.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}