-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelFileRequestBuilder.go
190 lines (164 loc) · 4.71 KB
/
ModelFileRequestBuilder.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package ollama
// ModelFileRequestBuilder represents the model creation API request.
type ModelFileRequestBuilder struct {
Model *string `json:"model"`
Path *string `json:"path"`
Modelfile *string `json:"modelfile"`
Quantize *string `json:"quantize"`
Stream *bool `json:"stream"`
StreamBufferSize *int `json:"-"`
StreamFunc func(r *StatusResponse, err error) `json:"-"`
from *string
parameters []Parameter
template *string
system *string
adapter *string
license *string
messages []Message
}
// Parameter represents a parameter sent to the API,
type Parameter struct {
Key string
Value string
}
// WithModel sets the new model's name for this request.
//
// Parameters:
// - v: The model name.
func (f *CreateModelFunc) WithModel(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.Model = &v
}
}
// WithPath sets the path for this request.
//
// Parameters:
// - v: The path.
func (f *CreateModelFunc) WithPath(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.Path = &v
}
}
// WithStream passes a function to allow reading stream types.
//
// Parameters:
// - v: A boolean indicating whether to use streaming.
// - bufferSize: The size of the streamed buffer
// - fc: The function to handle streaming types.
func (f *CreateModelFunc) WithStream(v bool, bufferSize int, fc func(r *StatusResponse, err error)) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.Stream = &v
r.StreamBufferSize = &bufferSize
r.StreamFunc = fc
}
}
// WithQuantize sets the quantize for this request.
//
// Parameters:
// - v: The quantize value.
func (f *CreateModelFunc) WithQuantize(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.Quantize = &v
}
}
// WithFrom defines the base model to use.
//
// Parameters:
// - v: The base model string.
func (f *CreateModelFunc) WithFrom(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.from = &v
}
}
// WithTemplate sets the full prompt template to be sent to the model.
//
// Parameters:
// - v: The template string.
func (f *CreateModelFunc) WithTemplate(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.template = &v
}
}
// WithSystem specifies the system message that will be set in the template.
//
// Parameters:
// - v: The system message string.
func (f *CreateModelFunc) WithSystem(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.system = &v
}
}
// WithParameter appends a new parameter for how Ollama will run the model.
//
// Parameters:
// - v: The parameter to append.
func (f *CreateModelFunc) WithParameter(v Parameter) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.parameters = append(r.parameters, v)
}
}
// WithAdapter defines the (Q)LoRA adapters to apply to the model.
//
// Parameters:
// - v: The adapter string.
func (f *CreateModelFunc) WithAdapter(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.adapter = &v
}
}
// WithLicense specifies the legal license.
//
// Parameters:
// - v: The license string.
func (f *CreateModelFunc) WithLicense(v string) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.license = &v
}
}
// WithMessage appends a new message to the message history.
//
// Parameters:
// - v: The message to append.
func (f *CreateModelFunc) WithMessage(v Message) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.messages = append(r.messages, v)
}
}
// WithChat appends all the messages from a chat to the message history.
//
// Parameters:
// - chat: The chat whose messages to append.
func (f *CreateModelFunc) WithChat(chat *Chat) func(*ModelFileRequestBuilder) {
return func(r *ModelFileRequestBuilder) {
r.messages = append(r.messages, chat.Messages...)
}
}
// Build generates the ModelFile.
//
// Parameters:
// - defaultModel: The default model string.
func (m *ModelFileRequestBuilder) Build() string {
r := ""
if m.from != nil {
r += "FROM " + *m.from + "\n"
}
for _, p := range m.parameters {
r += "PARAMETER " + p.Key + " " + p.Value + "\n"
}
if m.template != nil {
r += "TEMPLATE \"\"\"" + *m.template + "\"\"\"\n"
}
if m.system != nil {
r += "SYSTEM \"\"\"" + *m.system + "\"\"\"\n"
}
if m.adapter != nil {
r += "ADAPTER " + *m.adapter + "\n"
}
if m.license != nil {
r += "LICENSE\n\"\"\"" + *m.license + "\n\"\"\"\n"
}
for _, p := range m.messages {
r += "MESSAGE " + *p.Role + " " + *p.Content + "\n"
}
return r
}