-
Notifications
You must be signed in to change notification settings - Fork 630
feat(go/plugins/ollama): add runtime options and thinking #4028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
simakmak
wants to merge
8
commits into
firebase:main
Choose a base branch
from
simakmak:simakmak/ollama-struct-opts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3401c69
ollama plugin: add options
simakmak c4375a2
fix: gemini-code-assist
simakmak b06df0a
fix: gemini-code-assist
simakmak ee37ee2
fix: ai.GenerationCommonConfig
simakmak 33a4437
fix: Edits according to hugoaguirre's comments
simakmak b72fa1e
fix: Edits according to hugoaguirre's comments
simakmak f287bf5
update test
simakmak 36f6209
chore: add copyright headers
simakmak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ollama | ||
|
|
||
| import ( | ||
| "errors" | ||
|
|
||
| "github.com/firebase/genkit/go/ai" | ||
| ) | ||
|
|
||
| var topLevelOpts = map[string]struct{}{ | ||
| "think": {}, | ||
| "keep_alive": {}, | ||
| } | ||
|
|
||
| // Ollama has two API endpoints, one with a chat interface and another with a generate response interface. | ||
| // That's why have multiple request interfaces for the Ollama API below. | ||
|
|
||
| /* | ||
| TODO: Support optional, advanced parameters: | ||
| format: the format to return a response in. Currently the only accepted value is json | ||
| options: additional model parameters listed in the documentation for the Modelfile such as temperature | ||
| system: system message to (overrides what is defined in the Modelfile) | ||
| template: the prompt template to use (overrides what is defined in the Modelfile) | ||
| context: the context parameter returned from a previous request to /generate, this can be used to keep a short conversational memory | ||
| stream: if false the response will be returned as a single response object, rather than a stream of objects | ||
| raw: if true no formatting will be applied to the prompt. You may choose to use the raw parameter if you are specifying a full templated prompt in your request to the API | ||
| */ | ||
| type ollamaChatRequest struct { | ||
| Messages []*ollamaMessage `json:"messages"` | ||
| Images []string `json:"images,omitempty"` | ||
| Model string `json:"model"` | ||
| Stream bool `json:"stream"` | ||
| Format string `json:"format,omitempty"` | ||
| Tools []ollamaTool `json:"tools,omitempty"` | ||
| Think any `json:"think,omitempty"` | ||
| Options map[string]any `json:"options,omitempty"` | ||
| KeepAlive string `json:"keep_alive,omitempty"` | ||
| } | ||
|
|
||
| func (o *ollamaChatRequest) ApplyOptions(cfg any) error { | ||
| if cfg == nil { | ||
| return nil | ||
| } | ||
|
|
||
| switch cfg := cfg.(type) { | ||
| case GenerateContentConfig: | ||
| o.applyGenerateContentConfig(&cfg) | ||
| return nil | ||
| case *GenerateContentConfig: | ||
| o.applyGenerateContentConfig(cfg) | ||
| return nil | ||
| case map[string]any: | ||
| return o.applyMapAny(cfg) | ||
| case *ai.GenerationCommonConfig: | ||
| return o.applyGenerationCommonConfig(cfg) | ||
| case ai.GenerationCommonConfig: | ||
| return o.applyGenerationCommonConfig(&cfg) | ||
| default: | ||
| return errors.New("unknown generation config") | ||
| } | ||
| } | ||
| func (o *ollamaChatRequest) applyGenerateContentConfig(cfg *GenerateContentConfig) { | ||
| if cfg == nil { | ||
| return | ||
| } | ||
|
|
||
| // thinking | ||
| if cfg.Think != nil { | ||
| o.Think = cfg.Think | ||
| } | ||
|
|
||
| // runtime options | ||
| opts := map[string]any{} | ||
|
|
||
| if cfg.Seed != nil { | ||
| opts["seed"] = *cfg.Seed | ||
| } | ||
| if cfg.Temperature != nil { | ||
| opts["temperature"] = *cfg.Temperature | ||
| } | ||
| if cfg.TopK != nil { | ||
| opts["top_k"] = *cfg.TopK | ||
| } | ||
| if cfg.TopP != nil { | ||
| opts["top_p"] = *cfg.TopP | ||
| } | ||
| if cfg.MinP != nil { | ||
| opts["min_p"] = *cfg.MinP | ||
| } | ||
| if len(cfg.Stop) > 0 { | ||
| opts["stop"] = cfg.Stop | ||
| } | ||
| if cfg.NumCtx != nil { | ||
| opts["num_ctx"] = *cfg.NumCtx | ||
| } | ||
| if cfg.NumPredict != nil { | ||
| opts["num_predict"] = *cfg.NumPredict | ||
| } | ||
|
|
||
| if len(opts) > 0 { | ||
| o.Options = opts | ||
| } | ||
| } | ||
| func (o *ollamaChatRequest) applyGenerationCommonConfig(cfg *ai.GenerationCommonConfig) error { | ||
| if cfg == nil { | ||
| return nil | ||
| } | ||
|
|
||
| opts := map[string]any{} | ||
|
|
||
| if cfg.MaxOutputTokens > 0 { | ||
| opts["num_predict"] = cfg.MaxOutputTokens | ||
| } | ||
| if len(cfg.StopSequences) > 0 { | ||
| opts["stop"] = cfg.StopSequences | ||
| } | ||
| if cfg.Temperature != 0 { | ||
| opts["temperature"] = cfg.Temperature | ||
| } | ||
| if cfg.TopK > 0 { | ||
| opts["top_k"] = cfg.TopK | ||
| } | ||
| if cfg.TopP > 0 { | ||
| opts["top_p"] = cfg.TopP | ||
| } | ||
|
|
||
| if len(opts) > 0 { | ||
| o.Options = opts | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (o *ollamaChatRequest) applyMapAny(m map[string]any) error { | ||
| if len(m) == 0 { | ||
| return nil | ||
| } | ||
| opts := map[string]any{} | ||
| for k, v := range m { | ||
| if _, isTopLevel := topLevelOpts[k]; isTopLevel { | ||
| switch k { | ||
| case "think": | ||
| o.Think = v | ||
| case "keep_alive": | ||
| if s, ok := v.(string); ok { | ||
| o.KeepAlive = s | ||
| } else { | ||
| return errors.New("keep_alive must be string") | ||
| } | ||
| } | ||
| continue | ||
| } | ||
| opts[k] = v | ||
| } | ||
|
|
||
| if len(opts) > 0 { | ||
| o.Options = opts | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package ollama | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/firebase/genkit/go/ai" | ||
| ) | ||
|
|
||
| func TestOllamaChatRequest_ApplyOptions(t *testing.T) { | ||
| seed := 42 | ||
| temp := 0.7 | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| cfg any | ||
| want *ollamaChatRequest | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "GenerateContentConfig pointer", | ||
| cfg: &GenerateContentConfig{ | ||
| Seed: &seed, | ||
| Temperature: &temp, | ||
| Think: true, | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Think: true, | ||
| Options: map[string]any{ | ||
| "seed": seed, | ||
| "temperature": temp, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "GenerateContentConfig value", | ||
| cfg: GenerateContentConfig{ | ||
| Seed: &seed, | ||
| Think: true, | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Think: true, | ||
| Options: map[string]any{ | ||
| "seed": seed, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "map[string]any with opts only", | ||
| cfg: map[string]any{ | ||
| "temperature": 0.5, | ||
| "top_k": 40, | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Options: map[string]any{ | ||
| "temperature": 0.5, | ||
| "top_k": 40, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "map[string]any with top level fields", | ||
| cfg: map[string]any{ | ||
| "think": true, | ||
| "keep_alive": "10m", | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Think: true, | ||
| KeepAlive: "10m", | ||
| }, | ||
| }, | ||
| { | ||
| name: "map[string]any mixed main and opts", | ||
| cfg: map[string]any{ | ||
| "temperature": 0.9, | ||
| "think": true, | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Think: true, | ||
| Options: map[string]any{ | ||
| "temperature": 0.9, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "GenerationCommonConfig pointer", | ||
| cfg: &ai.GenerationCommonConfig{ | ||
| Temperature: temp, | ||
| TopK: 1, | ||
| TopP: 2.0, | ||
| }, | ||
| want: &ollamaChatRequest{ | ||
| Options: map[string]any{ | ||
| "temperature": temp, | ||
| "top_k": 1, | ||
| "top_p": 2.0, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "nil config", | ||
| cfg: nil, | ||
| want: &ollamaChatRequest{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| req := &ollamaChatRequest{} | ||
|
|
||
| err := req.ApplyOptions(tt.cfg) | ||
|
|
||
| if tt.wantErr { | ||
| if err == nil { | ||
| t.Fatalf("expected error, got nil") | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(req, tt.want) { | ||
| t.Errorf( | ||
| "unexpected result:\nwant: %#v\n got: %#v", | ||
| tt.want, | ||
| req, | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.