Dear Team,
I followed the course " Integrate ChatGPT into Excel using Macros and APIs". Here is the code:
Option Explicit
Sub GetOpenAIResponse()
Dim promptCell As Range
Dim outputCell As Range
Dim promptText As String
'Check if a cell selected
If TypeName(Selection) <> "Range" Then
MsgBox "Please select a cell"
Exit Sub
End If
Set promptCell = Selection
Set outputCell = promptCell.Offset(0, 1)
promptText = promptCell.Value
Dim responseText As String
responseText = CallOpenAI(promptText)
outputCell.Value = responseText
End Sub
Function CallOpenAI(prompt As String) As String
' API endpoint and key
Dim apiUrl As String
Dim apiKey As String
Dim requestBody As String
Dim xmlhttp As MSXML2.XMLHTTP60
Dim response As String
apiUrl = "https://api.openai.com/v1/chat/completions"
apiKey = " My API Key"
' Prepare API request payload
requestBody = "{""prompt"": """ & prompt & """, ""model"": ""text-davinci-003"", ""max_tokens"": 300, ""n"":1, ""stop"":null, ""temperature"":1}"
Set xmlhttp = New MSXML2.XMLHTTP60
' Send POST request to the API
With xmlhttp
.Open "POST", apiUrl, False
.setRequestHeader "Content-Type", "application/json"
.setRequestHeader "Authorization", "Bearer " & apiKey
.send requestBody
response = .responseText
End With
' Extract the response from the API
MsgBox (response)
Dim JSON As Object
Set JSON = ParseJson(response)
MsgBox (JSON("choice")(1)("text"))
CallOpenAI = JSON("choice")(1)("text")
End Function
When running, excel always asks:
JsonConverter.bas was imported as recommended in the course
How to fix this issue. Thank you for your help.