assembler

package
v1.9.3 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package assembler builds context strings from retrieved chunks for use in LLM prompts, with token budgeting and citation tracking.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssembleSimple

func AssembleSimple(results []retriever.Result) string

AssembleSimple creates a simple context string by joining chunk contents.

Types

type AssembleResult

type AssembleResult struct {
	// Context is the assembled context string for the LLM prompt.
	Context string `json:"context"`
	// Citations tracks which chunks were included.
	Citations []Citation `json:"citations,omitempty"`
	// TotalTokens is the estimated total token count.
	TotalTokens int `json:"total_tokens"`
	// TruncatedCount is the number of chunks that were dropped due to budget.
	TruncatedCount int `json:"truncated_count"`
}

AssembleResult contains the assembled context and metadata.

type Assembler

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

Assembler builds context strings from retrieved chunks.

func New

func New(opts ...Option) *Assembler

New creates a new Assembler with the given options.

func (*Assembler) Assemble

func (a *Assembler) Assemble(_ context.Context, results []retriever.Result) (*AssembleResult, error)

Assemble builds a context string from retrieved results within the token budget.

type BudgetManager

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

BudgetManager tracks token consumption against a budget.

func NewBudgetManager

func NewBudgetManager(counter TokenCounter, maxTokens int) *BudgetManager

NewBudgetManager creates a new budget manager.

func (*BudgetManager) CanFit

func (b *BudgetManager) CanFit(tokens int) bool

CanFit returns true if the given number of tokens fits in the remaining budget.

func (*BudgetManager) Consume

func (b *BudgetManager) Consume(tokens int)

Consume adds tokens to the used count.

func (*BudgetManager) EstimateTokens

func (b *BudgetManager) EstimateTokens(text string) int

EstimateTokens returns the token count for the given text.

func (*BudgetManager) Remaining

func (b *BudgetManager) Remaining() int

Remaining returns the tokens still available.

func (*BudgetManager) Used

func (b *BudgetManager) Used() int

Used returns the total tokens consumed.

type Citation

type Citation struct {
	// ChunkIndex is the position in the original retrieval results.
	ChunkIndex int `json:"chunk_index"`
	// Content is the chunk text.
	Content string `json:"content"`
	// Score is the retrieval relevance score.
	Score float64 `json:"score"`
	// Metadata from the source chunk.
	Metadata map[string]string `json:"metadata,omitempty"`
}

Citation tracks the provenance of a chunk included in the assembled context.

type CitationTracker

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

CitationTracker collects citations as chunks are included.

func (*CitationTracker) Add

func (t *CitationTracker) Add(c Citation)

Add records a citation.

func (*CitationTracker) Citations

func (t *CitationTracker) Citations() []Citation

Citations returns all recorded citations.

func (*CitationTracker) Count

func (t *CitationTracker) Count() int

Count returns the number of citations recorded.

type Option

type Option func(*Assembler)

Option configures the Assembler.

func WithMaxTokens

func WithMaxTokens(limit int) Option

WithMaxTokens sets the maximum token budget for assembled context.

func WithTemplate

func WithTemplate(t *Template) Option

WithTemplate sets a custom context template.

func WithTokenCounter

func WithTokenCounter(tc TokenCounter) Option

WithTokenCounter sets a custom token counter.

type SimpleTokenCounter

type SimpleTokenCounter struct{}

SimpleTokenCounter estimates tokens by dividing character count by 4.

func (*SimpleTokenCounter) CountTokens

func (c *SimpleTokenCounter) CountTokens(text string) int

CountTokens returns an approximate token count (~4 chars per token).

type Template

type Template struct {
	// Header is prepended to the assembled context.
	Header string
	// Footer is appended to the assembled context.
	Footer string
	// Separator is placed between chunks.
	Separator string
	// ChunkFormat formats each chunk. Use %d for index and %s for content.
	ChunkFormat string
}

Template defines how retrieved chunks are formatted into context.

func DefaultTemplate

func DefaultTemplate() *Template

DefaultTemplate returns a sensible default template.

func (*Template) Render

func (t *Template) Render(chunks []string) string

Render formats the chunks using this template.

type TokenCounter

type TokenCounter interface {
	// CountTokens returns the estimated number of tokens in the text.
	CountTokens(text string) int
}

TokenCounter estimates token counts for text.

Jump to

Keyboard shortcuts

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