How do ask ChatGPT with API from Excel macros vba

0 votes

I want to utilise Excel to send questions to ChatGPT and receive answers in a different cell. The API I have is shown in cell "A1." The answer should go in "A6," and the question should be removed from "A3":

  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

But I get this error back:

{ "error": { "message": "Unknown endpoint for this model.", "type": "invalid_request_error", "param": null, "code": null } }

What's wrong with this code?

Feb 11, 2023 in Others by narikkadan
• 63,700 points
1,777 views

1 answer to this question.

0 votes

A few things that will help. Don't use the Engines endpoints because it's deprecated.

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

Also the Engines endpoint does not respond to a prompt. What it does is

Lists the currently available (non-finetuned) models, and provides basic information about each one such as the owner and availability.

Instead use the Completion endpoint.

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

In order to use the API you'll have to add model to your request. Something like this.

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

Hope that helps.

Join the ChatGPT Course today and start building advanced chatbots and virtual assistants!

answered Feb 11, 2023 by Kithuzzz
• 38,010 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

How do you populate a google sheets/excel column with cells from a column in another sheet in the same document?

You have two options on chronology: sheet-by-sheet =QUERY({Sheet1!X:Z; Sheet2!X:Z; ...READ MORE

answered Dec 19, 2022 in Others by narikkadan
• 63,700 points
1,272 views
0 votes
0 answers

Download Excel sheet from .NET Core 3.1 Web API with jQuery Ajax client

I am trying to download an Excel ...READ MORE

Feb 10, 2022 in Others by Edureka
• 13,670 points
1,009 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,700 points
1,018 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
972 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,314 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
623 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
841 views
0 votes
1 answer

Excel VBA: how to find a description from an AD-group

First add the 'description' property to your ...READ MORE

answered Feb 16, 2023 in Others by Kithuzzz
• 38,010 points
656 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP