Skip to content

Commit

Permalink
Switched to /generate endpoint (use with ollama main branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
oderwat committed Oct 28, 2024
1 parent ece6548 commit 0d93391
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ func (args) Version() string {
}

func GenerateWithImage(ol *api.Client, model, prompt, imagePath string) (string, error) {
// First, convert the image to base64
imgData, err := os.ReadFile(imagePath)
if err != nil {
return "", fmt.Errorf("failed to read image: %w", err)
}

req := &api.GenerateRequest{
Model: model,
Prompt: prompt,
Images: []api.ImageData{imgData},
}

ctx := context.Background()
var response strings.Builder
respFunc := func(resp api.GenerateResponse) error {
response.WriteString(resp.Response)
return nil
}

err = ol.Generate(ctx, req, respFunc)
if err != nil {
log.Fatal(err)
}
return response.String(), nil
}

/*
func ChatWithImage(ol *api.Client, model, prompt, imagePath string) (string, error) {
// First, convert the image to base64
imageData, err := os.ReadFile(imagePath)
if err != nil {
Expand Down Expand Up @@ -63,6 +91,7 @@ func GenerateWithImage(ol *api.Client, model, prompt, imagePath string) (string,
}
return response.String(), nil
}
*/

// ProcessImages walks through a given path and processes image files
func ProcessImages(path string, processFunc func(imagePath, rootDir string)) error {
Expand Down Expand Up @@ -135,6 +164,7 @@ func main() {
}

captionText, err := GenerateWithImage(ol, args.Model, args.Prompt, path)
//captionText, err := ChatWithImage(ol, args.Model, args.Prompt, path)
if err != nil {
log.Fatalf("Aborting because of %v", err)
}
Expand Down

0 comments on commit 0d93391

Please sign in to comment.