Skip to content

Commit 590ee96

Browse files
hugoaguirrehendrixmar
authored andcommitted
fix(go/plugins/ollama): add response timeout config (#3523)
1 parent 6c44fcb commit 590ee96

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

go/plugins/ollama/ollama.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (o *Ollama) DefineModel(g *genkit.Genkit, model ModelDefinition, opts *ai.M
8787
Supports: modelOpts.Supports,
8888
Versions: []string{},
8989
}
90-
gen := &generator{model: model, serverAddress: o.ServerAddress}
90+
gen := &generator{model: model, serverAddress: o.ServerAddress, timeout: o.Timeout}
9191
return genkit.DefineModel(g, api.NewName(provider, model.Name), meta, gen.generate)
9292
}
9393

@@ -111,6 +111,7 @@ type ModelDefinition struct {
111111
type generator struct {
112112
model ModelDefinition
113113
serverAddress string
114+
timeout int
114115
}
115116

116117
type ollamaMessage struct {
@@ -196,6 +197,7 @@ type ollamaModelResponse struct {
196197
// Ollama provides configuration options for the Init function.
197198
type Ollama struct {
198199
ServerAddress string // Server address of oLLama.
200+
Timeout int // Response timeout in seconds (defaulted to 30 seconds)
199201

200202
mu sync.Mutex // Mutex to control access.
201203
initted bool // Whether the plugin has been initialized.
@@ -218,6 +220,9 @@ func (o *Ollama) Init(ctx context.Context) []api.Action {
218220
panic("ollama: need ServerAddress")
219221
}
220222
o.initted = true
223+
if o.Timeout == 0 {
224+
o.Timeout = 30
225+
}
221226
return []api.Action{}
222227
}
223228

@@ -274,7 +279,7 @@ func (g *generator) generate(ctx context.Context, input *ai.ModelRequest, cb fun
274279
payload = chatReq
275280
}
276281

277-
client := &http.Client{Timeout: 30 * time.Second}
282+
client := &http.Client{Timeout: time.Duration(g.timeout) * time.Second}
278283
payloadBytes, err := json.Marshal(payload)
279284
if err != nil {
280285
return nil, err

go/samples/ollama-tools/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func main() {
4343
// Initialize Genkit with the Ollama plugin
4444
ollamaPlugin := &ollama.Ollama{
4545
ServerAddress: "http://localhost:11434", // Default Ollama server address
46+
Timeout: 60, // Response timeout in seconds
4647
}
4748

4849
g := genkit.Init(ctx, genkit.WithPlugins(ollamaPlugin))
@@ -80,7 +81,6 @@ func main() {
8081
ai.WithTools(weatherTool),
8182
ai.WithToolChoice(ai.ToolChoiceAuto),
8283
)
83-
8484
if err != nil {
8585
fmt.Printf("Error: %v\n", err)
8686
return

0 commit comments

Comments
 (0)