我想使用 Excel 询问 ChatGPT 问题并将其返回到另一个单元格中。 我有一个 API,在单元格“A1”中给出。 问题应该从“A3”中取出 - 答案应该在“A6”中:
Sub SendQuestionToGPT3()
'Declare variables
Dim request As Object
Dim response As String
Dim API As String
API = Worksheets("API").Range("A1").Value
'Set the question in a variable
Dim question As String
question = Range("A3").Value
'Create an HTTP request object
Set request = CreateObject("MSXML2.XMLHTTP")
'Set the API endpoint and make the request
request.Open "POST", "https://api.openai.com/v1/engines/davinci/jobs", False
request.setRequestHeader "Content-Type", "application/json"
request.setRequestHeader "Authorization", "Bearer " & API
request.send "{""prompt"":""" & question & """,""max_tokens"":1000}"
'Get the response and parse it into a string
response = request.responseText
response = Replace(response, ",""choices"":[]", "")
response = Replace(response, """text"":""", "")
response = Replace(response, """}", "")
'Display the response in a cell
Range("A6").Value = response
'Clean up the object
Set request = Nothing
End Sub
但我收到此错误:
{ “错误”: { "message": "此模型的端点未知。", “类型”:“无效请求错误”, “参数”:空, “代码”:空 } }
这段代码有什么问题吗? 谢谢!
一些有帮助的事情。 不要使用引擎端点,因为它已被弃用。
GET https://api.openai.com/v1/engines/davinci/
引擎端点也不响应提示。它的作用是
列出当前可用(未微调)的模型,并提供每个模型的基本信息,例如所有者和可用性。
而是使用 Completion 端点。
POST https://api.openai.com/v1/completions
为了使用 API,您必须将
model
添加到您的请求中。像这样的东西。
{
"model": "text-davinci-003",
"prompt": "How are you?",
"max_tokens": 256
}
希望有帮助。
这在 MS Access VBA 中对我有用。它使用 Microsoft Scripting Runtime 和 JsonConverter 模块,用于从响应中提取文本。响应直接写入当前代码模块窗口中。
将此代码粘贴到模块中。
Option Compare Database
Option Explicit
Const API_ENDPOINT As String = "https://api.openai.com/v1/completions"
Const API_KEY As String = "YOUR_API_KEY_HERE"
Sub SendPostRequest()
On Error GoTo SendPostRequest_Error
Dim CodeModule As CodeModule
Set CodeModule = Application.VBE.ActiveCodePane.CodeModule
Dim lnglineCount As Long
' Set the API endpoint
Dim endpoint As String
endpoint = "https://api.openai.com/v1/completions"
' Set the JSON payload
Dim payload As String
payload = "{""model"": ""text-davinci-003"", ""prompt"": ""write a VBA function to bubble sort. please indent and comment the code and add error traping"", ""max_tokens"": 1000}"
' Create the HTTP request object
Dim xhr As Object
Set xhr = CreateObject("MSXML2.XMLHTTP")
' Set the request method and endpoint
xhr.Open "POST", endpoint, False
' Set the request headers
xhr.setRequestHeader "Content-Type", "application/json"
xhr.setRequestHeader "Authorization", "Bearer " & API_KEY
' Send the request with the payload
xhr.Send payload
lnglineCount = CodeModule.CountOfLines
CodeModule.InsertLines lineCount + 1, ExtractTextElement(xhr.responseText)
Debug.Print "Finished"
On Error GoTo 0
Exit Sub
SendPostRequest_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SendPostRequest, line " & Erl & "."
End Sub
Function ExtractTextElement(strResp As String)
On Error GoTo ExtractTextElement_Error
Dim responseText As String
Dim json As Object
responseText = strResp
Set json = JsonConverter.ParseJson(responseText)
Dim textElement As String
textElement = json("choices")(1)("text")
ExtractTextElement = textElement
On Error GoTo 0
Exit Function
ExtractTextElement_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ExtractTextElement, line " & Erl & "."
End Function
您可以使用免费的本机 Excel 插件,例如https://text2data.com/ChatGPT-in-Excel