Using GPT-4 on Excel (Using a simple prompt on a cell for now)

Hi, I want to use GPT-4 on Excel Data but all the extensions that try to do that cost money and are not exactly what I’m looking for. I just wanted for now with this topic to create a VBA Function to have a working prompt inside an Excel Cell.

For the test I tried using gpt-3.5-turbo instead of gpt-4 because it was not working.

Thanks in advance !

Here is the VBA Function :

Function GetGPT4Response(prompt As String) As String
Dim http As Object
Set http = CreateObject(“WinHTTP.WinHTTPRequest.5.1”)

Dim apiKey As String
apiKey = "API-KEY" ' Ensure to replace this with your actual API key

' API URL Parameter
Dim apiUrl As String
apiUrl = "https://api.openai.com/v1/engines/gpt-3.5-turbo/completions"

' Request parameter
Dim postData As String
postData = "{""prompt"": """ & prompt & """, ""max_tokens"": 50}"

Debug.Print "API URL: " & apiUrl
Debug.Print "Post Data: " & postData

' Setting up the HTTP request
With http
    .Open "POST", apiUrl, False
    .SetRequestHeader "Content-Type", "application/json"
    .SetRequestHeader "Authorization", "Bearer " & apiKey
    .Send postData
    
    Debug.Print "Request Sent"
    
    ' Handling the response
    If .Status = 200 Then
        Debug.Print "Response: " & .ResponseText
        GetGPT4Response = .ResponseText
    Else
        Debug.Print "Error: " & .Status & " " & .StatusText
        GetGPT4Response = "Error: " & .Status & " " & .StatusText
    End If
End With

End Function

The error I’m getting is : “Error: 404 Not Found” in the cell

Hi! And welcome to the forum.

It looks like you have the wrong URL for the API endpoint.

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

According to OpenAI Platform

Hi !
Thank you for the answer, really helped me !

Zoltan

1 Like