llm

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStopResponse = errors.New("stop response")

Functions

func BuildJSONSchema added in v0.0.5

func BuildJSONSchema(t reflect.Type) map[string]any

func FlattenTools added in v0.0.5

func FlattenTools(tools []interface{}) []interface{}

FlattenTools recursively unwraps Toolsets and returns a slice of Tool and ProviderTool.

func GenerateStrictJSONSchema

func GenerateStrictJSONSchema(t reflect.Type) map[string]interface{}

GenerateStrictJSONSchema inspects a Go function or struct to generate an OpenAI-compatible "strict" JSON schema. It enforces additionalProperties: false and makes all fields required by default, unless optional tags are provided.

func Register added in v0.0.5

func Register(provider string, factory LLMFactory)

Types

type AgentConfigUpdate

type AgentConfigUpdate struct {
	ID           string
	Instructions *string
	ToolsAdded   []string
	ToolsRemoved []string
	CreatedAt    time.Time
}

func (*AgentConfigUpdate) GetCreatedAt

func (a *AgentConfigUpdate) GetCreatedAt() time.Time

func (*AgentConfigUpdate) GetID

func (a *AgentConfigUpdate) GetID() string

func (*AgentConfigUpdate) GetType

func (a *AgentConfigUpdate) GetType() string

type AgentHandoff

type AgentHandoff struct {
	ID         string
	OldAgentID *string
	NewAgentID string
	CreatedAt  time.Time
}

func (*AgentHandoff) GetCreatedAt

func (a *AgentHandoff) GetCreatedAt() time.Time

func (*AgentHandoff) GetID

func (a *AgentHandoff) GetID() string

func (*AgentHandoff) GetType

func (a *AgentHandoff) GetType() string

type AudioContent

type AudioContent struct {
	Frames     []any
	Transcript string
}

type ChatChunk

type ChatChunk struct {
	ID    string
	Delta *ChoiceDelta
	Usage *CompletionUsage
}

type ChatContent

type ChatContent struct {
	Text  string
	Image *ImageContent
	Audio *AudioContent
}

type ChatContext

type ChatContext struct {
	Items       []ChatItem          `json:"items"`
	OnItemAdded func(item ChatItem) `json:"-"`
}

func NewChatContext

func NewChatContext() *ChatContext

func (*ChatContext) Append

func (c *ChatContext) Append(item ChatItem)

func (*ChatContext) Copy

func (c *ChatContext) Copy() *ChatContext

func (*ChatContext) FindInsertionIndex

func (c *ChatContext) FindInsertionIndex(createdAt time.Time) int

func (*ChatContext) Merge

func (c *ChatContext) Merge(other *ChatContext)

func (*ChatContext) Messages

func (c *ChatContext) Messages() []*ChatMessage

func (*ChatContext) ToDict added in v0.0.5

func (c *ChatContext) ToDict(excludeTimestamp bool) map[string]any

func (*ChatContext) ToProviderFormat

func (c *ChatContext) ToProviderFormat(format string) (any, any)

func (*ChatContext) Truncate

func (c *ChatContext) Truncate(maxItems int)

type ChatItem

type ChatItem interface {
	GetID() string
	GetType() string
	GetCreatedAt() time.Time
}

type ChatMessage

type ChatMessage struct {
	ID                   string
	Role                 ChatRole
	Content              []ChatContent
	Interrupted          bool
	TranscriptConfidence *float64
	Extra                map[string]any
	CreatedAt            time.Time
}

func (*ChatMessage) GetCreatedAt

func (m *ChatMessage) GetCreatedAt() time.Time

func (*ChatMessage) GetID

func (m *ChatMessage) GetID() string

func (*ChatMessage) GetType

func (m *ChatMessage) GetType() string

func (*ChatMessage) TextContent

func (m *ChatMessage) TextContent() string

type ChatOption

type ChatOption func(*ChatOptions)

func WithParallelToolCalls

func WithParallelToolCalls(parallel bool) ChatOption

func WithToolChoice

func WithToolChoice(choice ToolChoice) ChatOption

func WithTools

func WithTools(tools []interface{}) ChatOption

type ChatOptions

type ChatOptions struct {
	Tools             []interface{}
	ToolChoice        ToolChoice
	ParallelToolCalls bool
}

type ChatRole

type ChatRole string
const (
	ChatRoleDeveloper ChatRole = "developer"
	ChatRoleSystem    ChatRole = "system"
	ChatRoleUser      ChatRole = "user"
	ChatRoleAssistant ChatRole = "assistant"
)

type ChoiceDelta

type ChoiceDelta struct {
	Role      ChatRole
	Content   string
	ToolCalls []FunctionToolCall
	Extra     map[string]any
}

type CompletionUsage

