rakyll/openai-go

OpenAI Audio: Timeout exceeded

Darwinium opened this issue · 5 comments

Hey there,

I'm trying to use CreateTranscription

ctx    = context.Background()
	client = audio.NewClient(openai.NewSession(conf.openAiToken), "")
	resp, err := client.CreateTranscription(ctx, &audio.CreateTranscriptionParams{
		Language:    "en",
		Audio:       aFile,
		AudioFormat: "mp3",
	})

and I got the following error:
Failed to complete: error making a request: Post "https://api.openai.com/v1/audio/transcriptions": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

Looks like it is because the uploading of the audio file happens, and it takes some time. When the file is 1-3MB, then it works, but when I try 7+MB then, I get the timeout error.

Please advise how to fix that moment. I tried to find a solution, but the only one that I found was to change the timeout

httpClient := &http.Client{
    Timeout: time.Minute * 5, // Set a timeout of 5 minutes
}

But I couldn't figure out how then use httpClient and how to pass it to further.

Thanks in advance 🙏

You can set a custom HTTP client as below:

ctx := context.Background()

s := openai.NewSession(os.Getenv("OPENAI_API_KEY"))
s.HTTPClient = &http.Client{
	Timeout: time.Minute * 5, // Set a timeout of 5 minutes
}

client = audio.NewClient(s, "")
// Make a request with the client

Thank you @RussellLuo, it works!

I have also created a helper function that calculates the timeout based on the internet speed and the size of the file.

By the way, I noticed that there is no "prompt" support for audio. Do you have any plans to add this feature, or would it be better for me to fork and work on it myself?

cc @marwan-at-work for the question about prompting.

It looks like prompt is just a string that can be easily added to the current client according to https://platform.openai.com/docs/api-reference/audio/create

I don't see a reason for not adding it to the library's request struct, same goes for the other input parameters.