Golang SDK for using Ollama.
- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .forgejo/workflows | ||
| .gitignore | ||
| blobs.go | ||
| chat.go | ||
| client.go | ||
| copy.go | ||
| create.go | ||
| delete.go | ||
| embed.go | ||
| errors.go | ||
| generate.go | ||
| go.mod | ||
| LICENSE | ||
| ps.go | ||
| pull.go | ||
| push.go | ||
| README.md | ||
| renovate.json | ||
| show.go | ||
| structs.go | ||
| tags.go | ||
| version.go | ||
Ollama Go SDK
Lightweight GoLang SDK for working with Ollama.
Supports:
- Bearer Authentication
- All Ollama API routes:
- Generate (completion + streaming)
- Chat (chat completion + streaming, tools, thinking)
- Create a model (from a model, GGUF file, or safetensors directory)
- List local models
- Show model information
- Copy a model
- Delete a model
- Pull a model
- Push a model
- Generate embeddings (
/api/embedand the deprecated/api/embeddings) - List running models
- Check a blob exists / push a blob
- Version
Installation
go get vc.maxkaya.com/sdk/ollama
Quick Start
package main
import (
"fmt"
"vc.maxkaya.com/sdk/ollama"
)
func main() {
client := ollama.NewClient("http://localhost:11434")
// Or if you have an authentication bearer key
client := ollama.NewClientWithAuth("http://localhost:11434", "API_KEY")
res, err := client.Generate(ollama.GenerateRequest{
Model: "qwen3:8b",
Prompt: "What is the answer to life?",
Options: ollama.Options{"temperature": 0.8},
})
if err != nil {
panic(err)
}
fmt.Println("Response: ", res.Response)
}
Streaming
err := client.GenerateStream(ollama.GenerateRequest{
Model: "qwen3:8b",
Prompt: "Count to five",
}, func(chunk ollama.GenerateResponse) error {
fmt.Print(chunk.Response)
return nil // return a non-nil error to stop early
})
Chat with tools
The SDK sends tool definitions to the model and returns the model's
tool_calls. Invoking the underlying functions is your job — the SDK never
runs them for you.
func getWeather(city string) string {
return "11 degrees celsius" // your real implementation
}
func main() {
client := ollama.NewClient("http://localhost:11434")
tools := []ollama.Tool{{
Type: "function",
Function: ollama.ToolFunction{
Name: "get_weather",
Description: "Get the weather in a given city",
Arguments: map[string]any{
"type": "object",
"properties": map[string]any{
"city": map[string]any{"type": "string"},
},
"required": []string{"city"},
},
},
}}
messages := []ollama.ChatMessage{{Role: "user", Content: "What is the weather in Tokyo?"}}
for {
res, err := client.Chat(ollama.ChatRequest{Model: "llama3.2", Messages: messages, Tools: tools})
if err != nil {
panic(err)
}
if len(res.Message.ToolCalls) == 0 {
fmt.Println("Final answer:", res.Message.Content)
return
}
messages = append(messages, ollama.ChatMessage{Role: "assistant", Content: res.Message.Content, ToolCalls: res.Message.ToolCalls})
for _, call := range res.Message.ToolCalls {
city, _ := call.Function.Arguments["city"].(string)
result := getWeather(city)
messages = append(messages, ollama.ChatMessage{Role: "tool", ToolName: call.Function.Name, Content: result})
}
}
}
Pull a model programmatically
err := client.PullModel(ollama.PullRequest{Model: "qwen3:8b"}, func(status ollama.ProgressResponse) error {
fmt.Println("Pull Status: ", status.Status, "digest=", status.Digest, "completed=", status.Completed)
return nil
})
Embeddings
res, err := client.Embed(ollama.EmbedRequest{
Model: "all-minilm",
Input: "Why is the sky blue?",
})