Excel中直接调用ChatGPT,方法来了
马老师上期讲了如何在Google Sheets中通过插件使用ChatGPT。但是这显然满足不了热爱Microsoft Excel的骨灰粉。于是本篇作为补充,极诣将提供在Excel中调用ChatGPT的方法。
最重要的事先讲一下,由于需要运行VBA。马老师没有在Mac平台做过测试,仅在Windows上运行过。另外你需要科学上网,一些梯子请调整到全局模式。
方法一,用VBA UDF来实现
2023年7月注,该方法已更新,请查看马老师最新文章《在Excel中用GPT-4模型批量调用ChatGPT》
你可以把马老师的代码放到你的personal.xlsb文件中,也可以放到其他module中。至于什么是personal.xlsb请自行bing。API KEY的获取请至OpenAPI官网。代码如下:
Option Explicit
Public Function ChatGPT(ByVal question As String, _
Optional ByVal temperature As Single = 0, _
Optional ByVal maxtoken As Integer = 500, _
Optional ByVal model As String = "gpt-3.5-turbo") As String
Dim apiEndpoint As String
Dim requestHeaders As String
Dim httpRequest As MSXML2.XMLHTTP
Dim httpResponse As String
Dim cursor As Long
Const apiKey As String = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXX"
Set httpRequest = New MSXML2.XMLHTTP
If model = "gpt-3.5-turbo" Then
apiEndpoint = "https://api.openai.com/v1/chat/completions"
httpRequest.Open "POST", apiEndpoint, False
httpRequest.SetRequestHeader "Content-Type", "application/json"
httpRequest.SetRequestHeader "Authorization", "Bearer " & apiKey
httpRequest.Send "{""model"": """ & model & """,""messages"": [{""role"": ""user"", ""content"": """ & _
question & """}], ""temperature"": " & temperature & "}"
If httpRequest.Status = 200 Then
cursor = InStr(1, httpRequest.responseText, """content" & """:""")
ChatGPT = Replace(Mid(httpRequest.responseText, cursor + 11, InStr(cursor, httpRequest.responseText, """}," _
& """f") - cursor - 11), "\n", vbCrLf)
Else
ChatGPT = "API Error: " & httpRequest.responseText
End If
Else
apiEndpoint = "https://api.openai.com/v1/completions"
httpRequest.Open "POST", apiEndpoint, False
httpRequest.SetRequestHeader "Content-Type", "application/json"
httpRequest.SetRequestHeader "Authorization", "Bearer " & apiKey
httpRequest.Send "{""model"": """ & model & """,""prompt"": """ & question & """,""max_tokens"": " & maxtoken & _
",""temperature"": " & temperature & ",""n"": 1,""stream"": false,""logprobs"": null}"
If httpRequest.Status = 200 Then
cursor = InStr(1, httpRequest.responseText, """text" & """:""")
ChatGPT = Replace(Mid(httpRequest.responseText, cursor + 8, InStr(cursor, httpRequest.responseText, """," _
& """index") - cursor - 8), "\n", vbCrLf)
Else
ChatGPT = "API Error: " & httpRequest.responseText
End If
End If
End Function
上述代码有四个参数,分别是
- question,也就是prompt
- temperature,范围是0-2,越大越有创意
- maxtoken,建议控制好以免浪费API费用
- model,模型,默认是gpt-3.5-turbo,也可以用达芬奇003-“text-davinci-003”
当马老师写这篇文章的时候,OpenAI已经宣布免费用户API调用调整到每分钟三次。不过大家还是先充值信仰吧。反正便宜量足。
方法二,使用ChatGPT插件
如果说上面是一个轻量级应用,那么下面这个插件将可以解决更多问题。至于Excel插件如何使用请自行百度。
这个插件由ListenData制作并免费提供下载。马老师建议大家先去保存一个免费的版本以免未来收费。😊😊😊网址在这里
https://www.listendata.com/2023/03/how-to-run-chatgpt-inside-excel.html
下载链接:
https://github.com/deepanshu88/excelChatGPT/raw/main/ExcelAddIn/ChatGPT.xlam
这个插件有四个功能:
AIAssistant(text, [word_count])
使用ChatGPT生成并提供搜索查询的输出AIAssistant_FillData(rng_existingdata, rng_fill)
通过在现有数据上训练ChatGPT来填充不完整的数据AIAssistant_Extractor(prompt, keyword)
从ChatGPT中提取关键数据。关键数据可以是姓名、地点、组织详细信息等。AIAssistant_Explain(cell_formula, [detail])
帮助您解释您不理解和需要支持的Excel公式。
好了,以上两种方法你会选择哪一种呢?还是两种都用呢?期待你的反馈。