Golang SDK for using Ollama.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-09-20 18:31:31 -04:00
.forgejo/workflows chore(deps): update https://vc.maxkaya.com/maxpeterkaya/changelog action to v2 2026-09-16 14:44:46 +00:00
.gitignore init gitignore 2026-07-11 16:19:48 -04:00
blobs.go feat: add blob support 2026-08-14 16:36:05 -04:00
chat.go feat: improve chat functions with options 2026-08-14 16:35:24 -04:00
client.go fix: add missing comments for godoc 2026-08-14 16:49:34 -04:00
copy.go feat: add copy support 2026-08-14 16:36:13 -04:00
create.go feat: add create support 2026-08-14 16:36:18 -04:00
delete.go feat: add delete support 2026-08-14 16:36:23 -04:00
embed.go fix: add missing comments for godoc 2026-08-14 16:49:34 -04:00
errors.go fix: add missing comments for godoc 2026-08-14 16:49:34 -04:00
generate.go feat: improve generate functions with options 2026-08-14 16:35:31 -04:00
go.mod fix: set major version in module 2026-08-16 22:00:02 -04:00
LICENSE init license 2026-07-11 16:19:32 -04:00
ps.go feat: add ps support 2026-08-14 16:36:37 -04:00
pull.go feat: improve pull functions with chunk streaming 2026-08-14 16:35:51 -04:00
push.go feat: add push support 2026-08-14 16:36:42 -04:00
README.md docs: update readme with all functions and abilities 2026-08-14 16:39:49 -04:00
renovate.json Add renovate.json 2026-08-19 23:11:00 +00:00
show.go feat: add show support 2026-08-14 16:36:48 -04:00
structs.go fix: add missing comments for godoc 2026-08-14 16:49:34 -04:00
tags.go feat: add tags support 2026-08-14 16:36:53 -04:00
version.go feat: add version support 2026-08-14 16:36:58 -04:00

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/embed and 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?",
})