-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen.go
More file actions
391 lines (359 loc) · 9.65 KB
/
gen.go
File metadata and controls
391 lines (359 loc) · 9.65 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main
import (
"context"
"fmt"
"io"
"os"
"strings"
"google.golang.org/genai"
)
type Generator struct {
ctx context.Context
params *Parameters
keyVals ParamMap
client *genai.Client
in io.Reader
out io.Writer
parts []*genai.Part
sysParts []*genai.Part
schema map[string]any
}
func genContent(ctx context.Context, in io.Reader, out io.Writer) error {
params, ok := ctx.Value("params").(*Parameters)
if !ok {
return fmt.Errorf("missing params")
}
if !params.ChatMode {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, params.Timeout)
defer cancel()
}
g, err := newGenerator(ctx, in, out)
if err != nil {
return err
}
return g.run()
}
func newGenerator(ctx context.Context, in io.Reader, out io.Writer) (*Generator, error) {
params, ok := ctx.Value("params").(*Parameters)
if !ok {
return nil, fmt.Errorf("missing params")
}
keyVals, ok := ctx.Value("keyVals").(ParamMap)
if !ok {
return nil, fmt.Errorf("missing keyVals")
}
client, err := genai.NewClient(ctx, nil)
if err != nil {
return nil, err
}
return &Generator{
ctx: ctx,
params: params,
keyVals: keyVals,
client: client,
in: in,
out: out,
}, nil
}
func (g *Generator) run() error {
if g.params.Verbose {
if err := g.emitModelDetails(); err != nil {
return err
}
}
if err := g.setPromptsAndFiles(); err != nil {
return err
}
if g.params.Embed {
return g.saveEmbeddings() // exit after embeddings added
}
if len(g.params.DigestPaths) > 0 {
if err := g.searchDigests(); err != nil {
return err
}
}
config := genai.GenerateContentConfig{
Temperature: genai.Ptr(float32(g.params.Temp)),
TopP: genai.Ptr(float32(g.params.TopP)),
}
if err := g.buildConfig(&config); err != nil {
return err
}
return g.generateContent(&config)
}
func (g *Generator) emitModelDetails() error {
backend := "GeminiAPI"
if g.client.ClientConfig().Backend == genai.BackendVertexAI {
backend = "VertexAI"
}
var m *genai.Model
var err error
if (g.params.Embed || len(g.params.DigestPaths) > 0) && !isFlagSet("m") {
m, err = g.client.Models.Get(g.ctx, g.params.EmbModel, nil)
} else {
m, err = g.client.Models.Get(g.ctx, g.params.GenModel, nil)
}
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, infos("%s backend | %s | %d/%d in/out token limit | %s\n\n"),
backend, m.Name, m.InputTokenLimit, m.OutputTokenLimit, g.params.ThinkingLevel)
return nil
}
func (g *Generator) setPromptsAndFiles() error {
var stdinData []byte
var err error
// handle redirect/piped data
if !g.params.Interactive {
stdinData, err = io.ReadAll(g.in)
if err != nil {
return err
}
g.params.Interactive = len(stdinData) == 0 // ignore redirect
}
// handle prompts from argument
if len(g.params.Args) > 0 {
text := searchReplace(strings.Join(g.params.Args, " "), g.keyVals)
if !g.params.Interactive && text == "-" {
text = string(stdinData)
}
if g.params.SystemInstruction && (g.params.Interactive || !oneMatches(g.params.FilePaths, "-")) {
// argument used as system prompt for chat session unless `-f -` is set
g.sysParts = append(g.sysParts, &genai.Part{Text: text})
} else {
g.parts = append(g.parts, &genai.Part{Text: text})
}
}
// handle files
if len(g.params.FilePaths) > 0 {
for _, filePathVal := range g.params.FilePaths {
// case of redirect passed as file
if filePathVal == "-" {
if g.params.SystemInstruction {
// `-f -` takes precedence over `-s`
g.sysParts = append(g.sysParts, &genai.Part{Text: searchReplace(string(stdinData), g.keyVals)})
} else {
g.parts = append(g.parts, &genai.Part{Text: searchReplace(string(stdinData), g.keyVals)})
}
continue
}
// case of regular file, json schema, .prompt, .sprompt or directory
if err = glob(g.ctx, g.client, filePathVal, &g.parts, &g.sysParts, &g.schema); err != nil {
return err
}
}
// stash URIs of FileData parts for removal
for _, p := range g.parts {
if p.FileData != nil {
g.params.FileURIs = append(g.params.FileURIs, p.FileData.FileURI)
}
}
}
return nil
}
func (g *Generator) saveEmbeddings() error {
res, err := g.client.Models.EmbedContent(g.ctx, g.params.EmbModel, []*genai.Content{{Parts: g.parts}}, nil)
if err != nil {
return err
}
if err := appendToDigest(g.params.DigestPaths[0], res.Embeddings[0], g.keyVals, g.params.OnlyKvs, g.params.Verbose, g.parts...); err != nil {
return err
}
return nil
}
func (g *Generator) searchDigests() error {
var res []QueryResult
for _, digestPathVal := range g.params.DigestPaths {
query, err := g.client.Models.EmbedContent(g.ctx, g.params.EmbModel, []*genai.Content{{Parts: g.parts}}, nil)
if err != nil {
return err
}
res, err = queryDigest(digestPathVal, query.Embeddings[0], res, g.params.K, float32(g.params.Lambda), g.params.Verbose)
if err != nil {
return err
}
}
if len(res) > 0 {
// inject digest into a prompt or append as text
if idx := partWithKey(g.sysParts, DigestKey); idx != -1 {
replacePart(&g.sysParts, idx, DigestKey, res)
} else if idx := partWithKey(g.parts, DigestKey); idx != -1 {
replacePart(&g.parts, idx, DigestKey, res)
} else {
prependToParts(&g.parts, res)
}
}
return nil
}
func (g *Generator) buildConfig(config *genai.GenerateContentConfig) error {
var err error
if g.params.ImgModality {
config.ResponseModalities = []string{"TEXT", "IMAGE"}
} else {
config.ResponseModalities = []string{"TEXT"}
}
if g.params.JSON {
config.ResponseMIMEType = "application/json"
if g.schema != nil {
config.ResponseJsonSchema = g.schema
}
}
if g.params.Tool {
// register tools with genai.FunctionCallingConfigModeAny
config.Tools = []*genai.Tool{}
if err = registerGenTools(config); err != nil { // see tools.go
return err
}
if err = registerMCPTools(g.ctx, config); err != nil { // declared with -mcp
return err
}
conjTexts(&g.parts)
}
if g.params.CodeGen {
config.Tools =
[]*genai.Tool{{CodeExecution: &genai.ToolCodeExecution{}}}
}
if g.params.GoogleSearch {
config.Tools = []*genai.Tool{
{GoogleSearch: &genai.GoogleSearch{}},
{URLContext: &genai.URLContext{}},
}
}
if g.params.Unsafe {
config.SafetySettings = []*genai.SafetySetting{
{
Category: genai.HarmCategoryDangerousContent,
Threshold: genai.HarmBlockThresholdBlockNone,
},
{
Category: genai.HarmCategoryHarassment,
Threshold: genai.HarmBlockThresholdBlockNone,
},
{
Category: genai.HarmCategoryHateSpeech,
Threshold: genai.HarmBlockThresholdBlockNone,
},
{
Category: genai.HarmCategorySexuallyExplicit,
Threshold: genai.HarmBlockThresholdBlockNone,
},
}
}
if len(g.sysParts) > 0 {
config.SystemInstruction = &genai.Content{
Parts: g.sysParts,
}
if g.params.Verbose {
emitContent(os.Stderr, config.SystemInstruction, false, false, true, nil, nil, "")
}
}
if g.params.ThinkingLevel != genai.ThinkingLevelUnspecified {
config.ThinkingConfig = &genai.ThinkingConfig{
IncludeThoughts: true,
ThinkingLevel: genai.ThinkingLevel(g.params.ThinkingLevel),
}
}
return nil
}
func (g *Generator) generateContent(config *genai.GenerateContentConfig) error {
var err error
// retrieve previous session, if any
history := []*genai.Content{}
if g.params.ChatMode {
if err = retrieveHistory(&history); err != nil {
return err
}
if len(history) > 0 {
fmt.Fprintf(g.out, important("%s found\n"), DotGen)
if g.params.Verbose {
emitHistory(os.Stderr, history)
}
}
}
chat, err := g.client.Chats.Create(g.ctx, g.params.GenModel, config, history)
if err != nil {
return err
}
tty := g.in // assume in is terminal for chat
if !g.params.Interactive && g.params.ChatMode {
// in is a redirect, look for a terminal to open
tty, err = openConsole()
if err != nil {
return err
}
}
// main interaction loop
var visited bool // avoid tool call loops
for {
if len(g.parts) > 0 {
i := 0
turnParts := g.parts
g.parts = []*genai.Part{}
var fcAcc []*genai.FunctionCall
for resp, err := range chat.SendStream(g.ctx, turnParts...) {
if err != nil {
fmt.Fprintf(g.out, "\n")
return err
}
if fc := resp.FunctionCalls(); len(fc) > 0 {
fcAcc = append(fcAcc, fc...)
break
}
err := emitCandidate(g.out, resp.Candidates[0], g.params.OutRedirected, g.params.ImgModality, g.params.Verbose, &i, g.params.OutPath)
if err != nil {
fmt.Fprintf(g.out, "\n")
return err
}
if g.params.TokenCount && resp.UsageMetadata != nil {
TokenCount.Store(resp.UsageMetadata.TotalTokenCount)
}
} // end turn
if len(fcAcc) > 0 && !visited {
visited = true
resCand := processFunctionCalls(g.ctx, fcAcc)
if resCand != nil {
err := emitCandidate(g.out, resCand, g.params.OutRedirected, g.params.ImgModality, g.params.Verbose, &i, g.params.OutPath)
if err != nil {
fmt.Fprintf(g.out, "\n")
return err
}
// see protocol https://ai.google.dev/gemini-api/docs/function-calling
g.parts = append(g.parts, turnParts...)
g.parts = append(g.parts, resCand.Content.Parts...)
continue
}
}
}
// exit if not a chat
if !g.params.ChatMode {
break
}
input, err := readLine(tty)
if err != nil {
return err
}
// check for double blank line
if input == "" {
input, err = readLine(tty)
if err != nil {
return err
}
if input == "" {
break // exit chat mode
}
}
if g.params.OutRedirected {
fmt.Fprintf(g.out, "\n%s\n\n", input)
}
g.parts = append(g.parts, &genai.Part{Text: input})
visited = false
} // end main interaction loop
if g.params.ChatMode {
if err = persistChat(chat); err != nil {
fmt.Fprintf(g.out, "\n")
return err
}
}
return nil
}