talk

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 3 Imported by: 2

README

axon-talk

Primitives · Part of the lamina workspace

LLM provider adapters implementing a provider-agnostic interface. Each subpackage implements talk.LLMClient for a specific backend, translating axon-talk's provider-agnostic request/response types into native API calls.

Getting started

go get github.com/benaskins/axon-talk@latest
import (
    "github.com/benaskins/axon-talk"
    "github.com/benaskins/axon-talk/ollama"
)

client, err := ollama.NewClientFromEnvironment()
if err != nil {
    log.Fatal(err)
}

// client implements talk.LLMClient
req := &talk.Request{
    Model: "llama3.2",
    Messages: []talk.Message{
        {Role: talk.RoleUser, Content: "Say hello in one sentence."},
    },
    Stream: true,
}

err = client.Chat(ctx, req, func(resp talk.Response) error {
    fmt.Print(resp.Content) // Print tokens as they arrive
    return nil
})

Providers

Package Backend Constructor
ollama Ollama ollama.NewClientFromEnvironment()
anthropic Anthropic Messages API anthropic.NewClient(baseURL, key)
bedrock Claude via Amazon Bedrock bedrock.NewClient(modelID, invoke)
openai OpenAI-compatible APIs openai.NewClient(baseURL, token)
cloudflare Cloudflare Workers AI cloudflare.NewClient(baseURL, token)

The openai package works with any provider that speaks the OpenAI /v1/chat/completions protocol: OpenAI, Gemini, Grok, Groq, Together, Fireworks, Azure OpenAI, and others.

License

MIT

Documentation

Overview

Package talk defines the protocol types for LLM communication and provides provider adapters in sub-packages. Each sub-package implements talk.LLMClient for a specific backend, translating provider-agnostic types into native API calls.

Class: primitive UseWhen: Always required with axon-loop. For CLI agents, use axon-hand instead which provides axon-talk automatically.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ImageContent added in v0.13.0

type ImageContent struct {
	MediaType string
	Data      string
}

ImageContent is an image attached to a user message for vision models. Data is the raw image bytes, base64-encoded; MediaType is the MIME type (e.g. "image/jpeg"). Adapters that do not support images ignore these.

type LLMClient

type LLMClient interface {
	Chat(ctx context.Context, req *Request, fn func(Response) error) error
}

LLMClient abstracts communication with an LLM backend. Implementations translate to/from provider-specific APIs (e.g. Ollama, OpenAI, Anthropic).

type Message

type Message struct {
	Role       Role
	Content    string
	Images     []ImageContent // Inline images for a vision request; only meaningful on a user message.
	Thinking   string
	ToolCalls  []ToolCall
	ToolCallID string // Correlates a tool result with its originating ToolCall.
}

Message represents a single message in a conversation.

Example
package main

import (
	"fmt"

	talk "github.com/benaskins/axon-talk"
)

func main() {
	user := talk.Message{
		Role:    talk.RoleUser,
		Content: "Explain goroutines in one sentence.",
	}

	assistant := talk.Message{
		Role:    talk.RoleAssistant,
		Content: "Goroutines are lightweight, cooperatively scheduled threads managed by the Go runtime.",
	}

	fmt.Println(user.Role)
	fmt.Println(assistant.Role)
}
Output:
user
assistant

type Request

type Request struct {
	Model         string
	Messages      []Message
	Tools         []tool.ToolDef
	Stream        bool
	Think         *bool
	Options       map[string]any
	MaxIterations int // Maximum tool-call loop iterations. Defaults to 20 if 0.
	MaxTokens     int // Maximum estimated token budget for messages. 0 means no limit.
}

Request is a provider-agnostic request to an LLM.

func NewRequest

func NewRequest(model string, messages []Message, opts ...RequestOption) *Request

NewRequest creates a Request with the given model, messages, and options.

Example
package main

import (
	"fmt"

	talk "github.com/benaskins/axon-talk"
)

func main() {
	req := talk.NewRequest("claude-sonnet-4-20250514", []talk.Message{
		{Role: talk.RoleSystem, Content: "You are a helpful assistant."},
		{Role: talk.RoleUser, Content: "What is the capital of France?"},
	})
	fmt.Println(req.Model)
	fmt.Println(len(req.Messages), "messages")
}
Output:
claude-sonnet-4-20250514
2 messages

type RequestOption

type RequestOption func(*Request)

RequestOption configures a Request.

func WithStructuredOutput added in v0.6.0

func WithStructuredOutput(schema map[string]any) RequestOption

WithStructuredOutput returns a RequestOption that requests the LLM to respond with JSON conforming to the given schema. Each adapter translates this to its provider-specific wire format.

type Response

type Response struct {
	Content   string
	Thinking  string
	Done      bool
	ToolCalls []ToolCall
	Usage     *Usage // non-nil on the final chunk (Done=true) when the provider reports it
}

Response is a provider-agnostic streamed response chunk from an LLM.

type Role

type Role string

Role represents the sender of a message.

const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type StatusError added in v0.5.1

type StatusError struct {
	StatusCode int
	Body       string
	Provider   string
}

StatusError is returned by adapters when the provider API responds with a non-OK HTTP status. It carries the status code so callers (e.g. retry middleware) can make decisions based on it.

func (*StatusError) Error added in v0.5.1

func (e *StatusError) Error() string

type ToolCall

type ToolCall struct {
	ID        string // Provider-assigned correlation ID (e.g. OpenAI tool_call_id).
	Name      string
	Arguments map[string]any
}

ToolCall represents an LLM's decision to invoke a tool.

type Usage added in v0.10.0

type Usage struct {
	InputTokens  int
	OutputTokens int

	// Cache stats (zero when the provider does not support caching).
	CacheCreationInputTokens int
	CacheReadInputTokens     int
}

Usage reports token consumption for a single LLM call.

func (Usage) TotalTokens added in v0.10.0

func (u Usage) TotalTokens() int

TotalTokens returns InputTokens + OutputTokens.

Directories

Path Synopsis
Package anthropic provides a talk.LLMClient implementation for the Anthropic Messages API.
Package anthropic provides a talk.LLMClient implementation for the Anthropic Messages API.
Package bedrock provides a talk.LLMClient implementation for Anthropic Claude models served through Amazon Bedrock's InvokeModel API.
Package bedrock provides a talk.LLMClient implementation for Anthropic Claude models served through Amazon Bedrock's InvokeModel API.
Package openai provides a talk.LLMClient implementation for any OpenAI-compatible chat completions API.
Package openai provides a talk.LLMClient implementation for any OpenAI-compatible chat completions API.

Jump to

Keyboard shortcuts

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