M
代码部分
Option Explicit
Const key = "your key" # 这里写入我们在准备工作中拿到的API Key
Sub SendQuestionToGPT3()
'Declare variables
Dim request As Object
Dim response As String
Dim API As String
API = key
#这里我们把问题放到了B3里面
Dim question As String
question = """" & Range("B3").Value & """"
#创建HTTP request
Set request = CreateObject("MSXML2.XMLHTTP")
#设置HTTP 请求
request.Open "POST", "https://api.chatanywhere.com.cn/v1/chat/completions", False
request.setRequestHeader "Content-Type", "application/json"
request.setRequestHeader "Authorization", "Bearer " & API
request.send "{""model"": ""gpt-3.5-turbo"",""messages"": [{""role"": ""user"", ""content"": " & question & "}],""temperature"": 0.7 }"
#解析返回值,这里用到了我们刚刚import进去的library
response = request.responseText
response = Replace(response, "[{", "{")
response = Replace(response, "}]", "}")
Dim ParsedResponse As Object
Set ParsedResponse = JsonConverter.ParseJson(response)
#把返回的答案输入到excel里面
Range("B6").Value = ParsedResponse("choices")("message")("content")
'Clean up the object
Set request = Nothing
End Sub