llm

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package llm is a provider-agnostic interface over LLM chat + tool-use APIs.

The design mirrors browser-use's Python Protocol: one Chat method, provider-specific adapters translate between this common shape and the respective official SDK (Anthropic, OpenAI, Gemini, Ollama/OpenAI-compat).

The agent loop in cmd/scrapfly imports THIS package, never a provider package directly. Selection is by name at construction time.

Index

Constants

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

Role values on a Message.

Variables

View Source
var ErrNoAPIKey = errors.New("no API key configured for this provider")

ErrNoAPIKey is returned when a provider is selected but its credential is missing from both flags and env.

Functions

func Register

func Register(name string, f Factory)

Register adds a provider factory by name. Called from provider packages' init().

func Registered

func Registered() []string

Registered returns sorted provider names. Useful for help text.

Types

type ChatRequest

type ChatRequest struct {
	Model       string    // per-provider model id; empty → provider default
	System      string    // system prompt (cacheable where supported)
	Messages    []Message // rolling conversation
	Tools       []Tool
	MaxTokens   int     // 0 → provider default
	Temperature float64 // 0 → provider default
	ToolChoice  string  // "auto" | "any" | "required" | "" (= auto)
}

ChatRequest is the common input to Provider.Chat.

type ChatResponse

type ChatResponse struct {
	Text       string
	ToolCalls  []ToolCall
	Usage      Usage
	StopReason StopReason
}

ChatResponse is the common output from Provider.Chat.

type Factory

type Factory func(Options) (Provider, error)

Factory is a per-provider constructor registered at init time. Adapters self-register to avoid import cycles.

type Message

type Message struct {
	Role       string     `json:"role"`
	Content    string     `json:"content,omitempty"`
	ToolCalls  []ToolCall `json:"tool_calls,omitempty"`
	ToolCallID string     `json:"tool_call_id,omitempty"`
	IsError    bool       `json:"is_error,omitempty"` // only for role=tool
}

Message is a turn in the conversation.

  • role=system : Content is the system prompt (usually set via ChatRequest.System; this role exists for providers that don't separate it, e.g. OpenAI).
  • role=user : Content is the user text.
  • role=assistant : Content is free text; ToolCalls carries any tool calls.
  • role=tool : ToolCallID + Content (stringified tool result).

type Options

type Options struct {
	Provider string // anthropic|openai|gemini|ollama
	APIKey   string
	Model    string
	BaseURL  string // for OpenAI-compatible endpoints (ollama, vLLM, ...)
}

Options holds user-supplied overrides for provider construction. Empty fields fall back to env / defaults.

type Provider

type Provider interface {
	Name() string
	Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error)
}

Provider is the single interface each LLM adapter must satisfy.

func Autodetect

func Autodetect(opts Options) (Provider, error)

Autodetect picks a provider by inspecting env vars, in order of preference: ANTHROPIC_API_KEY → OPENAI_API_KEY → GEMINI_API_KEY → OLLAMA_HOST → first registered. Opts (including explicit Provider) always win.

func New

func New(name string, opts Options) (Provider, error)

New constructs a Provider by name.

type StopReason

type StopReason string

StopReason classifies why the model stopped producing output.

const (
	StopEndTurn   StopReason = "end_turn"
	StopToolUse   StopReason = "tool_use"
	StopMaxTokens StopReason = "max_tokens"
	StopOther     StopReason = "other"
)

type Tool

type Tool struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"input_schema"` // JSON Schema (draft 2020-12 works for all)
}

Tool is a provider-agnostic function definition.

type ToolCall

type ToolCall struct {
	ID    string          `json:"id"`
	Name  string          `json:"name"`
	Input json.RawMessage `json:"input"`
}

ToolCall is a single function invocation emitted by the model.

type Usage

type Usage struct {
	InputTokens      int `json:"input_tokens"`
	OutputTokens     int `json:"output_tokens"`
	CacheReadTokens  int `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int `json:"cache_write_tokens,omitempty"`
}

Usage holds token accounting (best-effort per provider).

Directories

Path Synopsis
Package anthropic adapts github.com/anthropics/anthropic-sdk-go to the llm.Provider interface.
Package anthropic adapts github.com/anthropics/anthropic-sdk-go to the llm.Provider interface.
Package gemini adapts google.golang.org/genai to the llm.Provider interface.
Package gemini adapts google.golang.org/genai to the llm.Provider interface.
Package ollama re-exports the OpenAI adapter pointed at a local Ollama server.
Package ollama re-exports the OpenAI adapter pointed at a local Ollama server.
Package openai adapts github.com/openai/openai-go to the llm.Provider interface.
Package openai adapts github.com/openai/openai-go to the llm.Provider interface.

Jump to

Keyboard shortcuts

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