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 ¶
const ( RoleSystem = "system" RoleUser = "user" RoleAssistant = "assistant" RoleTool = "tool" )
Role values on a Message.
Variables ¶
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 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 ¶
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 ¶
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.
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.
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. |