llm

package
v0.92.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package llm provides LLM agent integration and invocation.

Index

Constants

View Source
const (
	SystemRole    = "system"
	UserRole      = "user"
	AssistantRole = "model"
	ToolRole      = "tool"
)

Role constants

View Source
const (
	AgentChat              = "chat"
	AgentBillClassify      = "bill_classify"
	AgentExtractTags       = "extract_tags"
	AgentSimilarTags       = "similar_tags"
	AgentNewsSummary       = "news_summary"
	AgentRepoReviewComment = "repo_review_comment"
)

Agent name constants

View Source
const (
	ProviderOpenAI           = "openai"
	ProviderOpenAICompatible = "openai_compatible"
	ProviderGemini           = "gemini"
	ProviderAnthropic        = "anthropic"
)

Model provider constants

Variables

View Source
var ErrUnknownProvider = errors.New("unknown llm provider")

Functions

func AgentEnabled

func AgentEnabled(name string) bool

func AgentModelName

func AgentModelName(name string) string

func ConvertFromString

func ConvertFromString(input string) (map[string]any, error)

ConvertFromString converts string input to map if needed

func CountMessageTokens

func CountMessageTokens(messages []*Message) (int, error)

func CountToken

func CountToken(text string) int

func GetModel

func GetModel(modelName string) config.Model

func LLMGenerate

func LLMGenerate(ctx context.Context, modelName, prompt string) (string, error)

func RegisterAnthropic

func RegisterAnthropic()

RegisterAnthropic registers the Anthropic LLM provider in the global provider registry.

func RegisterGemini

func RegisterGemini()

RegisterGemini registers the Gemini LLM provider in the global provider registry.

func RegisterOpenAI

func RegisterOpenAI()

RegisterOpenAI registers the OpenAI and OpenAI-compatible LLM providers.

func Stream

func Stream(ctx context.Context, client *Client, messages []*Message) (<-chan *Message, error)

Stream streams an LLM response

Types

type BaseTool

type BaseTool interface {
	Info(ctx context.Context) (*ToolInfo, error)
	InvokableRun(ctx context.Context, input string) (string, error)
}

BaseTool is the base interface for all tools

type ChatTemplate

type ChatTemplate interface {
	Format(ctx context.Context, data map[string]any) ([]*Message, error)
}

func BaseTemplate

func BaseTemplate() ChatTemplate

func DefaultMultiChatTemplate

func DefaultMultiChatTemplate() ChatTemplate

func DefaultTemplate

func DefaultTemplate() ChatTemplate

type Client

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

Client wraps a Provider for model operations

func ChatModel

func ChatModel(_ context.Context, modelName string) (*Client, error)

ChatModel creates a chat model client

type FunctionTool

type FunctionTool struct {
	Name        string
	Description string
	Parameters  *ParamsOneOf
	Execute     func(ctx context.Context, input string) (string, error)
}

FunctionTool implements InvokableTool for function-based tools

func (*FunctionTool) Info

func (f *FunctionTool) Info(_ context.Context) (*ToolInfo, error)

Info returns tool metadata

func (*FunctionTool) InvokableRun

func (f *FunctionTool) InvokableRun(ctx context.Context, input string) (string, error)

InvokableRun executes the tool

type GenerateRequest

type GenerateRequest struct {
	Model       string
	Messages    []*Message
	Tools       []Tool
	MaxTokens   int
	Temperature float64
}

GenerateRequest holds parameters for a model generation call

type InvokableTool

type InvokableTool interface {
	BaseTool
}

InvokableTool is a tool that can be invoked

type Message

type Message struct {
	Role    string
	Content string
	Name    string
}

Message represents a chat message

func Generate

func Generate(ctx context.Context, client *Client, messages []*Message) (*Message, error)

Generate generates an LLM response

type MessageFragment

type MessageFragment struct {
	Content string
	Done    bool
	Err     error
}

MessageFragment represents a partial streaming response chunk

type ParamsOneOf

type ParamsOneOf struct {
	OneOf []Schema
}

ParamsOneOf represents tool parameters schema

func (*ParamsOneOf) ToJSONSchema

func (p *ParamsOneOf) ToJSONSchema() (map[string]any, error)

ToJSONSchema converts ParamsOneOf to JSON schema map

type Provider

type Provider interface {
	Name() string
	Generate(ctx context.Context, req *GenerateRequest) (*Message, error)
	GenerateStream(ctx context.Context, req *GenerateRequest) (<-chan MessageFragment, error)
}

Provider is the interface for LLM model backends

func NewProvider

func NewProvider(cfg config.Model) (Provider, error)

NewProvider creates a provider based on model configuration

type Role

type Role = string

Role type

type Schema

type Schema struct {
	Type        string
	Description string
	Properties  map[string]Schema
	Required    []string
}

Schema represents JSON schema for tool parameters

type Tool

type Tool interface {
	Name() string
	Description() string
	Parameters() *ParamsOneOf
	Execute(ctx context.Context, input string) (string, error)
}

Tool interface for agent tools

type ToolCall

type ToolCall struct {
	ID       string
	Type     string
	Function ToolCallFunction
}

ToolCall represents a function call from the model

type ToolCallFunction

type ToolCallFunction struct {
	Name      string
	Arguments string
}

ToolCallFunction represents function call details

type ToolInfo

type ToolInfo struct {
	Name        string
	Desc        string
	ParamsOneOf *ParamsOneOf
}

ToolInfo contains tool metadata

Jump to

Keyboard shortcuts

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