如何从Excel宏(vba)中使用API询问ChatGPT?

brtdzjyr  于 2023-02-14  发布在  其他
关注(0)|答案(1)|浏览(585)

我想使用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

但我得到这个错误回来:
{"错误":{"消息":"此模型的未知终结点。","类型":"无效请求错误","参数":空,"代码":空}}
这个代码有什么问题吗?谢谢!

bkhjykvo

bkhjykvo1#

有几点会有帮助。不要使用the Engines endpoints,因为它已经过时了。

GET https://api.openai.com/v1/engines/davinci/

此外,Engines端点不响应提示。
列出当前可用的(非微调)模型,并提供有关每个模型的基本信息,例如所有者和可用性。
请改用the Completion endpoint

POST https://api.openai.com/v1/completions

为了使用这个API,你必须在你的请求中添加model

{
  "model": "text-davinci-003",
  "prompt": "How are you?",
  "max_tokens": 256
}

希望能有所帮助。

相关问题