ai-sdk

module
v0.1.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 30, 2026 License: Apache-2.0

README

AI SDK (Go)

A provider-agnostic AI SDK for Go — a re-interpretation of the AI SDK ecosystem for the Go programming language. Chat, embeddings, image generation, speech, transcription, structured object generation, video, and reranking — all through a unified, type-safe, interface-driven API.

go get github.com/samcharles93/ai-sdk

Overview

This SDK provides a clean, composable way to work with AI providers in Go. Instead of vendor-specific clients scattered through your codebase, you program against domain interfaces in pkg/chat, pkg/embed, pkg/image, etc. Providers are injected at the composition root — your business logic never imports a provider directly.

Features
  • Unified interface across 8 domains: chat, embedding, image generation, speech synthesis, transcription, object generation, video generation, reranking
  • Pluggable providers — swap implementations at the wiring layer
  • Tool use and streaming built into the chat domain
  • Agent loops built on top of StreamText — tool-calling agent with streaming events
  • Middleware — compose logging, telemetry, and circuit-breaker layers around providers
  • Runtime layer — resolve provider/model references dynamically from a models.dev catalog
  • UI layer — Templ + Datastar components for real-time reactive chat UIs
  • Strict onion architecture — domain packages import nothing outside stdlib
Supported Providers
Provider Package Chat Embed Image Speech Transcribe Object Rerank Video
OpenAI pkg/provider/openai
Anthropic pkg/provider/anthropic
Azure pkg/provider/azure
Cohere pkg/provider/cohere
DeepSeek pkg/provider/deepseek
Gemini pkg/provider/gemini
Groq pkg/provider/groq
Mistral pkg/provider/mistral
Ollama pkg/provider/ollama
Perplexity pkg/provider/perplexity
TogetherAI pkg/provider/togetherai
xAI pkg/provider/xai

Quick Start

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/samcharles93/ai-sdk/chat"
    "github.com/samcharles93/ai-sdk/provider/openai"
)

func main() {
    provider, err := openai.New(openai.Config{
        APIKey: os.Getenv("OPENAI_API_KEY"),
    })
    if err != nil {
        log.Fatal(err)
    }

    resp, err := provider.Chat(context.Background(), chat.Request{
        Model:    "gpt-5.4",
        Messages: []chat.Message{
            {Role: chat.RoleUser, Content: "Hello!"},
        },
    })
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(resp.Content)
}
Streaming
stream, err := provider.ChatStream(ctx, chat.Request{
    Model:    "gpt-5.4",
    Messages: []chat.Message{{Role: chat.RoleUser, Content: "Tell me a story"}},
})
defer stream.Close()

