|
1 | 1 | package llm
|
2 | 2 |
|
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
3 | 8 | ///////////////////////////////////////////////////////////////////////////////
|
4 | 9 | // TYPES
|
5 | 10 |
|
6 | 11 | // A generic option type, which can set options on an agent or session
|
7 |
| -type Opt func(any) error |
| 12 | +type Opt func(*Opts) error |
| 13 | + |
| 14 | +// set of options |
| 15 | +type Opts struct { |
| 16 | + agents map[string]Agent // Set of agents |
| 17 | + toolkit ToolKit // Toolkit for tools |
| 18 | + callback func(Context) // Streaming callback |
| 19 | + attachments []*Attachment // Attachments |
| 20 | + options map[string]any // Additional options |
| 21 | +} |
| 22 | + |
| 23 | +//////////////////////////////////////////////////////////////////////////////// |
| 24 | +// LIFECYCLE |
| 25 | + |
| 26 | +// ApplyOpts returns a structure of options |
| 27 | +func ApplyOpts(opts ...Opt) (*Opts, error) { |
| 28 | + o := new(Opts) |
| 29 | + o.agents = make(map[string]Agent) |
| 30 | + o.options = make(map[string]any) |
| 31 | + for _, opt := range opts { |
| 32 | + if err := opt(o); err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + } |
| 36 | + return o, nil |
| 37 | +} |
| 38 | + |
| 39 | +/////////////////////////////////////////////////////////////////////////////// |
| 40 | +// PUBLIC METHODS - PROPERTIES |
| 41 | + |
| 42 | +// Return the set of tools |
| 43 | +func (o *Opts) ToolKit() ToolKit { |
| 44 | + return o.toolkit |
| 45 | +} |
| 46 | + |
| 47 | +// Return the stream function |
| 48 | +func (o *Opts) StreamFn() func(Context) { |
| 49 | + return o.callback |
| 50 | +} |
| 51 | + |
| 52 | +// Return the array of registered agents |
| 53 | +func (o *Opts) Agents() []Agent { |
| 54 | + result := make([]Agent, 0, len(o.agents)) |
| 55 | + for _, agent := range o.agents { |
| 56 | + result = append(result, agent) |
| 57 | + } |
| 58 | + return result |
| 59 | +} |
| 60 | + |
| 61 | +// Return attachments |
| 62 | +func (o *Opts) Attachments() []*Attachment { |
| 63 | + return o.attachments |
| 64 | +} |
| 65 | + |
| 66 | +// Set an option value |
| 67 | +func (o *Opts) Set(key string, value any) { |
| 68 | + o.options[key] = value |
| 69 | +} |
| 70 | + |
| 71 | +// Get an option value |
| 72 | +func (o *Opts) Get(key string) any { |
| 73 | + if value, exists := o.options[key]; exists { |
| 74 | + return value |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Has an option value |
| 80 | +func (o *Opts) Has(key string) bool { |
| 81 | + _, exists := o.options[key] |
| 82 | + return exists |
| 83 | +} |
| 84 | + |
| 85 | +// Get an option value as a string |
| 86 | +func (o *Opts) GetString(key string) string { |
| 87 | + if value, exists := o.options[key]; exists { |
| 88 | + if v, ok := value.(string); ok { |
| 89 | + return v |
| 90 | + } |
| 91 | + } |
| 92 | + return "" |
| 93 | +} |
| 94 | + |
| 95 | +// Get an option value as a boolean |
| 96 | +func (o *Opts) GetBool(key string) bool { |
| 97 | + if value, exists := o.options[key]; exists { |
| 98 | + if v, ok := value.(bool); ok { |
| 99 | + return v |
| 100 | + } |
| 101 | + } |
| 102 | + return false |
| 103 | +} |
| 104 | + |
| 105 | +// Get an option value as a duration |
| 106 | +func (o *Opts) GetDuration(key string) time.Duration { |
| 107 | + if value, exists := o.options[key]; exists { |
| 108 | + if v, ok := value.(time.Duration); ok { |
| 109 | + return v |
| 110 | + } |
| 111 | + } |
| 112 | + return 0 |
| 113 | +} |
| 114 | + |
| 115 | +/////////////////////////////////////////////////////////////////////////////// |
| 116 | +// PUBLIC METHODS - SET OPTIONS |
| 117 | + |
| 118 | +// Set toolkit of tools |
| 119 | +func WithToolKit(toolkit ToolKit) Opt { |
| 120 | + return func(o *Opts) error { |
| 121 | + o.toolkit = toolkit |
| 122 | + return nil |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Set chat streaming function |
| 127 | +func WithStream(fn func(Context)) Opt { |
| 128 | + return func(o *Opts) error { |
| 129 | + o.callback = fn |
| 130 | + return nil |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// Set agent |
| 135 | +func WithAgent(agent Agent) Opt { |
| 136 | + return func(o *Opts) error { |
| 137 | + // Check parameters |
| 138 | + if agent == nil { |
| 139 | + return ErrBadParameter.With("withAgent") |
| 140 | + } |
| 141 | + |
| 142 | + // Add agent |
| 143 | + name := agent.Name() |
| 144 | + if _, exists := o.agents[name]; exists { |
| 145 | + return ErrConflict.Withf("Agent %q already exists", name) |
| 146 | + } else { |
| 147 | + o.agents[name] = agent |
| 148 | + } |
| 149 | + |
| 150 | + // Return success |
| 151 | + return nil |
| 152 | + } |
| 153 | + |
| 154 | +} |
| 155 | + |
| 156 | +// Create an attachment |
| 157 | +func WithAttachment(r io.Reader) Opt { |
| 158 | + return func(o *Opts) error { |
| 159 | + if attachment, err := ReadAttachment(r); err != nil { |
| 160 | + return err |
| 161 | + } else { |
| 162 | + o.attachments = append(o.attachments, attachment) |
| 163 | + } |
| 164 | + return nil |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// The temperature of the model. Increasing the temperature will make the model answer more creatively. |
| 169 | +func WithTemperature(v float64) Opt { |
| 170 | + return func(o *Opts) error { |
| 171 | + if v < 0.0 || v > 1.0 { |
| 172 | + return ErrBadParameter.With("temperature must be between 0.0 and 1.0") |
| 173 | + } |
| 174 | + o.Set("temperature", v) |
| 175 | + return nil |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while |
| 180 | +// a lower value (e.g., 0.5) will generate more focused and conservative text. |
| 181 | +func WithTopP(v float64) Opt { |
| 182 | + return func(o *Opts) error { |
| 183 | + if v < 0.0 || v > 1.0 { |
| 184 | + return ErrBadParameter.With("top_p must be between 0.0 and 1.0") |
| 185 | + } |
| 186 | + o.Set("top_p", v) |
| 187 | + return nil |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more |
| 192 | +// diverse answers, while a lower value (e.g. 10) will be more conservative. |
| 193 | +func WithTopK(v uint) Opt { |
| 194 | + return func(o *Opts) error { |
| 195 | + o.Set("top_k", v) |
| 196 | + return nil |
| 197 | + } |
| 198 | +} |
0 commit comments