-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerate.go
51 lines (43 loc) · 1.18 KB
/
generate.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
package main
import (
"context"
"fmt"
"os"
"strings"
"github.com/gaurav-gosain/gollama/internal/api"
"github.com/gaurav-gosain/gollama/internal/utils"
oapi "github.com/ollama/ollama/api"
)
// Generates a response using the provided configuration (model, prompt and images)
func (cfg *gollamaConfig) generate() {
ollamaAPI, err := api.NewOllamaAPI()
if err != nil {
utils.PrintError(err, true)
}
msg := strings.TrimSpace(cfg.Prompt)
// Create a slice of ImageData to send to Ollama,
// expand the paths of the images (if needed) and read the image data
imageData := []oapi.ImageData{}
for _, img := range cfg.Images {
expandedPath, err := utils.ExpandPath(img)
if err != nil {
continue
}
imgData, err := os.ReadFile(expandedPath)
if err == nil {
imageData = append(imageData, imgData)
}
}
chatRequest := oapi.GenerateRequest{
Model: cfg.ModelName,
Prompt: msg,
Images: imageData,
}
ctx := context.Background()
// Send the request to Ollama, print the response to stdout using the callback function
ollamaAPI.Client.Generate(ctx, &chatRequest, func(response oapi.GenerateResponse) error {
fmt.Print(response.Response)
return nil
})
fmt.Println()
}