main.go 865 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/ollama/ollama/api"
  7. )
  8. func main() {
  9. client, err := api.ClientFromEnvironment()
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. // By default, GenerateRequest is streaming.
  14. req := &api.GenerateRequest{
  15. Model: "gemma2",
  16. Prompt: "how many planets are there?",
  17. }
  18. ctx := context.Background()
  19. respFunc := func(resp api.GenerateResponse) error {
  20. // Only print the response here; GenerateResponse has a number of other
  21. // interesting fields you want to examine.
  22. // In streaming mode, responses are partial so we call fmt.Print (and not
  23. // Println) in order to avoid spurious newlines being introduced. The
  24. // model will insert its own newlines if it wants.
  25. fmt.Print(resp.Response)
  26. return nil
  27. }
  28. err = client.Generate(ctx, req, respFunc)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. fmt.Println()
  33. }