First AI generated version
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
package gollama
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/ollama/ollama/api"
|
||||
)
|
||||
|
||||
// CallOption configures a single completion or chat request.
|
||||
type CallOption func(*callConfig)
|
||||
|
||||
type callConfig struct {
|
||||
model string
|
||||
opts OptionSet
|
||||
system string
|
||||
raw bool
|
||||
keepAlive *api.Duration
|
||||
think *api.ThinkValue
|
||||
format json.RawMessage
|
||||
truncate *bool
|
||||
shift *bool
|
||||
stream *bool
|
||||
}
|
||||
|
||||
func (c *Client) mergeCallConfig(callOpts ...CallOption) (*callConfig, error) {
|
||||
cfg := &callConfig{opts: c.defaultOpts}
|
||||
for _, opt := range callOpts {
|
||||
opt(cfg)
|
||||
}
|
||||
model, err := c.resolveModel(cfg.model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.model = model
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (cfg *callConfig) optionsMap() (map[string]any, error) {
|
||||
return cfg.opts.toMap()
|
||||
}
|
||||
|
||||
func boolPtr(v bool) *bool { return &v }
|
||||
|
||||
// WithModel sets the model for a single call.
|
||||
func WithModel(name string) CallOption {
|
||||
return func(c *callConfig) { c.model = name }
|
||||
}
|
||||
|
||||
// WithOptions merges per-call generation options.
|
||||
func WithOptions(opts OptionSet) CallOption {
|
||||
return func(c *callConfig) { c.opts = c.opts.Merge(opts) }
|
||||
}
|
||||
|
||||
// WithSystem sets a system prompt for the call.
|
||||
func WithSystem(system string) CallOption {
|
||||
return func(c *callConfig) { c.system = system }
|
||||
}
|
||||
|
||||
// WithKeepAlive controls how long the model stays loaded after the request.
|
||||
func WithKeepAlive(d time.Duration) CallOption {
|
||||
return func(c *callConfig) {
|
||||
c.keepAlive = &api.Duration{Duration: d}
|
||||
}
|
||||
}
|
||||
|
||||
// WithThink enables thinking/reasoning for supported models.
|
||||
func WithThink(enabled bool) CallOption {
|
||||
return func(c *callConfig) {
|
||||
c.think = &api.ThinkValue{Value: enabled}
|
||||
}
|
||||
}
|
||||
|
||||
// WithThinkLevel sets thinking level ("high", "medium", "low", "max").
|
||||
func WithThinkLevel(level string) CallOption {
|
||||
return func(c *callConfig) {
|
||||
c.think = &api.ThinkValue{Value: level}
|
||||
}
|
||||
}
|
||||
|
||||
// WithFormatJSON requests JSON output format.
|
||||
func WithFormatJSON() CallOption {
|
||||
return func(c *callConfig) { c.format = json.RawMessage(`"json"`) }
|
||||
}
|
||||
|
||||
// WithFormatSchema requests structured output matching a JSON schema.
|
||||
func WithFormatSchema(schema any) CallOption {
|
||||
return func(c *callConfig) {
|
||||
b, err := json.Marshal(schema)
|
||||
if err == nil {
|
||||
c.format = b
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithTruncate truncates chat history when context is exceeded.
|
||||
func WithTruncate(enabled bool) CallOption {
|
||||
return func(c *callConfig) { c.truncate = &enabled }
|
||||
}
|
||||
|
||||
// WithShift shifts chat history when context is exceeded.
|
||||
func WithShift(enabled bool) CallOption {
|
||||
return func(c *callConfig) { c.shift = &enabled }
|
||||
}
|
||||
Reference in New Issue
Block a user