-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini.go
73 lines (64 loc) · 1.81 KB
/
gemini.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
package main
import (
"context"
"fmt"
"time"
"github.com/google/generative-ai-go/genai"
)
/*
uploadFilesToGemini uploads all files given from command line.
*/
func uploadFilesToGemini(ctx context.Context, client *genai.Client, clFiles []string) ([]*genai.File, error) {
var err error
files := []*genai.File{}
if len(clFiles) == 0 {
return files, nil
}
fmt.Printf("\nFile uploads:\n")
for _, filename := range clFiles {
fmt.Printf(" %s ... ", filename)
if !fileExists(filename) {
fmt.Printf("error: file does't exist\n")
continue
}
// display name = max 512 characters
uploadOptions := genai.UploadFileOptions{}
uploadOptions.DisplayName = filename
file, err := client.UploadFileFromPath(ctx, filename, &uploadOptions)
if err != nil {
fmt.Printf("error: %v\n", err)
} else {
fmt.Printf("uploaded (%.1f KiB, %s)\n", float64(file.SizeBytes)/1024.0, file.MIMEType)
files = append(files, file)
}
}
// ensure that all remote (uploaded) files have state "active" (e.g. videos need to be processed)
fmt.Printf("\nRemote file states:\n")
maxWaitDuration := progConfig.GeminiMaxWaitTimeFileProcessing // seconds
for i, file := range files {
currentWaitDuration := 0
tmpFile := file
for tmpFile.State == genai.FileStateProcessing {
tmpFile, err = client.GetFile(ctx, file.Name)
if err != nil {
fmt.Printf(" error [%s] getting state for remote file [%s]\n", err, file.DisplayName)
break
}
if tmpFile.State == genai.FileStateActive {
break
}
if tmpFile.State != genai.FileStateProcessing {
break
}
time.Sleep(3 * time.Second)
currentWaitDuration += 3
if currentWaitDuration >= maxWaitDuration {
break
}
fmt.Printf(".")
}
files[i] = tmpFile
fmt.Printf("\r %s ... %s\n", tmpFile.DisplayName, tmpFile.State.String())
}
return files, nil
}