14
type StatusError struct {
17
ErrorMessage string `json:"error"`
20
func (e StatusError) Error() string {
22
case e.Status != "" && e.ErrorMessage != "":
23
return fmt.Sprintf("%s: %s", e.Status, e.ErrorMessage)
26
case e.ErrorMessage != "":
29
// this should not happen
30
return "something went wrong, please see the ollama server logs for details"
36
type GenerateRequest struct {
37
Model string `json:"model"`
38
Prompt string `json:"prompt"`
39
System string `json:"system"`
40
Template string `json:"template"`
41
Context []int `json:"context,omitempty"`
42
Stream *bool `json:"stream,omitempty"`
43
Raw bool `json:"raw,omitempty"`
44
Format string `json:"format"`
45
KeepAlive *Duration `json:"keep_alive,omitempty"`
46
Images []ImageData `json:"images,omitempty"`
48
Options map[string]interface{} `json:"options"`
51
type ChatRequest struct {
52
Model string `json:"model"`
53
Messages []Message `json:"messages"`
54
Stream *bool `json:"stream,omitempty"`
55
Format string `json:"format"`
56
KeepAlive *Duration `json:"keep_alive,omitempty"`
58
Options map[string]interface{} `json:"options"`
62
Role string `json:"role"` // one of ["system", "user", "assistant"]
63
Content string `json:"content"`
64
Images []ImageData `json:"images,omitempty"`
67
type ChatResponse struct {
68
Model string `json:"model"`
69
CreatedAt time.Time `json:"created_at"`
70
Message Message `json:"message"`
72
Done bool `json:"done"`
78
TotalDuration time.Duration `json:"total_duration,omitempty"`
79
LoadDuration time.Duration `json:"load_duration,omitempty"`
80
PromptEvalCount int `json:"prompt_eval_count,omitempty"`
81
PromptEvalDuration time.Duration `json:"prompt_eval_duration,omitempty"`
82
EvalCount int `json:"eval_count,omitempty"`
83
EvalDuration time.Duration `json:"eval_duration,omitempty"`
86
// Options specified in GenerateRequest, if you add a new option here add it to the API docs also
90
// Predict options used at runtime
91
NumKeep int `json:"num_keep,omitempty"`
92
Seed int `json:"seed,omitempty"`
93
NumPredict int `json:"num_predict,omitempty"`
94
TopK int `json:"top_k,omitempty"`
95
TopP float32 `json:"top_p,omitempty"`
96
TFSZ float32 `json:"tfs_z,omitempty"`
97
TypicalP float32 `json:"typical_p,omitempty"`
98
RepeatLastN int `json:"repeat_last_n,omitempty"`
99
Temperature float32 `json:"temperature,omitempty"`
100
RepeatPenalty float32 `json:"repeat_penalty,omitempty"`
101
PresencePenalty float32 `json:"presence_penalty,omitempty"`
102
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
103
Mirostat int `json:"mirostat,omitempty"`
104
MirostatTau float32 `json:"mirostat_tau,omitempty"`
105
MirostatEta float32 `json:"mirostat_eta,omitempty"`
106
PenalizeNewline bool `json:"penalize_newline,omitempty"`
107
Stop []string `json:"stop,omitempty"`
110
// Runner options which must be set when the model is loaded into memory
112
UseNUMA bool `json:"numa,omitempty"`
113
NumCtx int `json:"num_ctx,omitempty"`
114
NumBatch int `json:"num_batch,omitempty"`
115
NumGQA int `json:"num_gqa,omitempty"`
116
NumGPU int `json:"num_gpu,omitempty"`
117
MainGPU int `json:"main_gpu,omitempty"`
118
LowVRAM bool `json:"low_vram,omitempty"`
119
F16KV bool `json:"f16_kv,omitempty"`
120
LogitsAll bool `json:"logits_all,omitempty"`
121
VocabOnly bool `json:"vocab_only,omitempty"`
122
UseMMap bool `json:"use_mmap,omitempty"`
123
UseMLock bool `json:"use_mlock,omitempty"`
124
RopeFrequencyBase float32 `json:"rope_frequency_base,omitempty"`
125
RopeFrequencyScale float32 `json:"rope_frequency_scale,omitempty"`
126
NumThread int `json:"num_thread,omitempty"`
129
type EmbeddingRequest struct {
130
Model string `json:"model"`
131
Prompt string `json:"prompt"`
132
KeepAlive *Duration `json:"keep_alive,omitempty"`
134
Options map[string]interface{} `json:"options"`
137
type EmbeddingResponse struct {
138
Embedding []float64 `json:"embedding"`
141
type CreateRequest struct {
142
Model string `json:"model"`
143
Path string `json:"path"`
144
Modelfile string `json:"modelfile"`
145
Stream *bool `json:"stream,omitempty"`
147
// Name is deprecated, see Model
148
Name string `json:"name"`
151
type DeleteRequest struct {
152
Model string `json:"model"`
154
// Name is deprecated, see Model
155
Name string `json:"name"`
158
type ShowRequest struct {
159
Model string `json:"model"`
160
System string `json:"system"`
161
Template string `json:"template"`
163
Options map[string]interface{} `json:"options"`
165
// Name is deprecated, see Model
166
Name string `json:"name"`
169
type ShowResponse struct {
170
License string `json:"license,omitempty"`
171
Modelfile string `json:"modelfile,omitempty"`
172
Parameters string `json:"parameters,omitempty"`
173
Template string `json:"template,omitempty"`
174
System string `json:"system,omitempty"`
175
Details ModelDetails `json:"details,omitempty"`
176
Messages []Message `json:"messages,omitempty"`
179
type CopyRequest struct {
180
Source string `json:"source"`
181
Destination string `json:"destination"`
184
type PullRequest struct {
185
Model string `json:"model"`
186
Insecure bool `json:"insecure,omitempty"`
187
Username string `json:"username"`
188
Password string `json:"password"`
189
Stream *bool `json:"stream,omitempty"`
191
// Name is deprecated, see Model
192
Name string `json:"name"`
195
type ProgressResponse struct {
196
Status string `json:"status"`
197
Digest string `json:"digest,omitempty"`
198
Total int64 `json:"total,omitempty"`
199
Completed int64 `json:"completed,omitempty"`
202
type PushRequest struct {
203
Model string `json:"model"`
204
Insecure bool `json:"insecure,omitempty"`
205
Username string `json:"username"`
206
Password string `json:"password"`
207
Stream *bool `json:"stream,omitempty"`
209
// Name is deprecated, see Model
210
Name string `json:"name"`
213
type ListResponse struct {
214
Models []ModelResponse `json:"models"`
217
type ModelResponse struct {
218
Name string `json:"name"`
219
Model string `json:"model"`
220
ModifiedAt time.Time `json:"modified_at"`
221
Size int64 `json:"size"`
222
Digest string `json:"digest"`
223
Details ModelDetails `json:"details,omitempty"`
226
type TokenResponse struct {
227
Token string `json:"token"`
230
type GenerateResponse struct {
231
Model string `json:"model"`
232
CreatedAt time.Time `json:"created_at"`
233
Response string `json:"response"`
235
Done bool `json:"done"`
236
Context []int `json:"context,omitempty"`
241
type ModelDetails struct {
242
ParentModel string `json:"parent_model"`
243
Format string `json:"format"`
244
Family string `json:"family"`
245
Families []string `json:"families"`
246
ParameterSize string `json:"parameter_size"`
247
QuantizationLevel string `json:"quantization_level"`
250
func (m *Metrics) Summary() {
251
if m.TotalDuration > 0 {
252
fmt.Fprintf(os.Stderr, "total duration: %v\n", m.TotalDuration)
255
if m.LoadDuration > 0 {
256
fmt.Fprintf(os.Stderr, "load duration: %v\n", m.LoadDuration)
259
if m.PromptEvalCount > 0 {
260
fmt.Fprintf(os.Stderr, "prompt eval count: %d token(s)\n", m.PromptEvalCount)
263
if m.PromptEvalDuration > 0 {
264
fmt.Fprintf(os.Stderr, "prompt eval duration: %s\n", m.PromptEvalDuration)
265
fmt.Fprintf(os.Stderr, "prompt eval rate: %.2f tokens/s\n", float64(m.PromptEvalCount)/m.PromptEvalDuration.Seconds())
269
fmt.Fprintf(os.Stderr, "eval count: %d token(s)\n", m.EvalCount)
272
if m.EvalDuration > 0 {
273
fmt.Fprintf(os.Stderr, "eval duration: %s\n", m.EvalDuration)
274
fmt.Fprintf(os.Stderr, "eval rate: %.2f tokens/s\n", float64(m.EvalCount)/m.EvalDuration.Seconds())
278
var ErrInvalidOpts = fmt.Errorf("invalid options")
280
func (opts *Options) FromMap(m map[string]interface{}) error {
281
valueOpts := reflect.ValueOf(opts).Elem() // names of the fields in the options struct
282
typeOpts := reflect.TypeOf(opts).Elem() // types of the fields in the options struct
284
// build map of json struct tags to their types
285
jsonOpts := make(map[string]reflect.StructField)
286
for _, field := range reflect.VisibleFields(typeOpts) {
287
jsonTag := strings.Split(field.Tag.Get("json"), ",")[0]
289
jsonOpts[jsonTag] = field
293
invalidOpts := []string{}
294
for key, val := range m {
295
if opt, ok := jsonOpts[key]; ok {
296
field := valueOpts.FieldByName(opt.Name)
297
if field.IsValid() && field.CanSet() {
302
switch field.Kind() {
304
switch t := val.(type) {
308
// when JSON unmarshals numbers, it uses float64, not int
309
field.SetInt(int64(t))
311
return fmt.Errorf("option %q must be of type integer", key)
314
val, ok := val.(bool)
316
return fmt.Errorf("option %q must be of type boolean", key)
319
case reflect.Float32:
320
// JSON unmarshals to float64
321
val, ok := val.(float64)
323
return fmt.Errorf("option %q must be of type float32", key)
327
val, ok := val.(string)
329
return fmt.Errorf("option %q must be of type string", key)
333
// JSON unmarshals to []interface{}, not []string
334
val, ok := val.([]interface{})
336
return fmt.Errorf("option %q must be of type array", key)
338
// convert []interface{} to []string
339
slice := make([]string, len(val))
340
for i, item := range val {
341
str, ok := item.(string)
343
return fmt.Errorf("option %q must be of an array of strings", key)
347
field.Set(reflect.ValueOf(slice))
349
return fmt.Errorf("unknown type loading config params: %v", field.Kind())
353
invalidOpts = append(invalidOpts, key)
357
if len(invalidOpts) > 0 {
358
return fmt.Errorf("%w: %v", ErrInvalidOpts, strings.Join(invalidOpts, ", "))
363
func DefaultOptions() Options {
365
// options set on request to runner
375
PresencePenalty: 0.0,
376
FrequencyPenalty: 0.0,
380
PenalizeNewline: true,
384
// options set when the model is loaded
386
RopeFrequencyBase: 10000.0,
387
RopeFrequencyScale: 1.0,
389
NumGPU: -1, // -1 here indicates that NumGPU should be set dynamically
391
NumThread: 0, // let the runtime decide
401
type Duration struct {
405
func (d *Duration) UnmarshalJSON(b []byte) (err error) {
407
if err := json.Unmarshal(b, &v); err != nil {
411
d.Duration = 5 * time.Minute
413
switch t := v.(type) {
416
d.Duration = time.Duration(math.MaxInt64)
418
d.Duration = time.Duration(t * float64(time.Second))
421
d.Duration, err = time.ParseDuration(t)
426
d.Duration = time.Duration(math.MaxInt64)
433
// FormatParams converts specified parameter options to their correct types
434
func FormatParams(params map[string][]string) (map[string]interface{}, error) {
436
valueOpts := reflect.ValueOf(&opts).Elem() // names of the fields in the options struct
437
typeOpts := reflect.TypeOf(opts) // types of the fields in the options struct
439
// build map of json struct tags to their types
440
jsonOpts := make(map[string]reflect.StructField)
441
for _, field := range reflect.VisibleFields(typeOpts) {
442
jsonTag := strings.Split(field.Tag.Get("json"), ",")[0]
444
jsonOpts[jsonTag] = field
448
out := make(map[string]interface{})
449
// iterate params and set values based on json struct tags
450
for key, vals := range params {
451
if opt, ok := jsonOpts[key]; !ok {
452
return nil, fmt.Errorf("unknown parameter '%s'", key)
454
field := valueOpts.FieldByName(opt.Name)
455
if field.IsValid() && field.CanSet() {
456
switch field.Kind() {
457
case reflect.Float32:
458
floatVal, err := strconv.ParseFloat(vals[0], 32)
460
return nil, fmt.Errorf("invalid float value %s", vals)
463
out[key] = float32(floatVal)
465
intVal, err := strconv.ParseInt(vals[0], 10, 64)
467
return nil, fmt.Errorf("invalid int value %s", vals)
472
boolVal, err := strconv.ParseBool(vals[0])
474
return nil, fmt.Errorf("invalid bool value %s", vals)
481
// TODO: only string slices are supported right now
484
return nil, fmt.Errorf("unknown type %s for %s", field.Kind(), key)