2024-04-10 17:26:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2024-04-10 18:26:15 +00:00
|
|
|
"github.com/ollama/ollama/api"
|
2024-04-10 17:26:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
client, err := api.ClientFromEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default, GenerateRequest is streaming.
|
|
|
|
req := &api.GenerateRequest{
|
2024-07-30 06:56:37 +00:00
|
|
|
Model: "gemma2",
|
2024-04-10 17:26:45 +00:00
|
|
|
Prompt: "how many planets are there?",
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
respFunc := func(resp api.GenerateResponse) error {
|
|
|
|
// Only print the response here; GenerateResponse has a number of other
|
|
|
|
// interesting fields you want to examine.
|
|
|
|
|
|
|
|
// In streaming mode, responses are partial so we call fmt.Print (and not
|
|
|
|
// Println) in order to avoid spurious newlines being introduced. The
|
|
|
|
// model will insert its own newlines if it wants.
|
|
|
|
fmt.Print(resp.Response)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Generate(ctx, req, respFunc)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|