Skip to content

Instantly share code, notes, and snippets.

@stevegt
Created September 3, 2024 00:43
Show Gist options
  • Save stevegt/c912c4708e8181cecc6d2ba588c20620 to your computer and use it in GitHub Desktop.
Save stevegt/c912c4708e8181cecc6d2ba588c20620 to your computer and use it in GitHub Desktop.
example Ollama client in Go
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/ollama/ollama/api"
. "github.com/stevegt/goadapt"
)
func main() {
if len(os.Args) < 3 {
fmt.Printf("usage: %s {ip} {port}\n", os.Args[0])
os.Exit(1)
}
ip := os.Args[1]
port := os.Args[2]
host := fmt.Sprintf("%s:%s", ip, port)
// set OLLAMA_HOST and OLLAMA_PORT
os.Setenv("OLLAMA_HOST", host)
// create a new api client
client, err := api.ClientFromEnvironment()
Ck(err)
messages := []api.Message{
api.Message{
Role: "system",
Content: "Provide very brief, concise responses",
},
api.Message{
Role: "user",
Content: "Name some unusual animals",
},
api.Message{
Role: "assistant",
Content: "Monotreme, platypus, echidna",
},
api.Message{
Role: "user",
Content: "which of these is the most dangerous?",
},
api.Message{
Role: "assistant",
Content: "The platypus is venomous",
},
api.Message{
Role: "user",
Content: "What is the platypus venom used for?",
},
}
ctx := context.Background()
req := &api.ChatRequest{
Model: "llama3.1:8b",
Messages: messages,
}
respFunc := func(resp api.ChatResponse) error {
fmt.Print(resp.Message.Content)
return nil
}
err = client.Chat(ctx, req, respFunc)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment