Documentation
¶
Overview ¶
Package llm talks to language models behind one interface, Provider (Chat and Stream), with the message and tool types they share.
NewProvider builds the generic providers: "openai", "gemini", "anthropic", "mistral", "ollama", "llamacpp" and "openai-compat" (any OpenAI-compatible server, BaseURL required), each wrapped with WithRetry. A provider written outside this package implements Provider; one that speaks the OpenAI chat/completions format can start from NewOpenAICompat.
Index ¶
Examples ¶
Constants ¶
const ( ProviderOpenAI ProviderType = "openai" ProviderGemini ProviderType = "gemini" ProviderOllama ProviderType = "ollama" ProviderAnthropic ProviderType = "anthropic" ProviderMistral ProviderType = "mistral" ProviderLlamaCpp ProviderType = "llamacpp" // llama.cpp local server ProviderOpenAICompat ProviderType = "openai-compat" // any OpenAI-compatible server; BaseURL required MediaAudio MediaType = "audio" MediaImage MediaType = "image" )
Variables ¶
This section is empty.
Functions ¶
func ListLlamaCppModels ¶
func ListLlamaCppModels() []string
ListLlamaCppModels queries the local llama.cpp server for the list of loaded models. Returns nil if the server is unreachable.
func ListOllamaModels ¶
func ListOllamaModels() []string
ListOllamaModels queries the local Ollama server for available models. Returns nil if the server is unreachable - Ollama is optional.
Types ¶
type Config ¶
type Config struct {
Provider ProviderType
Model string
APIKey string // empty = the provider's environment variable
BaseURL string // empty = the provider's default URL; required by "openai-compat"
OllamaNumCtx int // Ollama context window, 0 = Ollama's default
OllamaNumPredict int // Ollama max generated tokens, 0 = Ollama's default
}
Config describes a provider for NewProvider.
type FunctionCall ¶
type FunctionCall struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
FunctionCall represents the function name and arguments of a tool call.
type FunctionDef ¶
type FunctionDef struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters ToolParams `json:"parameters"`
}
FunctionDef defines the name, description and parameters of a tool function.
type Media ¶
type Media struct {
Type MediaType `json:"type"`
MimeType string `json:"mime_type"` // "audio/wav", "image/jpeg", etc.
Data []byte `json:"data"` // raw bytes (base64 in JSON)
}
Media represents binary content such as audio or an image attached to a message.
type MediaType ¶
type MediaType string
MediaType identifies the kind of media attached to a message.
type Message ¶
type Message struct {
Role string `json:"role"` // user, assistant, system, tool
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolID string `json:"tool_call_id,omitempty"` // for tool responses
Name string `json:"name,omitempty"` // tool name for responses
Media []Media `json:"media,omitempty"` // for messages containing media
}
Message is a universal message compatible with all providers.
type OpenAICompatConfig ¶
type OpenAICompatConfig struct {
APIKey string // sent as "Authorization: Bearer <APIKey>" when not empty
BaseURL string // up to and including the version, e.g. http://localhost:8000/v1
Model string
Name string // returned by Name and used in error messages
Timeout time.Duration // whole-request timeout, 600s when zero
// ExtraBody adds raw fields to every request body (tool_choice,
// temperature, reasoning_effort...). tool_choice is left out of
// requests that carry no tools.
ExtraBody map[string]any
ExtraHeaders map[string]string // extra HTTP headers on every request
// AudioFormat selects the audio media encoding: "input_audio"
// (OpenAI convention, expected by llama.cpp) or "audio_url" (vLLM/Gemma
// recipes). Empty defaults to "audio_url".
AudioFormat string
}
OpenAICompatConfig describes an OpenAI-compatible chat/completions endpoint for NewOpenAICompat.
type Provider ¶
type Provider interface {
Chat(ctx context.Context, messages []Message, tools []Tool) (*Message, error)
Stream(ctx context.Context, messages []Message, tools []Tool, onChunk func(string) error) (*Message, error)
Name() string
ModelName() string
}
Provider is the interface for all LLMs.
func NewOpenAICompat ¶
func NewOpenAICompat(cfg OpenAICompatConfig) Provider
NewOpenAICompat returns a provider for an OpenAI-compatible endpoint. It does not retry: wrap it with WithRetry to get the behaviour of NewProvider.
Example ¶
A provider for an OpenAI-compatible server that needs its own settings, with the same retries as NewProvider.
package main
import (
"fmt"
"github.com/ThiraSoft/agentkit/llm"
)
func main() {
p := llm.WithRetry(llm.NewOpenAICompat(llm.OpenAICompatConfig{
BaseURL: "http://localhost:8000/v1",
Model: "local-model",
Name: "local",
ExtraBody: map[string]any{"temperature": 0.2},
}))
fmt.Println(p.Name(), p.ModelName())
}
Output: local local-model
func NewProvider ¶
NewProvider builds the provider described by cfg, wrapped with WithRetry. APIKey and BaseURL, when set, take precedence over the environment variable and the default URL of the provider. "openai-compat" requires BaseURL. An unknown provider is an error.
Example ¶
package main
import (
"fmt"
"log"
"github.com/ThiraSoft/agentkit/llm"
)
func main() {
p, err := llm.NewProvider(llm.Config{
Provider: llm.ProviderOpenAICompat,
Model: "local-model",
BaseURL: "http://localhost:8080/v1",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(p.Name(), p.ModelName())
}
Output: openai-compat local-model
func UnwrapProvider ¶
UnwrapProvider removes the decorators (retry) to get back the concrete provider, for instance to type-assert a provider you wrote yourself.
func WithRetry ¶
WithRetry wraps p so that transient errors (rate limit, overload, network drops) are retried with exponential backoff, up to 3 attempts. A stream is retried only while no chunk has been emitted, so shown text is never duplicated. WithRetry returns nil for nil and p itself when p is already wrapped. NewProvider applies it to every provider it builds; use it on a provider built with NewOpenAICompat or written outside this package.
type ProviderType ¶
type ProviderType string
ProviderType identifies an LLM provider implementation.
type Tool ¶
type Tool struct {
Type string `json:"type"` // "function"
Function FunctionDef `json:"function"`
}
Tool describes a function tool that an LLM can call.
type ToolArg ¶
type ToolArg struct {
Type string `json:"type"` // JSON Schema type: "string", "number", "object", etc.
Description string `json:"description"`
Example string `json:"example,omitempty"`
Default string `json:"default,omitempty"`
Items any `json:"items,omitempty"` // for arrays
}
ToolArg describes a single argument in a tool schema.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"` // "function"
Function FunctionCall `json:"function"`
ThoughtSignature string `json:"thoughtSignature,omitempty"` // for Gemini
}
ToolCall represents a tool invocation requested by the model.
type ToolParams ¶
type ToolParams struct {
Type string `json:"type"` // "object"
Properties ToolProperties `json:"properties"`
Required []string `json:"required,omitempty"`
}
ToolParams defines the parameters schema for a tool.
type ToolProperties ¶
ToolProperties maps argument names to their schema definitions.