You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I have this script, when I specify option.WithRequestTimeout(20*time.Second) it doesn't run properly, but when I remove that line it works. Is this a bug or is RequestTimeout not supposed to work with Streaming?
package main
import (
"context""os""time""github.com/openai/openai-go""github.com/openai/openai-go/option"
)
// Mock function to simulate weather data retrievalfuncgetWeather(locationstring) string {
// In a real implementation, this function would call a weather APIreturn"Sunny, 25°C"
}
funcmain() {
client:=openai.NewClient(
option.WithAPIKey(os.Getenv("OPEN_AI_TOKEN")),
)
ctx:=context.Background()
question:="Begin a very brief introduction of Greece, then incorporate the local weather of a few towns"print("> ")
println(question)
println()
params:= openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
openai.UserMessage(question),
}),
Seed: openai.Int(0),
Model: openai.F(openai.ChatModelGPT4o),
Tools: openai.F([]openai.ChatCompletionToolParam{
{
Type: openai.F(openai.ChatCompletionToolTypeFunction),
Function: openai.F(openai.FunctionDefinitionParam{
Name: openai.String("get_live_weather"),
Description: openai.String("Get weather at the given location"),
Parameters: openai.F(openai.FunctionParameters{
"type": "object",
"properties": map[string]interface{}{
"location": map[string]string{
"type": "string",
},
},
"required": []string{"location"},
}),
}),
},
}),
}
stream:=client.Chat.Completions.NewStreaming(
ctx,
params,
option.WithRequestTimeout(20*time.Second),
)
acc:= openai.ChatCompletionAccumulator{}
forstream.Next() {
chunk:=stream.Current()
acc.AddChunk(chunk)
// When this fires, the current chunk value will not contain content dataifcontent, ok:=acc.JustFinishedContent(); ok {
println("Content stream finished:", content)
println()
}
iftool, ok:=acc.JustFinishedToolCall(); ok {
println("Tool call stream finished:", tool.Index, tool.Name, tool.Arguments)
println()
}
ifrefusal, ok:=acc.JustFinishedRefusal(); ok {
println("Refusal stream finished:", refusal)
println()
}
// It's best to use chunks after handling JustFinished eventsiflen(chunk.Choices) >0 {
println(chunk.Choices[0].Delta.JSON.RawJSON())
}
}
iferr:=stream.Err(); err!=nil {
panic(err)
}
// After the stream is finished, acc can be used like a ChatCompletion_=acc.Choices[0].Message.Contentprintln("Total Tokens:", acc.Usage.TotalTokens)
println("Finish Reason:", acc.Choices[0].FinishReason)
}
The text was updated successfully, but these errors were encountered:
Hello! I have this script, when I specify
option.WithRequestTimeout(20*time.Second)
it doesn't run properly, but when I remove that line it works. Is this a bug or is RequestTimeout not supposed to work with Streaming?The text was updated successfully, but these errors were encountered: