From 4340f8eba4b3a0e5672a0c4837ac45a23c074fdf Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Wed, 10 Apr 2024 10:26:45 -0700 Subject: [PATCH] examples: start adding Go examples using api/ (#2879) We can have the same examples as e.g. https://github.com/ollama/ollama-python/tree/main/examples here. Using consistent naming and renaming the existing example to have -http- since it uses direct HTTP requests rather than api/ Updates #2840 --- examples/go-generate-streaming/main.go | 40 +++++++++++++++++++ examples/go-generate/main.go | 37 +++++++++++++++++ .../README.md | 0 .../main.go | 0 4 files changed, 77 insertions(+) create mode 100644 examples/go-generate-streaming/main.go create mode 100644 examples/go-generate/main.go rename examples/{golang-simplegenerate => go-http-generate}/README.md (100%) rename examples/{golang-simplegenerate => go-http-generate}/main.go (100%) diff --git a/examples/go-generate-streaming/main.go b/examples/go-generate-streaming/main.go new file mode 100644 index 00000000..ab7fdc73 --- /dev/null +++ b/examples/go-generate-streaming/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/jmorganca/ollama/api" +) + +func main() { + client, err := api.ClientFromEnvironment() + if err != nil { + log.Fatal(err) + } + + // By default, GenerateRequest is streaming. + req := &api.GenerateRequest{ + Model: "gemma", + 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() +} diff --git a/examples/go-generate/main.go b/examples/go-generate/main.go new file mode 100644 index 00000000..048da473 --- /dev/null +++ b/examples/go-generate/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + "fmt" + "log" + + "github.com/jmorganca/ollama/api" +) + +func main() { + client, err := api.ClientFromEnvironment() + if err != nil { + log.Fatal(err) + } + + req := &api.GenerateRequest{ + Model: "gemma", + Prompt: "how many planets are there?", + + // set streaming to false + Stream: new(bool), + } + + 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. + fmt.Println(resp.Response) + return nil + } + + err = client.Generate(ctx, req, respFunc) + if err != nil { + log.Fatal(err) + } +} diff --git a/examples/golang-simplegenerate/README.md b/examples/go-http-generate/README.md similarity index 100% rename from examples/golang-simplegenerate/README.md rename to examples/go-http-generate/README.md diff --git a/examples/golang-simplegenerate/main.go b/examples/go-http-generate/main.go similarity index 100% rename from examples/golang-simplegenerate/main.go rename to examples/go-http-generate/main.go