type CompletionUsage struct {
	CompletionTokens   int
	PromptTokens       int
	PromptCachedTokens int
	TotalTokens        int
}

type DiffOps

type DiffOps struct {
	ToRemove []string
	ToCreate [][2]*string // [previous_item_id, id]
	ToUpdate [][2]*string // [previous_item_id, id]
}

func ComputeChatCtxDiff

func ComputeChatCtxDiff(oldCtx, newCtx *ChatContext) *DiffOps

type FallbackAdapter

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

func NewFallbackAdapter

func NewFallbackAdapter(llms []LLM) *FallbackAdapter

func (*FallbackAdapter) Chat

func (f *FallbackAdapter) Chat(ctx context.Context, chatCtx *ChatContext, opts ...ChatOption) (LLMStream, error)

type FunctionCall

type FunctionCall struct {
	ID        string
	CallID    string
	Name      string
	Arguments string
	Extra     map[string]any
	GroupID   *string
	CreatedAt time.Time
}

func (*FunctionCall) GetCreatedAt

func (f *FunctionCall) GetCreatedAt() time.Time

func (*FunctionCall) GetID

func (f *FunctionCall) GetID() string

func (*FunctionCall) GetType

func (f *FunctionCall) GetType() string

type FunctionCallOutput

type FunctionCallOutput struct {
	ID        string
	CallID    string
	Name      string
	Output    string
	IsError   bool
	CreatedAt time.Time
}

func (*FunctionCallOutput) GetCreatedAt

func (f *FunctionCallOutput) GetCreatedAt() time.Time

func (*FunctionCallOutput) GetID

func (f *FunctionCallOutput) GetID() string

func (*FunctionCallOutput) GetType

func (f *FunctionCallOutput) GetType() string

type FunctionToolCall

type FunctionToolCall struct {
	Type      string
	Name      string
	Arguments string
	CallID    string
	Extra     map[string]any
}

type ImageContent

type ImageContent struct {
	ID              string
	Image           any
	InferenceWidth  *int
	InferenceHeight *int
	InferenceDetail string
	MimeType        string
}

type LLM

type LLM interface {
	Chat(ctx context.Context, chatCtx *ChatContext, opts ...ChatOption) (LLMStream, error)
}

func FromModelString added in v0.0.5

func FromModelString(s string) (LLM, error)

type LLMFactory added in v0.0.5

type LLMFactory func(model string) (LLM, error)

type LLMStream

type LLMStream interface {
	Next() (*ChatChunk, error)
	Close() error
}

type MCPServer

type MCPServer interface {
	Initialize(ctx context.Context) error
	ListTools(ctx context.Context) ([]Tool, error)
	Close() error
}

type MCPServerHTTP

type MCPServerHTTP struct {
	URL           string
	TransportType string
	AllowedTools  []string
	Headers       map[string]string
	// contains filtered or unexported fields
}

func NewMCPServerHTTP

func NewMCPServerHTTP(url string) *MCPServerHTTP

func (*MCPServerHTTP) Close

func (s *MCPServerHTTP) Close() error

func (*MCPServerHTTP) Initialize

func (s *MCPServerHTTP) Initialize(ctx context.Context) error

func (*MCPServerHTTP) ListTools

func (s *MCPServerHTTP) ListTools(ctx context.Context) ([]Tool, error)

type MCPServerStdio

type MCPServerStdio struct {
	Command string
	Args    []string
	Env     map[string]string
	Cwd     string
	// contains filtered or unexported fields
}

func NewMCPServerStdio

func NewMCPServerStdio(command string, args []string) *MCPServerStdio

func (*MCPServerStdio) Close

func (s *MCPServerStdio) Close() error

func (*MCPServerStdio) Initialize

func (s *MCPServerStdio) Initialize(ctx context.Context) error

func (*MCPServerStdio) ListTools

func (s *MCPServerStdio) ListTools(ctx context.Context) ([]Tool, error)

type MetricsReport

type MetricsReport struct {
	Usage     telemetry.UsageSummary
	CreatedAt time.Time
}

func (*MetricsReport) GetCreatedAt

func (m *MetricsReport) GetCreatedAt() time.Time

func (*MetricsReport) GetID

func (m *MetricsReport) GetID() string

func (*MetricsReport) GetType

func (m *MetricsReport) GetType() string

type ProviderTool added in v0.0.5

type ProviderTool interface {
	IsProviderTool() bool
	Name() string
	ProviderSchema(format string) map[string]any
}

ProviderTool represents a tool that is evaluated or passed raw to a provider.

type RawFunctionTool added in v0.0.5

type RawFunctionTool struct {
	ToolName        string
	ToolDescription string
	ToolParameters  map[string]any
	ExecuteFunc     func(ctx context.Context, args map[string]any) (any, error)
}

