Documentation
¶
Overview ¶
// Package promptengine provides a low-allocation prompt runtime for building // bounded, context-aware LLM prompts in Go. // // The package is designed for high-throughput services that need predictable // memory behavior, reusable buffers, and a compact sliding-window memory model // for system/user/assistant prompt history.
Index ¶
- type Config
- type Engine
- func (e *Engine) AddAssistant(content string) error
- func (e *Engine) AddSystem(content string) error
- func (e *Engine) AddUser(content string) error
- func (e *Engine) CharCount() int
- func (e *Engine) Compile(dst []byte, vars map[string]string) []byte
- func (e *Engine) CompileTemplate(dst []byte, template string, vars map[string]string) []byte
- func (e *Engine) CompileTemplateWithContext(ctx context.Context, dst []byte, template string, vars map[string]string) []byte
- func (e *Engine) ExecuteWithProvider(ctx context.Context, provider Provider, template string, ...) (string, error)
- func (e *Engine) Len() int
- func (e *Engine) Push(role, content string) error
- func (e *Engine) Reset()
- func (e *Engine) Snapshot() []Message
- func (e *Engine) TokenCount() int
- type Message
- type Provider
- type ProviderFunc
- type Summarizer
- type Tokenizer
- type WhitespaceTokenizer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
MaxChars int
MaxMessages int
MaxTokens int
Tokenizer Tokenizer
Summarizer Summarizer
}
Config controls the sliding window budgets used by the engine.
MaxChars limits the total accumulated character length of the retained messages. MaxMessages limits the number of retained messages. MaxTokens provides an additional token-based guard for prompt size.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine compiles prompt templates with a reusable message window.
It is designed for low-allocation prompt construction in high-throughput Go services. The engine keeps a bounded sliding window of messages and prunes old entries when configured budgets are exceeded.
func NewEngine ¶
NewEngine creates a prompt engine with bounded memory usage.
If Config values are left at zero, the engine uses conservative production defaults intended for a reusable prompt runtime.
func (*Engine) AddAssistant ¶
AddAssistant appends an assistant message using the helper API.
func (*Engine) Compile ¶
Compile renders the current message window into dst using the provided vars.
func (*Engine) CompileTemplate ¶
CompileTemplate renders the current message window into dst using the supplied vars.
The function supports `{{var}}` replacements and a special `{{messages}}` placeholder. Callers should reuse a byte slice across requests when they want to minimize allocations on the hot path.
func (*Engine) CompileTemplateWithContext ¶
func (e *Engine) CompileTemplateWithContext(ctx context.Context, dst []byte, template string, vars map[string]string) []byte
CompileTemplateWithContext renders the current message window into dst using the supplied vars and context.
If the context is canceled before rendering begins, the method returns an empty output slice immediately.
func (*Engine) ExecuteWithProvider ¶
func (e *Engine) ExecuteWithProvider(ctx context.Context, provider Provider, template string, vars map[string]string) (string, error)
ExecuteWithProvider renders the current prompt and sends it to the supplied provider.
func (*Engine) Push ¶
Push stores a new role/content pair in the sliding window.
The engine normalizes the role and prunes the oldest messages if the configured character or token budgets would be exceeded. Empty content is ignored so callers can safely add blank messages.
func (*Engine) Reset ¶
func (e *Engine) Reset()
Reset clears the sliding window and returns pooled message blocks.
It resets the internal window state and returns the retained blocks to the pool so future use can reuse them without additional allocations.
func (*Engine) Snapshot ¶
Snapshot returns a copy of the active messages in oldest-to-newest order.
The returned slice preserves the current order of the message window, from oldest to newest.
func (*Engine) TokenCount ¶
TokenCount returns the current estimated token budget usage.
type ProviderFunc ¶
ProviderFunc adapts a function to the Provider interface.
type Summarizer ¶
Summarizer converts a set of messages into a compact summary string.
type WhitespaceTokenizer ¶
type WhitespaceTokenizer struct{}
WhitespaceTokenizer is a simple tokenizer based on whitespace-delimited tokens.
func (WhitespaceTokenizer) CountTokens ¶
func (WhitespaceTokenizer) CountTokens(text string) int
CountTokens counts whitespace-delimited tokens.