Skip to content

digitallysavvy/go-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

120 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Go AI SDK

The Go AI SDK is a comprehensive toolkit designed to help you build AI-powered applications and agents using Go. It provides 1:1 feature parity with the Vercel AI SDK for backend functionality.

To learn more about how to use the Go AI SDK, check out our Documentation.

Installation

You will need Go 1.21+ installed on your local development machine.

go get github.com/digitallysavvy/go-ai

Unified Provider Architecture

The Go AI SDK provides a unified API to interact with model providers like OpenAI, Anthropic, Google, and more.

go get github.com/digitallysavvy/go-ai/pkg/providers/openai
go get github.com/digitallysavvy/go-ai/pkg/providers/anthropic
go get github.com/digitallysavvy/go-ai/pkg/providers/google

Usage

Generating Text

import (
    "context"
    "fmt"
    "os"

    "github.com/digitallysavvy/go-ai/pkg/ai"
    "github.com/digitallysavvy/go-ai/pkg/providers/openai"
)

func main() {
    ctx := context.Background()

    provider := openai.New(openai.Config{
        APIKey: os.Getenv("OPENAI_API_KEY"),
    })
    model, _ := provider.LanguageModel("gpt-4")

    result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
        Model:  model,
        Prompt: "What is an agent?",
    })

    fmt.Println(result.Text)
}

Streaming Text

stream, _ := ai.StreamText(ctx, ai.StreamTextOptions{
    Model:  model,
    Prompt: "Write a story about Go programming",
})

for chunk := range stream.TextChannel {
    fmt.Print(chunk)
}

Generating Structured Data

import "github.com/digitallysavvy/go-ai/pkg/schema"

type Recipe struct {
    Name        string   `json:"name"`
    Ingredients []string `json:"ingredients"`
    Steps       []string `json:"steps"`
}

recipeSchema := schema.NewSimpleJSONSchema(map[string]interface{}{
    "type": "object",
    "properties": map[string]interface{}{
        "name":        map[string]interface{}{"type": "string"},
        "ingredients": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
        "steps":       map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
    },
    "required": []string{"name", "ingredients", "steps"},
})

result, _ := ai.GenerateObject(ctx, ai.GenerateObjectOptions{
    Model:  model,
    Prompt: "Generate a lasagna recipe.",
    Schema: recipeSchema,
})

var recipe Recipe
json.Unmarshal([]byte(result.Object), &recipe)
fmt.Printf("Recipe: %s\n", recipe.Name)

Agents

Build autonomous agents with multi-step reasoning:

import (
    "github.com/digitallysavvy/go-ai/pkg/agent"
    "github.com/digitallysavvy/go-ai/pkg/provider/types"
)

myAgent := agent.New(agent.Config{
    Model:        model,
    Instructions: "You are a helpful research assistant.",
    Tools: []types.Tool{
        searchTool,
        calculatorTool,
    },
    MaxSteps: 10,
})

result, _ := myAgent.Execute(ctx, "What is the population of Tokyo?")
fmt.Println(result.Text)

Tool Calling

Extend AI capabilities with custom tools:

import "github.com/digitallysavvy/go-ai/pkg/provider/types"

weatherTool := types.Tool{
    Name:        "get_weather",
    Description: "Get current weather for a location",
    Parameters: map[string]interface{}{
        "type": "object",
        "properties": map[string]interface{}{
            "location": map[string]interface{}{
                "type":        "string",
                "description": "City name",
            },
        },
        "required": []string{"location"},
    },
    Execute: func(ctx context.Context, params map[string]interface{}, opts types.ToolExecutionOptions) (interface{}, error) {
        location := params["location"].(string)
        return map[string]interface{}{
            "temperature": 72,
            "condition":   "sunny",
        }, nil
    },
}

result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
    Model:  model,
    Prompt: "What's the weather in San Francisco?",
    Tools:  []types.Tool{weatherTool},
})

Embeddings

Generate embeddings for semantic search:

embeddingModel, _ := provider.EmbeddingModel("text-embedding-3-small")

result, _ := ai.Embed(ctx, ai.EmbedOptions{
    Model: embeddingModel,
    Input: "Go is great for building AI applications",
})

// result.Embedding contains the vector

Image Generation

imageModel, _ := provider.ImageModel("dall-e-3")

result, _ := ai.GenerateImage(ctx, ai.GenerateImageOptions{
    Model:  imageModel,
    Prompt: "A serene mountain landscape at sunset",
    Size:   "1024x1024",
})

// result.Image contains the generated image bytes

Speech and Transcription

// Generate speech
speechModel, _ := provider.SpeechModel("tts-1")
result, _ := ai.GenerateSpeech(ctx, ai.GenerateSpeechOptions{
    Model: speechModel,
    Text:  "Hello, welcome to the Go AI SDK!",
    Voice: "alloy",
})

// Transcribe audio
transcriptionModel, _ := provider.TranscriptionModel("whisper-1")
transcript, _ := ai.Transcribe(ctx, ai.TranscribeOptions{
    Model: transcriptionModel,
    Audio: audioBytes,
})

Memory Optimization

