{"id":522,"date":"2024-06-02T23:29:38","date_gmt":"2024-06-02T15:29:38","guid":{"rendered":"https:\/\/blog.mada.press\/?p=522"},"modified":"2024-06-02T23:29:38","modified_gmt":"2024-06-02T15:29:38","slug":"excle%e4%b8%ad%e8%b0%83%e7%94%a8ai%e7%9a%84vba%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/blog.mada.press\/index.php\/2024\/06\/02\/excle%e4%b8%ad%e8%b0%83%e7%94%a8ai%e7%9a%84vba%e4%bb%a3%e7%a0%81\/","title":{"rendered":"Excle\u4e2d\u8c03\u7528AI\u7684VBA\u4ee3\u7801"},"content":{"rendered":"\n<p>\u53c2\u8003Github\u9879\u76ee<br><a href=\"https:\/\/github.com\/Sven-Bo\/Integrate-ChatGPT-in-Excel-using-VBA\/blob\/master\/Demo_ChatGPT.xlsm\" target=\"_blank\" rel=\"noreferrer noopener\">Integrate-ChatGPT-in-Excel-using-VBA\/Demo_ChatGPT.xlsm at master \u00b7 Sven-Bo\/Integrate-ChatGPT-in-Excel-using-VBA (github.com)<\/a><\/p>\n\n\n\n<p>\u4ee3\u7801\u5728ChatGPT-4o\u6a21\u578b\u5e2e\u52a9\u4e0b\u5b8c\u6210<\/p>\n\n\n\n<p>\u76ee\u7684\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5316\u539f\u9879\u76ee\u4ee3\u7801\uff0c\u5b9a\u4e49\u51fd\u6570\uff0c\u5c06\u54cd\u5e94\u8f93\u51fa\u5230Excel\u5355\u5143\u683c\u3002<\/li>\n\n\n\n<li>\u5b9e\u73b0\u5f02\u6b65\u8bf7\u6c42\uff0cExcel\u7b49\u5f85 HTTP \u8bf7\u6c42\u54cd\u5e94\u65f6\u4e0d\u4f1a\u51bb\u7ed3\u7a97\u53e3\u3002<\/li>\n\n\n\n<li>\u914d\u5408ONEAPI\u4f7f\u7528\uff0c\u53ef\u5728\u56fd\u5185\u7f51\u7edc\u73af\u5883\u8c03\u7528Azure Openai\u7b49\u5927\u6a21\u578bAPI\u63a5\u53e3\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Option Explicit\n\n'=====================================================\nConst API_KEY As String = \"&lt;API_KEY>\"\nConst API_ENDPOINT As String = \"https:\/\/api.openai.com\/v1\/chat\/completions\"\nConst MODEL As String = \"chatgpt-3.5-turbo\"\nConst MAX_TOKENS As String = \"512\"\nConst TEMPERATURE As String = \"0.1\"\n'=====================================================\n\nFunction Chat(prompt As String) As String\n    ' \u68c0\u67e5 API \u5bc6\u94a5\u662f\u5426\u53ef\u7528\n    If API_KEY = \"&lt;API_KEY>\" Then\n        MsgBox \"\u8bf7\u5728\u4ee3\u7801\u4e2d\u8f93\u5165\u6709\u6548\u7684 API \u5bc6\u94a5\u3002\", vbCritical, \"\u672a\u627e\u5230 API \u5bc6\u94a5\"\n        Exit Function\n    End If\n\n    ' \u521b\u5efa XMLHTTP \u5bf9\u8c61\n    Dim httpRequest As Object\n    Set httpRequest = CreateObject(\"MSXML2.XMLHTTP\")\n\n    ' \u5b9a\u4e49\u8bf7\u6c42\u4f53\n    Dim requestBody As String\n    requestBody = \"{\" &amp; _\n        \"\"\"model\"\": \"\"\" &amp; MODEL &amp; \"\"\",\" &amp; _\n        \"\"\"messages\"\": &#91;{\"\"role\"\": \"\"user\"\", \"\"content\"\": \"\"\" &amp; prompt &amp; \"\"\"}],\" &amp; _\n        \"\"\"max_tokens\"\": \" &amp; MAX_TOKENS &amp; \",\" &amp; _\n        \"\"\"temperature\"\": \" &amp; TEMPERATURE &amp; _\n        \"}\"\n\n    ' \u53d1\u9001 HTTP \u8bf7\u6c42\n    With httpRequest\n        .Open \"POST\", API_ENDPOINT, True ' \u4f7f\u7528\u5f02\u6b65\u8bf7\u6c42\n        .SetRequestHeader \"Content-Type\", \"application\/json\"\n        .SetRequestHeader \"Authorization\", \"Bearer \" &amp; API_KEY\n        .send (requestBody)\n    End With\n\n    ' \u7b49\u5f85\u8bf7\u6c42\u5b8c\u6210\n    Do While httpRequest.readyState &lt;> 4\n        DoEvents\n    Loop\n\n    ' \u68c0\u67e5\u8bf7\u6c42\u662f\u5426\u6210\u529f\n    If httpRequest.Status = 200 Then\n        Dim response As String\n        response = httpRequest.responseText\n\n        Dim completion As String\n        completion = ParseResponse(response)\n\n        ' \u8fd4\u56de\u751f\u6210\u7684\u6587\u672c\u5b8c\u6210\u7ed3\u679c\n        Chat = completion\n    Else\n        MsgBox \"\u8bf7\u6c42\u5931\u8d25,\u72b6\u6001\u7801:\" &amp; httpRequest.Status &amp; vbCrLf &amp; vbCrLf &amp; \"\u9519\u8bef\u6d88\u606f:\" &amp; vbCrLf &amp; httpRequest.responseText, vbCritical, \"OpenAI \u8bf7\u6c42\u5931\u8d25\"\n        Chat = \"\"\n    End If\nEnd Function\n\nFunction ParseResponse(ByVal response As String) As String\n    ' \u4ece JSON \u54cd\u5e94\u4e2d\u89e3\u6790\u751f\u6210\u7684\u6587\u672c\n    On Error Resume Next\n    Dim startIndex As Long\n    startIndex = InStr(response, \"\"\"content\"\":\"\"\") + 11\n    Dim endIndex As Long\n    endIndex = InStr(startIndex, response, \"\"\"\") - 1\n    ParseResponse = Mid(response, startIndex, endIndex - startIndex)\n    On Error GoTo 0\nEnd Function\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003Github\u9879\u76eeIntegrate-ChatGPT-in-Excel-usi &hellip; <a href=\"https:\/\/blog.mada.press\/index.php\/2024\/06\/02\/excle%e4%b8%ad%e8%b0%83%e7%94%a8ai%e7%9a%84vba%e4%bb%a3%e7%a0%81\/\">\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":[],"class_list":["post-522","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/522","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=522"}],"version-history":[{"count":1,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions"}],"predecessor-version":[{"id":523,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/posts\/522\/revisions\/523"}],"wp:attachment":[{"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/media?parent=522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/categories?post=522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mada.press\/index.php\/wp-json\/wp\/v2\/tags?post=522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}