Hi everyone, Ryan here on the Syntax Bite. Today, we’re going to take a look at how to use the ChatGPT API from within Excel using Power Query. This will allow you to create a query in Power Query that enables you to ask questions to ChatGPT and get a response via the API.
To get started, you’ll need to sign up for the ChatGPT API and obtain an API key. This is different from the ChatGPT itself, but you can use the same account. Once you have the API key, you can proceed with the following steps.
First, open Excel and go to the Data tab. Click on ‘Get Data’ and select ‘From Other Sources’ and then ‘Blank Query’. This will open the Advanced Editor, where we’ll write our code.
I
n the Advanced Editor, we'll define the URL for the ChatGPT API and set it as a variable. We'll also define the body of our request, which includes the model (GPT 3.5 turbo) and the conversation with ChatGPT. The conversation consists of a system message (optional) and a user message.
Next, we'll send a POST request to the API endpoint using the Web.Contents function. We'll pass the URL, headers (content type and authorization), and the body of our request. The response will be stored in a variable.
To turn this into a function, we'll modify the code and add a parameter for the user's inquiry. We'll replace the user message in the body with the inquiry parameter. This will allow us to ask different questions to ChatGPT.
Once we have the function, we can use it to fill in the responses for a list of questions. We'll create a table in Excel with the questions and use Power Query to invoke the function for each question. The responses will be displayed in a new column.
That's it! Now you can easily interact with ChatGPT from within Excel using Power Query. This can be useful if you have a lot of questions or if you want to quickly get the input into Excel.
Remember to obtain your own API key and replace it in the code. Also, note that the ChatGPT API offers more features and models like Whisper and Dolly, but we focused on ChatGPT for this tutorial.
I hope you found this tutorial helpful. If you have any questions, feel free to leave them in the comments. Thanks for watching, and I'll see you in the next video!