Gollama is a robust, idiomatic Go client library for the Ollama API. It provides complete, type-safe access to all Ollama endpoints, including model management, text generation, chat, embeddings, and process status.
- Full API Coverage: All Ollama endpoints implemented
- Streaming Support: Real-time streaming for generation, chat, and model operations
- Type Safety: Comprehensive Go structs for all requests and responses
- Context-Aware: All methods accept
context.Context - Custom Error Handling: Detailed API errors with status codes
- Idiomatic Go: Follows Go best practices and conventions
- Comprehensive Tests: 1000+ lines of test coverage
go get github.com/astrica1/gollamapackage main
import (
"context"
"fmt"
"log"
"time"
"github.com/astrica1/gollama"
)
func main() {
client, err := gollama.NewClient() // Defaults to http://localhost:11434
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req := &gollama.GenerateRequest{
Model: "llama2",
Prompt: "Why is the sky blue?",
}
resp, err := client.Generate(ctx, req)
if err != nil {
log.Fatal(err)
}
fmt.Println("Response:", resp.Response)
}This library provides Go types and methods for all Ollama API endpoints:
- Model Management:
/api/tags,/api/show,/api/copy,/api/delete,/api/pull,/api/create,/api/push - Text Generation:
/api/generate - Chat:
/api/chat - Embeddings:
/api/embeddings - Process Status:
/api/ps
List(ctx context.Context) (*ListModelsResponse, error)Show(ctx context.Context, modelName string) (*ModelResponse, error)Copy(ctx context.Context, source, destination string) errorDelete(ctx context.Context, modelName string) errorPull(ctx context.Context, modelName string, fn func(PullProgress)) errorCreate(ctx context.Context, modelName, modelfileContent string, fn func(CreateProgress)) errorPush(ctx context.Context, modelName string, fn func(PushProgress)) error
Generate(ctx context.Context, req *GenerateRequest) (*GenerateResponse, error)GenerateStream(ctx context.Context, req *GenerateRequest, fn func(*GenerateResponse)) error
Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)ChatStream(ctx context.Context, req *ChatRequest, fn func(*ChatResponse)) error
Embeddings(ctx context.Context, req *EmbeddingRequest) (*EmbeddingResponse, error)
PS(ctx context.Context) (*PSResponse, error)
ctx := context.Background()
models, err := client.List(ctx)
model, err := client.Show(ctx, "llama2")
err = client.Copy(ctx, "llama2", "llama2-backup")
err = client.Delete(ctx, "old-model")
err = client.Pull(ctx, "llama2", func(progress gollama.PullProgress) {
fmt.Printf("Progress: %s\n", progress.Status)
})
modelfile := "FROM llama2\nSYSTEM You are a helpful assistant."
err = client.Create(ctx, "my-model", modelfile, func(progress gollama.CreateProgress) {
fmt.Printf("Creating: %s\n", progress.Status)
})
err = client.Push(ctx, "my-model", func(progress gollama.PushProgress) {
fmt.Printf("Pushing: %s\n", progress.Status)
})req := &gollama.GenerateRequest{
Model: "llama2",
Prompt: "Tell me a joke",
Options: map[string]interface{}{
"temperature": 0.7,
"top_p": 0.9,
},
}
resp, err := client.Generate(ctx, req)
err = client.GenerateStream(ctx, req, func(resp *gollama.GenerateResponse) {
fmt.Print(resp.Response)
})chatReq := &gollama.ChatRequest{
Model: "llama2",
Messages: []gollama.Message{
{Role: "user", Content: "Hello!"},
},
}
chatResp, err := client.Chat(ctx, chatReq)
err = client.ChatStream(ctx, chatReq, func(resp *gollama.ChatResponse) {
fmt.Print(resp.Message.Content)
})embReq := &gollama.EmbeddingRequest{
Model: "llama2",
Prompt: "Hello world",
}
embResp, err := client.Embeddings(ctx, embReq)
fmt.Printf("Embedding vector length: %d\n", len(embResp.Embedding))status, err := client.PS(ctx)
fmt.Printf("Running models: %d\n", len(status.Models))Client- Main API clientMessage- Chat messagesModelResponse/ListModelsResponse- Model informationGenerateRequest/GenerateResponse- Text generationChatRequest/ChatResponse- Chat completionsEmbeddingRequest/EmbeddingResponse- Vector embeddingsCreateRequest/CreateProgress- Model creationPushRequest/PushProgress- Model publishingPSResponse- Process statusOllamaError- Custom error type
All errors returned by the client implement the error interface. API errors are of type *OllamaError and include HTTP status codes and messages.
resp, err := client.Generate(ctx, req)
if err != nil {
if ollamaErr, ok := err.(*gollama.OllamaError); ok {
fmt.Printf("Ollama API error (status %d): %s\n", ollamaErr.StatusCode, ollamaErr.Message)
} else {
fmt.Printf("Other error: %v\n", err)
}
}See the examples/ directory for complete working examples:
- Go 1.21 or later
- Access to an Ollama server (local or remote)
Contributions are welcome! Please open an issue or submit a pull request.
MIT License - see LICENSE
π§ Foundation Complete - This library currently provides:
- β Complete client structure with HTTP client
- β All necessary data types and structs
- β Proper JSON serialization/deserialization
- β Custom error handling
- β Comprehensive test suite
- β Usage examples
π Coming Next - API method implementations for:
Model listing and managementβ COMPLETED- Text generation (streaming and non-streaming)
- Chat completions (streaming and non-streaming)
- Embedding generation
The library now includes complete model management functionality:
ctx := context.Background()
// List all available models
models, err := client.List(ctx)
if err != nil {
log.Fatal(err)
}
// Show details for a specific model
model, err := client.Show(ctx, "llama2")
if err != nil {
log.Fatal(err)
}
// Copy a model
err = client.Copy(ctx, "llama2", "llama2-backup")
if err != nil {
log.Fatal(err)
}
// Pull a model with progress tracking
err = client.Pull(ctx, "llama2", func(progress gollama.PullProgress) {
if progress.Total > 0 {
percent := float64(progress.Completed) / float64(progress.Total) * 100
fmt.Printf("Progress: %.1f%% - %s\n", percent, progress.Status)
} else {
fmt.Printf("Status: %s\n", progress.Status)
}
})
// Delete a model
err = client.Delete(ctx, "old-model")
if err != nil {
log.Fatal(err)
}The library includes complete Go structs for all Ollama API interactions:
Client- Main API clientMessage- Chat messagesModelResponse/ListModelsResponse- Model informationGenerateRequest/GenerateResponse- Text generationChatRequest/ChatResponse- Chat completionsEmbeddingRequest/EmbeddingResponse- Vector embeddingsOllamaError- Custom error type
See doc.go for complete API documentation.
Check out the examples directory for usage examples:
- Basic Usage - Client initialization and data structures
- Model Management - Complete model management demo
# Run tests
go test ./...
# Run examples
go run examples/basic/main.go
# Format code
go fmt ./...
# Lint code
golangci-lint run- Go 1.21 or later
- Access to an Ollama server (local or remote)
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the terms specified in the LICENSE file.
- Ollama for providing the excellent local LLM platform
- The Go community for best practices and conventions