-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.go
133 lines (110 loc) · 2.9 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package gollama
import (
"errors"
"fmt"
)
func (c *Gollama) ListModels() ([]ModelInfo, error) {
type tagsResponse struct {
Models []ModelInfo `json:"models"`
}
var r tagsResponse
c.apiGet("/api/tags", &r)
return r.Models, nil
}
// HasModel checks if a given model is available on the server.
//
// The function will return an error if the request fails.
//
// The function will return false if the model is not found on the server.
func (c *Gollama) HasModel(model string) (bool, error) {
models, err := c.ListModels()
if err != nil {
return false, err
}
for _, m := range models {
if m.Model == model || m.Model == model+":latest" {
return true, nil
}
}
return false, nil
}
// ModelSize returns the size of a model on the server.
//
// The function will return an error if the model is not found.
//
// The function will return 0 if the model is not found.
func (c *Gollama) ModelSize(model string) (int, error) {
models, err := c.ListModels()
if err != nil {
return 0, err
}
for _, m := range models {
if m.Model == model || m.Model == model+":latest" {
return m.Size, nil
}
}
return 0, errors.New("model not found")
}
// PullModel pulls a model from the server if it is not available locally.
//
// The function will return an error if the request fails.
//
// The function will return an error if the model is not found on the server.
func (c *Gollama) PullModel(model string) error {
req := pullRequest{
Model: model,
Stream: false,
}
var resp pullResponse
c.apiPost("/api/pull", &resp, req)
if resp.Status != "success" {
return fmt.Errorf("failed to pull model %s", model)
}
return nil
}
// PullIfMissing pulls a model from the server if it is not available locally.
//
// The function will return an error if the request fails.
//
// The function will return an error if the model is not found on the server.
//
// If no model is specified, the model name set in the Gollama object is used.
func (c *Gollama) PullIfMissing(model ...string) error {
if len(model) == 0 {
model = []string{c.ModelName}
}
for _, m := range model {
hasModel, err := c.HasModel(m)
if err != nil {
return err
}
if !hasModel {
return c.PullModel(m)
}
}
return nil
}
// GetDetails retrieves the details of specified models from the server.
//
// The function accepts a variadic parameter of model names. If no model names are provided,
// it defaults to using the model name set in the Gollama object.
//
// It returns a slice of ModelDetails for each requested model, or an error if the request fails.
func (c *Gollama) GetDetails(model ...string) ([]ModelDetails, error) {
if len(model) == 0 {
model = []string{c.ModelName}
}
ret := make([]ModelDetails, 0)
for _, m := range model {
req := showRequest{
Model: m,
}
var resp ModelDetails
err := c.apiPost("/api/show", &resp, req)
if err != nil {
return nil, err
}
ret = append(ret, resp)
}
return ret, nil
}