{"id":1354,"date":"2023-02-07T12:06:10","date_gmt":"2023-02-07T04:06:10","guid":{"rendered":"https:\/\/blog.iotot.com\/?p=1354"},"modified":"2023-02-07T12:06:10","modified_gmt":"2023-02-07T04:06:10","slug":"chatgpt%e7%9a%84api%e8%b0%83%e7%94%a8%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.iotot.com\/?p=1354","title":{"rendered":"chatGPT\u7684API\u8c03\u7528\u4ee3\u7801"},"content":{"rendered":"<p>https:\/\/github.com\/amrrs\/chatgpt-clone<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import os\r\nimport openai\r\nimport gradio as gr\r\n\r\n#if you have OpenAI API key as an environment variable, enable the below\r\n#openai.api_key = os.getenv(\"OPENAI_API_KEY\")\r\n\r\n#if you have OpenAI API key as a string, enable the below\r\nopenai.api_key = \"xxxxxx\"\r\n\r\nstart_sequence = \"\\nAI:\"\r\nrestart_sequence = \"\\nHuman: \"\r\n\r\nprompt = \"The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\\n\\nHuman: Hello, who are you?\\nAI: I am an AI created by OpenAI. How can I help you today?\\nHuman: \"\r\n\r\ndef openai_create(prompt):\r\n\r\n    response = openai.Completion.create(\r\n    model=\"text-davinci-003\",\r\n    prompt=prompt,\r\n    temperature=0.9,\r\n    max_tokens=150,\r\n    top_p=1,\r\n    frequency_penalty=0,\r\n    presence_penalty=0.6,\r\n    stop=[\" Human:\", \" AI:\"]\r\n    )\r\n\r\n    return response.choices[0].text\r\n\r\n\r\n\r\ndef chatgpt_clone(input, history):\r\n    history = history or []\r\n    s = list(sum(history, ()))\r\n    s.append(input)\r\n    inp = ' '.join(s)\r\n    output = openai_create(inp)\r\n    history.append((input, output))\r\n    return history, history\r\n\r\n\r\nblock = gr.Blocks()\r\n\r\n\r\nwith block:\r\n    gr.Markdown(\"\"\"&lt;h1&gt;&lt;center&gt;Build Yo'own ChatGPT with OpenAI API &amp; Gradio&lt;\/center&gt;&lt;\/h1&gt;\r\n    \"\"\")\r\n    chatbot = gr.Chatbot()\r\n    message = gr.Textbox(placeholder=prompt)\r\n    state = gr.State()\r\n    submit = gr.Button(\"SEND\")\r\n    submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])\r\n\r\nblock.launch(debug = True)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/github.com\/amrrs\/chatgpt-clone import os import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[95],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1354"}],"collection":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1354"}],"version-history":[{"count":1,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1354\/revisions"}],"predecessor-version":[{"id":1355,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=\/wp\/v2\/posts\/1354\/revisions\/1355"}],"wp:attachment":[{"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iotot.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}