Here is the major part of the code in golang,
const (
maxTokens = 3000
temperature = 0.7
engine = gpt3.TextDavinci003Engine
)
func GetAnswer(question string) (reply string, ok bool) {
fmt.Print("Bot: ")
ok = false
reply = “”
i := 0
ctx := context.Background()
if err := client.CompletionStreamWithEngine(ctx, engine, gpt3.CompletionRequest{
Prompt: string{
question,
},
MaxTokens: gpt3.IntPtr(maxTokens),
Temperature: gpt3.Float32Ptr(temperature),
}, func(resp *gpt3.CompletionResponse) {
if i > 1 {
fmt.Print(resp.Choices[0].Text)
reply += resp.Choices[0].Text
}
i++
}); err != nil {
log.Fatalln(err)
}
if reply != “” {
ok = true
}
fmt.Println()
return reply, ok
}
When I call the function GetAnswer, I got an error : Post “https://api.openai.com/v1/engines/text-davinci-003/completions ”: context deadline exceeded (Client.Timeout exceeded while awaiting headers). What could be wrong? BTW, the same code used to work for a quite period of time. Thanks.
FYI:
Did I misread your question, @fifthwheel8 ?
Hope this helps.
Thanks for your reply. But I don’t quite understand what you meant. So is there anything from my original post that made you confused?
It was just hard to read because you did not format your code using indentation and Markdown so your code was easy to read.
Normally, to post code and data, we should post like this, using Markdown triple backticks:
```
# your properly intended, formatted code here
```
Don’t worry, it seems like 90% + of the members here have never formatted code or posted code in a forum which uses Markdown.
However, it does make code and data hard to read with members do not post using Markdown and format their code.
You can search the net for “markdown cheat cheat” if you are interested to learn more.
Hope this helps.
Thanks for reminding me. I will definitely format my code when I post my code next time. But for now I still keep getting the error: “Post “https://api.openai.com/v1/engines/text-davinci-003/completions ”: context deadline exceeded (Client.Timeout exceeded while awaiting headers)”, what might be the problem? Thanks.
The engines endpoint has been depreciated
Instead you need to pass it as a model name in your parameters
The url you are using is wrong
I re-post my code in the following,
const (
maxTokens = 3000
temperature = 0.7
engine = gpt3.TextDavinci003Engine
)
func GetAnswer(question string) (reply string, ok bool) {
fmt.Print("Bot: ")
ok = false
reply = ""
i := 0
ctx := context.Background()
if err := client.CompletionStreamWithEngine(ctx, engine, gpt3.CompletionRequest{
Prompt: []string{
question,
},
MaxTokens: gpt3.IntPtr(maxTokens),
Temperature: gpt3.Float32Ptr(temperature),
}, func(resp *gpt3.CompletionResponse) {
if i > 1 {
fmt.Print(resp.Choices[0].Text)
reply += resp.Choices[0].Text
}
i++
}); err != nil {
log.Fatalln(err)
}
if reply != "" {
ok = true
}
fmt.Println()
return reply, ok
}
Would you please be more specific pointing out what url is wrong and what is supposed to use model name in my code? Thanks and I really appreciate it.
I assume you are using a library of some sort and didn’t code the function being called in the line below
client.CompletionStreamWithEngine …
If you are using a client library, your error indicates that it is calling an old endpoint
I would check the library for later versions before proceeding further. You can post a link to the GitHub or page you got the library from if you are unsure.
This code is deprecated because the API endpoint is deprecated.
If you do a quick Google search (and limited the time to the past week), you will find many examples of golang
code which works and you can use the more updated code example(s):
For Example
package main
import (
"context"
"fmt"
"os"
"github.com/otiai10/openaigo"
)
func main() {
client := openaigo.NewClient(os.Getenv("OPENAI_API_KEY"))
request := openaigo.ChatCompletionRequestBody{
Model: "gpt-3.5-turbo",
Messages: []openaigo.ChatMessage{
{Role: "user", Content: "Hello!"},
},
}
ctx := context.Background()
response, err := client.Chat(ctx, request)
fmt.Println(response, err)
}
HTH
Reference:
OpenAI GPT3/3.5 and GPT4 ChatGPT API Client Library for Go, simple, less dependencies, and well-tested - GitHub - otiai10/openaigo: OpenAI GPT3/3.5 and GPT4 ChatGPT API Client Library for Go, simpl...
1 Like
Yes, I did use a library and this is the link: GitHub - PullRequestInc/go-gpt3: An OpenAI GPT-3 API client enabling Go/Golang programs to interact with the gpt3 APIs. . Please let me know if there is anything deprecated I should be aware of. Thanks.
Thanks for the references. I tried to use them in my code,
func getResponse(ctx context.Context, client openaigo.Client, question string) (response string, err error) {
request := openaigo.ChatCompletionRequestBody{
Model: "gpt-3.5-turbo",
Messages: []openaigo.ChatMessage{
{Role: "user", Content: question},
},
}
resp, err := client.Chat(ctx, request)
if err != nil {
return "", err
}
response = resp.Choices[0].Message.Content
return response, nil
}
But unfortunately, I got another error: Post “https://api.openai.com/v1/chat/completions ”: context deadline exceeded. I have no idea what went wrong.
Yeah, sorry. I am not a Golang programmer.
Maybe someone else can assist you.
I looked at the source code and most of the endpoints that library is calling have been depreciated
It looks like they have just added the chatcompletion endpoint
Unfortunately you will have to ask them for help. It looks like an old library that may have been revived for chat
All of the engine, search and plain completion functions look like they are no longer valid
Can you look at this one, GitHub - otiai10/openaigo: OpenAI GPT3/3.5 ChatGPT API Client Library for Go, simple, less dependencies, and well-tested and also my code,
func getResponse(ctx context.Context, client openaigo.Client, question string) (response string, err error) {
request := openaigo.ChatCompletionRequestBody{
Model: "gpt-3.5-turbo",
Messages: []openaigo.ChatMessage{
{Role: "user", Content: question},
},
}
resp, err := client.Chat(ctx, request)
if err != nil {
return "", err
}
response = resp.Choices[0].Message.Content
return response, nil
}
I got another error: Post “https://api.openai.com/v1/chat/completions ”: context deadline exceeded
Where is your code which initializes the OpenAI client with the OPENAI_API_KEY
, @fifthwheel8 ?
Here is how I initialized the OpenAI client with OPENAI_API_KEY,
apiKey, ok := os.LookupEnv("CHATGPT_API_KEY")
if !ok {
panic("Missing API_KEY")
}
client := openaigo.NewClient(apiKey)
1 Like
It says
Post “https://api.openai.com/v1/chat/completions”: context deadline exceeded
which means your context cannot wait for the response coming from OpenAI, otherwise, your VPN or proxy took too long to handle the request.
Please check either of followings:
Try using context.Background()
so that it can wait forever.
Try using other pubic network to clarify your network doesn’t cause the issue.
Try setting client.HTTPClient
with setting Transport.Proxy
if you use VPN (please check here for reference)
Hope it helps.
OpenAI GPT3/3.5 ChatGPT API Client Library for Go, simple, less dependencies, and well-tested - GitHub - otiai10/openaigo: OpenAI GPT3/3.5 ChatGPT API Client Library for Go, simple, less dependenci...
2 Likes