Reduce memory consumption by 50-80% for image-heavy or large-context workloads using retention settings:

import "github.com/digitallysavvy/go-ai/pkg/provider/types"

// Enable memory optimization
retention := &types.RetentionSettings{
    RequestBody:  types.BoolPtr(false), // Don't retain request
    ResponseBody: types.BoolPtr(false), // Don't retain response
}

result, _ := ai.GenerateText(ctx, ai.GenerateTextOptions{
    Model:                 model,
    Prompt:                "Analyze this image...",
    ExperimentalRetention: retention,
})

// Result still has usage, finish reason, text, etc.
// But raw request/response bodies are excluded to save memory

Perfect for:

  • πŸ–ΌοΈ Image processing workloads
  • πŸ“„ Large document analysis
  • πŸ”’ Privacy-sensitive applications
  • ⚑ Long-running services

See examples/features/retention for detailed usage.

Supported Providers

The Go AI SDK supports 26+ providers:

Provider Language Models Embeddings Images Speech
OpenAI GPT-4, GPT-3.5, O1 βœ“ DALL-E TTS, Whisper
Anthropic Claude 3.5 Sonnet, Claude 3 - - -
Google Gemini Pro, Flash βœ“ - -
AWS Bedrock Claude, Titan, Llama βœ“ - -
Azure OpenAI Azure-hosted models βœ“ βœ“ βœ“
Mistral Large, Medium, Small βœ“ - -
Cohere Command R+, Command βœ“ - -
Groq Llama, Mixtral - - Whisper
Together AI Llama, Mixtral, Qwen - Stable Diffusion -
Fireworks Llama, Mixtral βœ“ - -
Perplexity Sonar models - - -
DeepSeek DeepSeek Chat, Coder - - -
xAI Grok - - -
Replicate All hosted models - βœ“ -
Hugging Face Inference API - - -
Ollama Local models βœ“ - -

And many more...

Features

  • βœ… Unified API - One interface for 26+ providers
  • βœ… Text Generation - GenerateText() and StreamText()
  • βœ… Structured Output - Type-safe GenerateObject() with JSON validation
  • βœ… Tool Calling - Extend models with custom functions
  • βœ… Agents - Autonomous multi-step reasoning with ToolLoopAgent
  • βœ… Embeddings - Generate and search with vector embeddings
  • βœ… Image Generation - Text-to-image with multiple providers
  • βœ… Speech - TTS and transcription capabilities
  • βœ… Middleware - Logging, caching, rate limiting, and more
  • βœ… Telemetry - Built-in OpenTelemetry integration
  • βœ… Registry - Resolve models by string ID (e.g., "openai:gpt-4")
  • βœ… Context Support - Native Go context cancellation and timeouts
  • βœ… Streaming - Real-time responses with automatic backpressure
  • βœ… Memory Optimization - Retention settings for 50-80% memory reduction

Why Go for AI?

While Python dominates AI/ML model training, Go excels at building production AI applications:

  • πŸš€ Performance - Fast execution and low memory overhead
  • ⚑ Concurrency - Native goroutines and channels for parallel processing
  • πŸ“¦ Single Binary - No dependencies, easy deployment
  • πŸ”’ Type Safety - Catch errors at compile time
  • ☁️ Cloud Native - Perfect for Kubernetes, Docker, microservices
  • 🏒 Production Ready - Built for scalable backend systems

Examples

We provide 50+ production-ready examples covering every feature. See the examples directory for complete working code.

πŸš€ HTTP Servers (5 examples)

πŸ“¦ Structured Output (4 examples)

πŸ€– Provider Features (8 examples)

  • OpenAI - Reasoning (o1), structured outputs, vision
  • Anthropic - Caching, extended thinking, PDF support
  • Google, Azure - Integration patterns

🧠 Agents (5 examples)

πŸ› οΈ Production Patterns (7 examples)

  • Middleware - Logging, caching, rate limiting, retry, telemetry
  • Testing - Unit and integration test patterns
  • MCP - Model Context Protocol (stdio, HTTP, auth, tools)

🎨 Multimodal (5 examples)

πŸ”¬ Advanced (4 examples)

All examples:

  • βœ… Compile and pass go vet
  • βœ… Include comprehensive README with usage
  • βœ… Follow Go best practices
  • βœ… Work with real API calls

Browse all 50+ examples β†’

Documentation

TypeScript Parity

This SDK maintains 1:1 feature parity with the Vercel AI SDK for backend functionality:

  • Same function signatures and patterns
  • Same provider interfaces
  • Same middleware system
  • Compatible workflows
  • Feature complete for server-side use

Community

The Go AI SDK community can be found on GitHub where you can ask questions, voice ideas, and share your projects:

Contributing

Contributions to the Go AI SDK are welcome and highly appreciated. However, before you jump right into it, we would like you to review our Contribution Guidelines to make sure you have a smooth experience contributing to the Go AI SDK.

License

Apache 2.0 - See LICENSE for details.

Authors

This library is created by @digitallysavvy, inspired by the Vercel AI SDK, with contributions from the Open Source Community.

About

The AI Toolkit for Golang. The GO AI SDK is a free open-source library for building AI-powered applications and agents

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages