main.go 860 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "ollama.com/api"
  8. )
  9. func main() {
  10. if len(os.Args) <= 1 {
  11. log.Fatal("usage: <image name>")
  12. }
  13. imgData, err := os.ReadFile(os.Args[1])
  14. if err != nil {
  15. log.Fatal(err)
  16. }
  17. client, err := api.ClientFromEnvironment()
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. req := &api.GenerateRequest{
  22. Model: "llava",
  23. Prompt: "describe this image",
  24. Images: []api.ImageData{imgData},
  25. }
  26. ctx := context.Background()
  27. respFunc := func(resp api.GenerateResponse) error {
  28. // In streaming mode, responses are partial so we call fmt.Print (and not
  29. // Println) in order to avoid spurious newlines being introduced. The
  30. // model will insert its own newlines if it wants.
  31. fmt.Print(resp.Response)
  32. return nil
  33. }
  34. err = client.Generate(ctx, req, respFunc)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. fmt.Println()
  39. }