for {
    chunk, err := stream.Next(ctx)
    if err == io.EOF {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(chunk.Delta)
}
With Tool Use
resp, err := provider.Chat(ctx, chat.Request{
    Model:    "gpt-5.4",
    Messages: []chat.Message{{Role: chat.RoleUser, Content: "What's the weather in London?"}},
    Tools: []chat.Tool{{
        Name:        "get_weather",
        Description: "Get current weather for a location",
        Parameters:  json.RawMessage(`{"type":"object","properties":{"location":{"type":"string"}},"required":["location"]}`),
    }},
    ToolChoice: &chat.ToolChoice{Type: chat.ToolChoiceAuto},
})

Architecture

The SDK follows a strict onion architecture — dependencies flow inward. Outer layers depend on inner layers, never the reverse.

┌──────────────────────────────────────────┐
│  UI Layer        pkg/ui/                 │  Templ + Datastar
│  Runtime         pkg/runtime/            │  Provider resolution
│  Agent           pkg/agent/              │  Tool-loop agent
│  Core/Services   pkg/core/               │  Orchestration facades
│  Middleware      pkg/middleware/          │  Provider wrappers
│  Infrastructure  pkg/registry/, schema/, │
│                  util/, upload/, error/,  │
│                  logger/, telemetry/,     │
│                  prompt/                  │
│  Domain          pkg/chat/, embed/,       │  Interfaces + types (stdlib only)
│  Providers       pkg/provider/*/          │  Wire implementations
└──────────────────────────────────────────┘
Key rules
  • Domain packages (pkg/chat, pkg/embed, etc.) import only stdlib
  • Provider packages implement domain interfaces; import only domain packages + stdlib + HTTP
  • Core/Services orchestrate providers through interfaces — no provider import
  • Runtime resolves provider/model strings into working provider instances
  • No global state, no init() wiring, no package-level singletons

Examples

Run examples from the repo root:

# Chat
OPENAI_API_KEY=sk-... go run ./ai-sdk-examples/openai-chat/

# Agent with tool use
ANTHROPIC_API_KEY=sk-ant-... go run ./ai-sdk-examples/anthropic-agent/ "What's the weather in London?"

# Object generation
go run ./ai-sdk-examples/object-generation/

# Image generation
AZURE_API_KEY=... go run ./ai-sdk-examples/image-generation/

# Transcription
go run ./ai-sdk-examples/speech-to-text/

Full example list at ai-sdk-examples/README.md.


Development

Prerequisites
Commands
go test ./...              # run all tests
gofumpt -w .               # format
golangci-lint run ./...    # lint

The project uses gofumpt for formatting and golangci-lint with govet, staticcheck, unused, nilerr, and misspell enabled.


License

Apache-2.0 — see LICENSE.

Directories

Path Synopsis
Package agent provides a tool-loop agent that orchestrates multi-step reasoning and tool execution.
Package agent provides a tool-loop agent that orchestrates multi-step reasoning and tool execution.
Package agentloop runs a single autonomous agent mission to completion: one model, a jailed toolset, a quality gate that must pass before the run can succeed, budgets that bound it, and a structured result that reports what happened.
Package agentloop runs a single autonomous agent mission to completion: one model, a jailed toolset, a quality gate that must pass before the run can succeed, budgets that bound it, and a structured result that reports what happened.
Package chat defines provider-agnostic chat types and the Provider interface that all model backends implement.
Package chat defines provider-agnostic chat types and the Provider interface that all model backends implement.
Package core provides the high-level AI SDK orchestration functions: GenerateText, StreamText, and supporting types for tools, structured output, and stop conditions.
Package core provides the high-level AI SDK orchestration functions: GenerateText, StreamText, and supporting types for tools, structured output, and stop conditions.
Package embed defines provider-agnostic embedding types and the Provider interface that all embedding model backends implement.
Package embed defines provider-agnostic embedding types and the Provider interface that all embedding model backends implement.
Package image defines provider-agnostic image generation types and the ImageProvider interface that all image model backends implement.
Package image defines provider-agnostic image generation types and the ImageProvider interface that all image model backends implement.
Package middleware provides middleware types for wrapping AI SDK providers.
Package middleware provides middleware types for wrapping AI SDK providers.
Package object defines provider-agnostic types and the Provider interface for object generation operations.
Package object defines provider-agnostic types and the Provider interface for object generation operations.
Package prompt provides small, self-contained helpers for building and formatting prompts used with language models.
Package prompt provides small, self-contained helpers for building and formatting prompts used with language models.
provider
anthropic
Package anthropic implements the chat.Provider interface for the Anthropic Messages API.
Package anthropic implements the chat.Provider interface for the Anthropic Messages API.
azure
Package azure implements chat.Provider, embed.Provider, and image.Provider for the Azure OpenAI Service.
Package azure implements chat.Provider, embed.Provider, and image.Provider for the Azure OpenAI Service.
cohere
Package cohere implements chat.Provider, embed.Provider and rerank.Provider for the Cohere API (https://api.cohere.com/v1).
Package cohere implements chat.Provider, embed.Provider and rerank.Provider for the Cohere API (https://api.cohere.com/v1).
deepseek
Package deepseek implements the chat.Provider interface for the DeepSeek chat completion API.
Package deepseek implements the chat.Provider interface for the DeepSeek chat completion API.
gemini
Package gemini implements the chat.Provider interface against Google's Gemini native generateContent API.
Package gemini implements the chat.Provider interface against Google's Gemini native generateContent API.
groq
Package groq provides access to Groq's Whisper transcription API via the transcribe.Provider interface.
Package groq provides access to Groq's Whisper transcription API via the transcribe.Provider interface.
ollama
Package ollama provides a chat.Provider implementation backed by an Ollama HTTP server (https://ollama.com).
Package ollama provides a chat.Provider implementation backed by an Ollama HTTP server (https://ollama.com).
openai
Package openai implements the chat.Provider interface for OpenAI's Chat Completions and Responses APIs.
Package openai implements the chat.Provider interface for OpenAI's Chat Completions and Responses APIs.
togetherai
Package togetherai provides Together AI provider implementations for the SDK.
Package togetherai provides Together AI provider implementations for the SDK.
xai
Package xai implements the chat.Provider interface for the xAI (Grok) chat completion API.
Package xai implements the chat.Provider interface for the xAI (Grok) chat completion API.
Package registry provides a provider registry for managing multiple AI model providers (chat, embedding, image, speech, transcription) through a single, unified interface.
Package registry provides a provider registry for managing multiple AI model providers (chat, embedding, image, speech, transcription) through a single, unified interface.
Package rerank defines provider-agnostic document reranking types and the Provider interface that all reranking model backends implement.
Package rerank defines provider-agnostic document reranking types and the Provider interface that all reranking model backends implement.
Package runtime provides a provider-agnostic AI runtime for the ai-sdk.
Package runtime provides a provider-agnostic AI runtime for the ai-sdk.
Package schema provides helpers for working with JSON Schema definitions in the AI SDK ecosystem.
Package schema provides helpers for working with JSON Schema definitions in the AI SDK ecosystem.
Package speech defines provider-agnostic speech synthesis types and the SpeechProvider interface that all text-to-speech backends implement.
Package speech defines provider-agnostic speech synthesis types and the SpeechProvider interface that all text-to-speech backends implement.
Package telemetry defines minimal tracing interfaces used across the ai-sdk codebase.
Package telemetry defines minimal tracing interfaces used across the ai-sdk codebase.
Package toolkit provides a registry of built-in agent tools — file read/write/edit, shell, grep, and find — hardened for autonomous use with working-directory confinement, write/shell mutation serialisation, size caps, and output truncation.
Package toolkit provides a registry of built-in agent tools — file read/write/edit, shell, grep, and find — hardened for autonomous use with working-directory confinement, write/shell mutation serialisation, size caps, and output truncation.
rg
Package rg embeds a statically-linked ripgrep binary and exposes a single entry point so the grep tool can always use authoritative rg matching.
Package rg embeds a statically-linked ripgrep binary and exposes a single entry point so the grep tool can always use authoritative rg matching.
Package transcribe defines provider-agnostic audio transcription types and the TranscriptionProvider interface that all speech-to-text backends implement.
Package transcribe defines provider-agnostic audio transcription types and the TranscriptionProvider interface that all speech-to-text backends implement.
ui
Package ui provides the AI SDK UI layer — a Go re-interpretation of the AI SDK UI libraries (React, Svelte, Vue, Angular) using server-side Templ components and Datastar for streaming reactivity.
Package ui provides the AI SDK UI layer — a Go re-interpretation of the AI SDK UI libraries (React, Svelte, Vue, Angular) using server-side Templ components and Datastar for streaming reactivity.
chat
Package chat provides server-side chat state management — the Go equivalent of the AI SDK's useChat() hook.
Package chat provides server-side chat state management — the Go equivalent of the AI SDK's useChat() hook.
components
templ: version: v0.3.1001
templ: version: v0.3.1001
handlers
Package handlers provides HTTP handler implementations for AI SDK UI endpoints.
Package handlers provides HTTP handler implementations for AI SDK UI endpoints.
Package uimessage implements the AI SDK UI Message Stream protocol.
Package uimessage implements the AI SDK UI Message Stream protocol.
sse
Package sse implements the SSE wire format for the AI SDK UI message stream protocol.
Package sse implements the SSE wire format for the AI SDK UI message stream protocol.
Package upload provides simple helpers for parsing multipart file uploads and parsing simple skill definition payloads used by the UI.
Package upload provides simple helpers for parsing multipart file uploads and parsing simple skill definition payloads used by the UI.
Package util provides shared utilities used across the AI SDK.
Package util provides shared utilities used across the AI SDK.
Package video defines provider-agnostic video generation types and the VideoProvider interface that all video model backends implement.
Package video defines provider-agnostic video generation types and the VideoProvider interface that all video model backends implement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL