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
This commit is contained in:
parent
4c7db6b7e9
commit
4340f8eba4
4 changed files with 77 additions and 0 deletions
40
examples/go-generate-streaming/main.go
Normal file
40
examples/go-generate-streaming/main.go
Normal file
|
@ -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()
|
||||||
|
}
|
37
examples/go-generate/main.go
Normal file
37
examples/go-generate/main.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue