ai

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoveSurroundingMarkdown

func RemoveSurroundingMarkdown(content string) string

RemoveSurroundingMarkdown removes first and last lines if they start with ``` and removes empty lines at the beginning and end

func RenderTemplateString

func RenderTemplateString(tpl string, data map[string]string) (string, error)

func SaveInteractionsToFile

func SaveInteractionsToFile(interactions []Interaction, filename string) error

SaveInteractionsToFile is a convenience function to save interactions to a file

Types

type Attr

type Attr struct {
	Key   string
	Value string
}

An Attr is a key-value pair.

func MapToAttr

func MapToAttr(attrs map[string]string) []Attr

func StringsToAttr

func StringsToAttr(attrs []string) []Attr

map string slice to Attr slice

type CaptureConfig

type CaptureConfig struct {
	Enabled      bool
	DebugMode    bool
	OutputFile   string
	ProviderName string
}

CaptureConfig configures the capture middleware

func GetCaptureConfigFromEnv

func GetCaptureConfigFromEnv(providerName string) CaptureConfig

GetCaptureConfigFromEnv creates capture config from environment variables

type CaptureMiddleware

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

CaptureMiddleware wraps any AI Gen implementation to capture interactions

func EnableCaptureForTesting

func EnableCaptureForTesting(underlying Gen) *CaptureMiddleware

EnableCaptureForTesting enables capture with in-memory storage for testing

func EnableDebugCaptureForTesting

func EnableDebugCaptureForTesting(underlying Gen) *CaptureMiddleware

EnableDebugCaptureForTesting enables capture with debug output for testing

func (*CaptureMiddleware) CountTokens

func (c *CaptureMiddleware) CountTokens(ctx context.Context, p Prompt, debug bool, args ...string) (*TokenCount, error)

CountTokens delegates to the underlying LLM client

func (*CaptureMiddleware) CountTokensAttr

func (c *CaptureMiddleware) CountTokensAttr(ctx context.Context, p Prompt, debug bool, attrs []Attr) (*TokenCount, error)

CountTokens delegates to the underlying LLM client

func (*CaptureMiddleware) GenerateContent

func (c *CaptureMiddleware) GenerateContent(ctx context.Context, prompt Prompt, debug bool, args ...string) (string, error)

GenerateContent implements the Gen interface with capture

func (*CaptureMiddleware) GenerateContentAttr

func (c *CaptureMiddleware) GenerateContentAttr(ctx context.Context, prompt Prompt, debug bool, attrs []Attr) (string, error)

GenerateContentAttr implements the Gen interface with capture

func (*CaptureMiddleware) GetCapture

func (c *CaptureMiddleware) GetCapture() *InteractionCapture

GetCapture returns the underlying capture for inspection

func (*CaptureMiddleware) GetCapturedInteractions

func (c *CaptureMiddleware) GetCapturedInteractions() []Interaction

GetCapturedInteractions returns all captured interactions

func (*CaptureMiddleware) GetLastInteraction

func (c *CaptureMiddleware) GetLastInteraction() *Interaction

GetLastInteraction returns the most recent interaction

func (*CaptureMiddleware) GetStatus

func (c *CaptureMiddleware) GetStatus() *Status

GetStatus delegates to the underlying LLM client

func (*CaptureMiddleware) LoadCapture

func (c *CaptureMiddleware) LoadCapture(filename string) error

LoadCapture loads interactions from a file (for replay scenarios)

func (*CaptureMiddleware) PrintCaptureSummary

func (c *CaptureMiddleware) PrintCaptureSummary()

PrintCaptureSummary prints a summary of captured interactions

func (*CaptureMiddleware) SaveCapture

func (c *CaptureMiddleware) SaveCapture(filename string) error

SaveCapture saves captured interactions to a file

type CapturedAttr

type CapturedAttr struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

CapturedAttr represents an attribute for serialization

type CapturedError

type CapturedError struct {
	Message string `json:"message"`
	Type    string `json:"type"`
}

CapturedError represents an error that can be serialized

type CapturedFunction

type CapturedFunction struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters,omitempty"`
}

CapturedFunction represents a function declaration for serialization

type CapturedPrompt

type CapturedPrompt struct {
	Name        string                 `json:"name"`
	Text        string                 `json:"text"`
	Instruction string                 `json:"instruction"`
	Functions   []CapturedFunction     `json:"functions,omitempty"`
	Context     map[string]interface{} `json:"context,omitempty"`
}

CapturedPrompt represents a prompt that can be serialized

type FunctionDeclaration

type FunctionDeclaration struct {
	Name        string
	Description string
	Parameters  *Schema
	Response    *Schema
}

type FunctionResponse

type FunctionResponse struct {
	Name     string
	Response map[string]any
}

type Gen

type Gen interface {
	GenerateContent(ctx context.Context, p Prompt, debug bool, args ...string) (string, error)
	GenerateContentAttr(ctx context.Context, prompt Prompt, debug bool, attrs []Attr) (string, error)
	CountTokens(ctx context.Context, p Prompt, debug bool, args ...string) (*TokenCount, error)
	CountTokensAttr(ctx context.Context, p Prompt, debug bool, attrs []Attr) (*TokenCount, error)
	GetStatus() *Status
}

func NewCaptureMiddleware

func NewCaptureMiddleware(underlying Gen, config CaptureConfig) Gen

NewCaptureMiddleware creates a new capture middleware

type HandlerFunc

type HandlerFunc func(ctx context.Context, attr map[string]any) (map[string]any, error)

type Image

type Image struct {
	Type     string `yaml:"type"`
	Filename string `yaml:"filename"`
	Data     []byte `yaml:"data"`
}

type Interaction

type Interaction struct {
	ID          string                 `json:"id"`
	Timestamp   time.Time              `json:"timestamp"`
	Prompt      CapturedPrompt         `json:"prompt"`
	Args        []string               `json:"args"`
	Attrs       []CapturedAttr         `json:"attrs,omitempty"`
	Response    string                 `json:"response"`
	Error       *CapturedError         `json:"error,omitempty"`
	Duration    time.Duration          `json:"duration"`
	LLMProvider string                 `json:"llm_provider"`
	Tools       []string               `json:"tools"`
	Context     map[string]interface{} `json:"context,omitempty"`
	Debug       bool                   `json:"debug"`
}

Interaction represents a complete LLM interaction for capture and replay

func LoadInteractionsFromFile

func LoadInteractionsFromFile(filename string) ([]Interaction, error)

LoadInteractionsFromFile is a convenience function to load interactions from a file

type InteractionCapture

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

InteractionCapture manages the recording and storage of LLM interactions

func NewInteractionCapture

func NewInteractionCapture() *InteractionCapture

NewInteractionCapture creates a new interaction capture instance

func (*InteractionCapture) Clear

func (ic *InteractionCapture) Clear()

Clear removes all captured interactions

func (*InteractionCapture) CompleteInteraction

func (ic *InteractionCapture) CompleteInteraction(interaction *Interaction, response string, err error, duration time.Duration)

CompleteInteraction finishes recording an interaction

func (*InteractionCapture) GetInteractionByID

func (ic *InteractionCapture) GetInteractionByID(id string) *Interaction

GetInteractionByID finds an interaction by its ID

func (*InteractionCapture) GetInteractions

func (ic *InteractionCapture) GetInteractions() []Interaction

GetInteractions returns all captured interactions

func (*InteractionCapture) GetLastInteraction

func (ic *InteractionCapture) GetLastInteraction() *Interaction

GetLastInteraction returns the most recent interaction

func (*InteractionCapture) GetSummary

func (ic *InteractionCapture) GetSummary() string

GetSummary returns a human-readable summary of captured interactions

func (*InteractionCapture) LoadFromFile

func (ic *InteractionCapture) LoadFromFile(filename string) error

LoadFromFile loads interactions from a JSON file

func (*InteractionCapture) SaveToFile

func (ic *InteractionCapture) SaveToFile(filename string) error

SaveToFile saves all captured interactions to a JSON file

func (*InteractionCapture) SetOutputFile

func (ic *InteractionCapture) SetOutputFile(filename string)

SetOutputFile configures where interactions should be saved

func (*InteractionCapture) StartInteraction

func (ic *InteractionCapture) StartInteraction(prompt Prompt, args []string) *Interaction

StartInteraction begins recording a new interaction

type MockGen

type MockGen struct {
	ResponseQueue []string
	CallCounts    map[string]int
	UsedPrompts   []Prompt
	LastAttrs     []Attr
	// contains filtered or unexported fields
}

MockGen implements the Gen interface for testing

func NewSharedMockGen

func NewSharedMockGen() *MockGen

NewSharedMockGen creates a new mock generator for testing

func (*MockGen) CountTokens

func (m *MockGen) CountTokens(ctx context.Context, p Prompt, debug bool, args ...string) (*TokenCount, error)

func (*MockGen) GenerateContent

func (m *MockGen) GenerateContent(ctx context.Context, prompt Prompt, debug bool, args ...string) (string, error)

GenerateContent implements the Gen interface

func (*MockGen) GenerateContentAttr

func (m *MockGen) GenerateContentAttr(ctx context.Context, prompt Prompt, debug bool, attrs []Attr) (string, error)

GenerateContentAttr implements the Gen interface

func (*MockGen) GetStatus

func (m *MockGen) GetStatus() *Status

type Prompt

type Prompt struct {
	Name           string   `yaml:"name"`
	Instruction    string   `yaml:"instruction"`
	Text           string   `yaml:"text"`
	Images         []*Image `yaml:"images"`
	RequiredTools  []string `yaml:"required_tools"`
	Functions      []*FunctionDeclaration
	ResponseSchema *Schema                `yaml:"response_schema"`
	Handlers       map[string]HandlerFunc `yaml:"-"`
	ModelName      string                 `yaml:"model_name"`
	MaxTokens      int32                  `yaml:"max_tokens"`
	Temperature    float32                `yaml:"temperature"`
	TopP           float32                `yaml:"top_p"`
}

func RenderPrompt

func RenderPrompt(base Prompt, data map[string]string) (Prompt, error)

RenderPrompt takes a base prompt and renders it with the given data.

type RetryConfig added in v0.1.6

type RetryConfig struct {
	Enabled        bool
	MaxRetries     int
	InitialBackoff time.Duration
}

RetryConfig configures the retry middleware

func GetRetryConfigFromEnv added in v0.1.6

func GetRetryConfigFromEnv(configManager config.Manager) RetryConfig

GetRetryConfigFromEnv creates retry config from environment variables

type RetryMiddleware added in v0.1.6

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

RetryMiddleware wraps an AI Gen implementation to add retry logic

func NewRetryMiddleware added in v0.1.6

func NewRetryMiddleware(underlying Gen, config RetryConfig) *RetryMiddleware

NewRetryMiddleware creates a new RetryMiddleware

func (*RetryMiddleware) CountTokens added in v0.1.6

func (r *RetryMiddleware) CountTokens(ctx context.Context, p Prompt, debug bool, args ...string) (*TokenCount, error)

CountTokens delegates to the underlying LLM client

func (*RetryMiddleware) CountTokensAttr added in v0.1.6

func (r *RetryMiddleware) CountTokensAttr(ctx context.Context, p Prompt, debug bool, attrs []Attr) (*TokenCount, error)

CountTokensAttr delegates to the underlying LLM client

func (*RetryMiddleware) GenerateContent added in v0.1.6

func (r *RetryMiddleware) GenerateContent(ctx context.Context, p Prompt, debug bool, args ...string) (string, error)

GenerateContent implements the Gen interface with retry logic

func (*RetryMiddleware) GenerateContentAttr added in v0.1.6

func (r *RetryMiddleware) GenerateContentAttr(ctx context.Context, p Prompt, debug bool, attrs []Attr) (string, error)

GenerateContentAttr implements the Gen interface with retry logic

func (*RetryMiddleware) GetStatus added in v0.1.6

func (r *RetryMiddleware) GetStatus() *Status

GetStatus delegates to the underlying LLM client

type Schema

type Schema struct {
	Type          Type               `yaml:"type"`
	Format        string             `yaml:"format"`
	Title         string             `yaml:"title"`
	Description   string             `yaml:"description"`
	Nullable      bool               `yaml:"nullable"`
	Items         *Schema            `yaml:"items"`
	MinItems      int64              `yaml:"min_items"`
	MaxItems      int64              `yaml:"max_items"`
	Enum          []string           `yaml:"enum"`
	Properties    map[string]*Schema `yaml:"properties"`
	Required      []string           `yaml:"required"`
	MinProperties int64              `yaml:"min_properties"`
	MaxProperties int64              `yaml:"max_properties"`
	Minimum       float64            `yaml:"minimum"`
	Maximum       float64            `yaml:"maximum"`
	MinLength     int64              `yaml:"min_length"`
	MaxLength     int64              `yaml:"max_length"`
	Pattern       string             `yaml:"pattern"`
}

func ToSchema

func ToSchema(input interface{}) (*Schema, error)

ToSchema is the top-level function that takes an object (either a value or pointer) and returns a *Schema describing it.

type Status

type Status struct {
	Connected bool
	Model     string
	Backend   string
	Message   string
}

type TokenCount

type TokenCount struct {
	TotalTokens  int32
	InputTokens  int32
	OutputTokens int32
}

type Type

type Type int32
const (
	TypeString  Type = 1
	TypeNumber  Type = 2
	TypeInteger Type = 3
	TypeBoolean Type = 4
	TypeArray   Type = 5
	TypeObject  Type = 6
)

Jump to

Keyboard shortcuts

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