Hello,
I am making http requests in excel vba - macro.
My code is simple:
Set http = CreateObject(“WinHttp.WinHttpRequest.5.1”)
…
…
apiEndpoint = “https://api.openai.com/v1/chat/completions”
…
…
With http
.Open “POST”, apiEndpoint, False
.setRequestHeader “Content-Type”, “application/json”
.setRequestHeader “Authorization”, "Bearer " & apiKey
.send requestBody
response = .responseText
End With
It works fine with model gpt-3.5-turbo but fails with model gpt-4.
gpt-4 model returns me run-rime error - ‘The operation timed out’
There is no gpt-4 response returned. The code fails at ‘.send requestBody’.
Only difference in the code is string “gpt-3.5-turbo” or “gpt-4”.
The role: system works also with gpt-4. It returns me the response that is expected.
The role : user gives the timed out - error.
What can cause this and how to solve it?
ps. I have read GPT-4 model: frequent No Reply (TimeOut) and it does not confirm to me if the problem is the same or not.
br, petri
I managed to resolve it with help of chatGPT
I had to increase the http timeout by .setTimeouts 90000, 90000, 90000, 90000.
br, petri
1 Like
ncj
3
Hi,
I have another issue and since you are doing this in VBA you might be able to help me. Seems as I am missing something in the requestBody…
I am moving from
'Const API_ENDPOINT As String = “https://api.openai.com/v1/completions”
to
Const API_ENDPOINT As String = “https://api.openai.com/v1/chat/completions”
And from:
’ Const MODEL As String = “text-davinci-003”
to:
Const MODEL As String = “gpt-4”
Dim requestBody As String
requestBody = "{" & _
"""model"": """ & MODEL & """," & _
"""prompt"": """ & prompt & """," & _
"""max_tokens"": " & MAX_TOKENS & "," & _
"""temperature"": " & TEMPERATURE & _
"}"
' Open and send the HTTP request
With httpRequest
.Open "POST", API_ENDPOINT, False
.SetRequestHeader "Content-Type", "application/json"
.SetRequestHeader "Authorization", "Bearer " & ApiKey_OpenAI
.Send (requestBody)
End With
Appreciate if you can share a snippet of your code. Thanks!
Your JSONs must look like these:
{“model”:“gpt-4”, “messages”: [{“role”:“system”,“content”: MESSAGE CONTENT}]}
{“model”:“gpt-4”, “messages”: [{“role”:“user”,“content”: MESSAGE CONTENT}]}