llm

package
v0.39.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 5 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 hosts provider-neutral LLM utilities — currently the rule-based RouterProvider for multi-model selection — and the per-provider subpackages.

Concrete providers live in their own subpackages so a binary that uses one vendor does not statically link the other vendors' SDKs:

  • pkg/llm/anthropic — Claude (Messages API)
  • pkg/llm/openai — OpenAI + OpenAI-compatible endpoints, embedder, vision analyzer, summary provider
  • pkg/llm/gemini — Google Gemini (public API + Vertex AI), embedder, media analyzer

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RouteCondition

type RouteCondition func(memory []history.Message) bool

RouteCondition inspects the current conversation and returns true when the associated provider should handle the request.

func Always

func Always() RouteCondition

Always returns a RouteCondition that always matches. Use it as the last route to make a provider the "second fallback":

router.AddRoute(llm.IfTokensUnder(500), fastProvider).
    AddRoute(llm.Always(), mediumProvider) // before the real fallback

func IfLastMessageContains

func IfLastMessageContains(keywords ...string) RouteCondition

IfLastMessageContains returns true when the most recent user message contains any of the given keywords (case-insensitive). Use for intent-based routing:

llm.IfLastMessageContains("summarize", "tldr", "shorten") → fastProvider
llm.IfLastMessageContains("analyze", "compare", "reason") → powerfulProvider

func IfMessageCountUnder

func IfMessageCountUnder(n int) RouteCondition

IfMessageCountUnder returns true when the conversation has fewer than n messages. Conversations with few turns are usually simple and can use a lighter model.

func IfSystemPromptContains

func IfSystemPromptContains(keyword string) RouteCondition

IfSystemPromptContains returns true when any system message contains the keyword. Useful for routing sub-agents by their declared role:

// In a sub-agent with system prompt "You are a summarizer…"
llm.IfSystemPromptContains("summarizer") → fastProvider

func IfTokensUnder

func IfTokensUnder(threshold int) RouteCondition

IfTokensUnder returns true when the estimated token count of the conversation is below threshold. Uses the same 4-chars/token heuristic as the agent loop. Useful for routing short/cheap tasks to a fast model.

type RouterProvider

type RouterProvider struct {
	// contains filtered or unexported fields
}

RouterProvider is an agent.LLMProvider that forwards each request to the first provider whose RouteCondition matches the conversation. Falls back to a default provider when no condition matches.

Typical pattern — use a cheap/fast model for short or simple tasks:

router := llm.NewRouterProvider(powerfulProvider).
    AddRoute(llm.IfTokensUnder(500), fastProvider).
    AddRoute(llm.IfLastMessageContains("summarize", "tldr"), fastProvider)

Register as the LLM for any AgentLoop:

loop := agent.NewAgentLoop(sessions, registry, router)

func NewRouterProvider

func NewRouterProvider(fallback agent.LLMProvider) *RouterProvider

NewRouterProvider creates a router with the given fallback provider. The fallback is used when no route condition matches.

func (*RouterProvider) AddRoute

func (r *RouterProvider) AddRoute(condition RouteCondition, provider agent.LLMProvider) *RouterProvider

AddRoute registers a condition+provider pair. Routes are evaluated in declaration order; the first match wins.

func (*RouterProvider) GenerateStream

func (r *RouterProvider) GenerateStream(
	ctx context.Context,
	memory []history.Message,
	availableTools *tools.Registry,
	streamChan chan<- agent.StreamEvent,
) (agent.LLMResult, error)

GenerateStream selects the appropriate provider and delegates.

Directories

Path Synopsis
Package anthropic implements agent.LLMProvider for Anthropic's Claude models via the Messages API, including prompt caching, extended thinking, and structured output (synthesized-tool JSON mode).
Package anthropic implements agent.LLMProvider for Anthropic's Claude models via the Messages API, including prompt caching, extended thinking, and structured output (synthesized-tool JSON mode).
Package gemini implements agent.LLMProvider for Google Gemini — both the public Gemini API and Vertex AI — plus the Gemini-backed embedder and multimodal media analyzer.
Package gemini implements agent.LLMProvider for Google Gemini — both the public Gemini API and Vertex AI — plus the Gemini-backed embedder and multimodal media analyzer.
Package llmfake provides in-tree fakes for the agent.LLMProvider interface, mirroring pkg/history/historyfake for the session layer.
Package llmfake provides in-tree fakes for the agent.LLMProvider interface, mirroring pkg/history/historyfake for the session layer.
Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider.
Package openai implements agent.LLMProvider for the OpenAI Chat Completions API and any OpenAI-compatible endpoint (Ollama, Groq, Together AI, vLLM, ...), plus the OpenAI-backed embedder, vision analyzer, and history summary provider.

Jump to

Keyboard shortcuts

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