func (*RawFunctionTool) Description added in v0.0.5

func (t *RawFunctionTool) Description() string

func (*RawFunctionTool) Execute added in v0.0.5

func (t *RawFunctionTool) Execute(ctx context.Context, args any) (any, error)

func (*RawFunctionTool) ID added in v0.0.5

func (t *RawFunctionTool) ID() string

func (*RawFunctionTool) Name added in v0.0.5

func (t *RawFunctionTool) Name() string

func (*RawFunctionTool) Parameters added in v0.0.5

func (t *RawFunctionTool) Parameters() map[string]any

type RealtimeCapabilities

type RealtimeCapabilities struct {
	MessageTruncation       bool
	TurnDetection           bool
	UserTranscription       bool
	AutoToolReplyGeneration bool
	AudioOutput             bool
}

type RealtimeEvent

type RealtimeEvent struct {
	Type     RealtimeEventType
	Data     []byte // For audio frames
	Text     string // For text deltas
	Function *FunctionToolCall
	Error    error
}

type RealtimeEventType

type RealtimeEventType string
const (
	RealtimeEventTypeAudio         RealtimeEventType = "audio"
	RealtimeEventTypeText          RealtimeEventType = "text"
	RealtimeEventTypeFunctionCall  RealtimeEventType = "function_call"
	RealtimeEventTypeSpeechStarted RealtimeEventType = "speech_started"
	RealtimeEventTypeSpeechStopped RealtimeEventType = "speech_stopped"
	RealtimeEventTypeError         RealtimeEventType = "error"
)

type RealtimeModel

type RealtimeModel interface {
	Session() (RealtimeSession, error)
	Close() error
}

type RealtimeSession

type RealtimeSession interface {
	UpdateInstructions(instructions string) error
	UpdateChatContext(chatCtx *ChatContext) error
	UpdateTools(tools []interface{}) error
	Interrupt() error
	Close() error
	EventCh() <-chan RealtimeEvent
	PushAudio(frame *model.AudioFrame) error
}

type RemoteChatContext

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

func NewRemoteChatContext

func NewRemoteChatContext() *RemoteChatContext

func (*RemoteChatContext) Delete

func (c *RemoteChatContext) Delete(itemID string) error

func (*RemoteChatContext) Get

func (c *RemoteChatContext) Get(itemID string) ChatItem

func (*RemoteChatContext) Insert

func (c *RemoteChatContext) Insert(previousItemID *string, message ChatItem) error

func (*RemoteChatContext) ToChatCtx

func (c *RemoteChatContext) ToChatCtx() *ChatContext

type Tool

type Tool interface {
	ID() string
	Name() string
	Description() string
	Parameters() map[string]any
	Execute(ctx context.Context, args any) (any, error)
}

func BuildFunctionTool added in v0.0.5

func BuildFunctionTool(fn any, name, description string) (Tool, error)

BuildFunctionTool uses reflection to build a Tool from a Go function, extracting its signature into a JSON schema.

type ToolChoice

type ToolChoice any

type ToolContext

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

func EmptyToolContext

func EmptyToolContext() *ToolContext

func NewToolContext

func NewToolContext(tools []interface{}) *ToolContext

func (*ToolContext) Copy

func (c *ToolContext) Copy() *ToolContext

func (*ToolContext) Equal added in v0.0.5

func (c *ToolContext) Equal(other *ToolContext) bool

func (*ToolContext) Flatten

func (c *ToolContext) Flatten() []interface{}

func (*ToolContext) FunctionTools

func (c *ToolContext) FunctionTools() map[string]Tool

func (*ToolContext) GetFunctionTool

func (c *ToolContext) GetFunctionTool(name string) Tool

func (*ToolContext) Merge added in v0.0.5

func (c *ToolContext) Merge(other *ToolContext)

func (*ToolContext) ParseFunctionTools added in v0.0.5

func (c *ToolContext) ParseFunctionTools(format string) []map[string]any

func (*ToolContext) ProviderTools

func (c *ToolContext) ProviderTools() []ProviderTool

func (*ToolContext) Toolsets

func (c *ToolContext) Toolsets() []Toolset

func (*ToolContext) UpdateTools

func (c *ToolContext) UpdateTools(tools []interface{}) error

type ToolWithArgs added in v0.0.5

type ToolWithArgs interface {
	Tool
	Args() any
}

ToolWithArgs indicates that a tool has a specific struct for its arguments.

type ToolWithReply added in v0.0.5

type ToolWithReply interface {
	Tool
	IsReplyRequired() bool
}

ToolWithReply indicates that a tool specifically requires or does not require an LLM reply.

type Toolset

type Toolset interface {
	ID() string
	Tools() []interface{}
}

Jump to

Keyboard shortcuts

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