{"id":56,"date":"2023-08-15T00:06:23","date_gmt":"2023-08-14T16:06:23","guid":{"rendered":"https:\/\/blog.mada.press\/?p=56"},"modified":"2023-08-15T00:21:34","modified_gmt":"2023-08-14T16:21:34","slug":"%e6%96%87%e5%bf%83%e4%b8%80%e8%a8%80%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/blog.mada.press\/index.php\/2023\/08\/15\/%e6%96%87%e5%bf%83%e4%b8%80%e8%a8%80%e6%b5%8b%e8%af%95\/","title":{"rendered":"API-\u7f51\u9875"},"content":{"rendered":"\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>\u7533\u8bf7\u4e86\u6587\u5fc3\u4e00\u8a00\u7684api\u63a5\u53e3\uff0c\u5229\u7528\u7f51\u9875\u7aef\u3001GitHub\u7684chatall\u9879\u76ee\u8bd5\u7528\u4e86\u4e0b\u3002\u611f\u89c9ERNIE-Bot\u7248\u672c\u5728\u56de\u7b54\u7269\u7406\u5316\u5b66\u548c\u7f16\u7a0b\u65f6\uff0c\u8868\u73b0\u5f97\u6bd4ERNIE-Bot-turbo\u6548\u679c\u66f4\u597d\u3002\u51c6\u786e\u5ea6\u4e5f\u5f88\u9ad8\u3002<\/p>\n\n\n\n<p>\u8ba9chatgpt3.5\u7ed9\u51fa\u4e86\u6587\u5fc3\u4e00\u8a00api\u6253\u5305\u6210\u7f51\u9875\u7684\u4ee3\u7801\u3002\u793a\u4f8b\u5982\u4e0b\uff08\u9700\u8981\u83b7\u5f97\u5176\u4e2d\u7684access_token\u5e76\u66f4\u6362\uff0c\u4fdd\u5b58\u4e3ahtml\u683c\u5f0f\u6587\u4ef6\uff09:<\/p>\n\n\n\n<p>\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0c\u5df2\u7ecf\u4f7f\u7528\u5185\u5d4c\u65b9\u5f0f\u5c06 JavaScript \u4ee3\u7801\u5d4c\u5165\u5230\u4e86 HTML \u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u6574\u5408\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;API\u4ea4\u4e92\u7f51\u9875&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h1&gt;API\u4ea4\u4e92\u7f51\u9875&lt;\/h1&gt;\n  &lt;label for=\"input\"&gt;\u8f93\u5165\uff1a&lt;\/label&gt;\n  &lt;input type=\"text\" id=\"input\" \/&gt;\n  &lt;button onclick=\"callAPI()\"&gt;\u63d0\u4ea4&lt;\/button&gt;\n  &lt;h2&gt;\u7ed3\u679c\uff1a&lt;\/h2&gt;\n  &lt;div id=\"result\"&gt;&lt;\/div&gt;\n\n  &lt;script&gt;\n    function callAPI() {\n      \/\/ \u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\n      var input = document.getElementById(\"input\").value;\n\n      \/\/ \u6784\u9020 API \u8bf7\u6c42\u7684\u6570\u636e\n      var requestData = {\n        messages: &#91;\n          { role: \"user\", content: input }\n        ]\n      };\n\n      \/\/ \u53d1\u9001 API \u8bf7\u6c42\n      fetch('https:\/\/aip.baidubce.com\/rpc\/2.0\/ai_custom\/v1\/wenxinworkshop\/chat\/completions?access_token=&#91;access_token]', {\n        method: 'POST',\n        headers: {\n          'Content-Type': 'application\/json'\n        },\n        body: JSON.stringify(requestData)\n      })\n      .then(response =&gt; response.json())\n      .then(data =&gt; {\n        \/\/ \u5904\u7406 API \u8fd4\u56de\u7684\u6570\u636e\n        var result = data.result;\n\n        \/\/ \u663e\u793a\u7ed3\u679c\n        var resultDiv = document.getElementById(\"result\");\n        resultDiv.innerText = result;\n      })\n      .catch(error =&gt; {\n        console.log(error);\n      });\n    }\n  &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0cJavaScript \u4ee3\u7801\u88ab\u76f4\u63a5\u5d4c\u5165\u5230\u4e86 <code>&lt;script&gt;<\/code> \u6807\u7b7e\u4e2d\uff0c\u4ee5\u5b9e\u73b0\u4e0e API \u7684\u4ea4\u4e92\u3002\u5f53\u7528\u6237\u70b9\u51fb &#8220;\u63d0\u4ea4&#8221; \u6309\u94ae\u65f6\uff0c<code>callAPI()<\/code> \u51fd\u6570\u4f1a\u88ab\u8c03\u7528\uff0c\u8be5\u51fd\u6570\u4f1a\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u5185\u5bb9\uff0c\u5e76\u53d1\u9001 API \u8bf7\u6c42\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5904\u7406 API \u8fd4\u56de\u7684\u6570\u636e\uff0c\u5c06\u7ed3\u679c\u663e\u793a\u5728\u9875\u9762\u4e0a\u3002<\/p>\n\n\n\n<p><\/p>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7533\u8bf7\u4e86\u6587\u5fc3\u4e00\u8a00\u7684api\u63a5\u53e3\uff0c\u5229\u7528\u7f51\u9875\u7aef\u3001GitHub\u7684chatall\u9879\u76ee\u8bd5\u7528\u4e86\u4e0b &hellip; <a href=\"https:\/\/blog.mada.press\/index.php\/2023\/08\/15\/%e6%96%87%e5%bf%83%e4%b8%80%e8%a8%80%e6%b5%8b%e8%af%95\/\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-chatgpt"],"_links":{"self":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":4,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/56\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}