Documentation
¶
Index ¶
- Variables
- func HashMessages(salt string, inputs []Message) string
- func TransformByPrefix(prefix string) func(string) string
- type CachedModelOpt
- type ConcurrentLimiter
- type Encoder
- type FakeReasoningModelOpt
- type FeedbackGenerator
- type GeminiModelOpt
- type ImageAttachment
- type Message
- type Model
- func NewCachedModel(model Model, cache ModelResponseCache, opts ...CachedModelOpt) Model
- func NewConcurrentLimitedModel(model Model, limiter ConcurrentLimiter) Model
- func NewFakeReasoningModel(reasoner Model, answerer Model, opts ...FakeReasoningModelOpt) Model
- func NewGeminiModel(key, modelName string, opts ...GeminiModelOpt) Model
- func NewLoggingModel(model Model, logger ModelLogger) Model
- func NewOpenAIModel(key, modelName string, opts ...OpenAIModelOpt) Model
- func NewRateLimitedModel(model Model, limiter *rate.Limiter) Model
- func NewRetryChainModel(models []Model) Model
- func NewRetryModel(model Model, maxRetries int, opts ...RetryModelOpt) Model
- func NewTimeoutModel(model Model, timeout time.Duration) Model
- func NewUsageCountingModel(model Model, counter *UsageCounter) Model
- type ModelLogger
- type ModelLoggingInfo
- type ModelResponse
- type ModelResponseCache
- type OpenAIModelOpt
- type Parser
- type Pipeline
- func NewFallbackPipeline[T, U any](encoder Encoder[T], parser Parser[U], validator Validator[T, U], ...) Pipeline[T, U]
- func NewFeedbackPipeline[T, U any](encoder Encoder[T], parser Parser[U], validator Validator[T, U], ...) Pipeline[T, U]
- func NewOneShotPipeline[T, U any](encoder Encoder[T], parser Parser[U], validator Validator[T, U], model Model) Pipeline[T, U]
- type ReasoningEffort
- type RetryModelOpt
- type Role
- type Usage
- type UsageCounter
- type Validator
- type Verbosity
- type WithDelay
- type WithHTTPHeader
- type WithJsonSchema
- type WithMaxOutputTokens
- type WithMessagePrefix
- type WithPrediction
- type WithPresencePenalty
- type WithReasoningAs
- type WithReasoningEffort
- type WithReasoningPrompt
- type WithSalt
- type WithStreamResponse
- type WithSystemAs
- type WithTemperature
- type WithTopP
- type WithURL
- type WithVerbosity
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidResponse = errors.New("llm produced an invalid response")
)
Functions ¶
func HashMessages ¶ added in v0.6.0
func TransformByPrefix ¶ added in v0.8.0
Types ¶
type CachedModelOpt ¶ added in v0.8.2
type CachedModelOpt interface {
// contains filtered or unexported methods
}
type ConcurrentLimiter ¶ added in v0.7.0
type ConcurrentLimiter chan struct{}
func NewMaxConcurrentLimiter ¶ added in v0.7.0
func NewMaxConcurrentLimiter(n int) ConcurrentLimiter
NewMaxConcurrentLimiter creates a ConcurrentLimiter that allows up to n concurrent operations. The limiter is implemented as a buffered channel with capacity n.
func NewOneConcurrentLimiter ¶ added in v0.7.0
func NewOneConcurrentLimiter() ConcurrentLimiter
NewOneConcurrentLimiter creates a ConcurrentLimiter that allows only one operation at a time. This is a convenience function equivalent to NewMaxConcurrentLimiter(1).
type Encoder ¶ added in v0.9.0
Encoder encodes a structured piece of data into a set of messages for an LLM.
func NewFixedEncoder ¶ added in v0.9.0
NewFixedEncoder creates an Encoder that encodes a static system prompt and raw user input as messages.
func NewTemplateEncoder ¶ added in v0.9.0
NewTemplateEncoder creates a Encoder that uses Go's text/template for formatting messages. It accepts templates for both system and user messages, allowing dynamic content insertion. The data parameter to BuildInputMessages should be a struct or map with fields accessible to the template. If either systemTemplate or userTemplate is an empty string, that message will be skipped.
type FakeReasoningModelOpt ¶ added in v0.8.0
type FakeReasoningModelOpt interface {
// contains filtered or unexported methods
}
type FeedbackGenerator ¶ added in v0.6.0
FeedbackGenerator takes an error and converts it to a piece of text feedback to send to the LLM.
func NewRawMessageFeedbackGenerator ¶ added in v0.6.0
func NewRawMessageFeedbackGenerator() FeedbackGenerator
NewRawMessageFeedbackGenerator creates a FeedbackGenerator that formats feedback by returning the error message as a string.
type GeminiModelOpt ¶ added in v0.8.0
type GeminiModelOpt interface {
// contains filtered or unexported methods
}
type ImageAttachment ¶ added in v0.7.0
func (*ImageAttachment) ToBase64Encoded ¶ added in v0.7.0
func (i *ImageAttachment) ToBase64Encoded(useCompression bool) (string, error)
type Message ¶
type Message struct {
Role Role
Content string
Images []ImageAttachment
}
Message defines a text message to/from an LLM.
type Model ¶
type Model interface {
// Responds to a set of input messages.
Respond(context.Context, []Message) (ModelResponse, error)
}
Model defines an interface to an LLM.
func NewCachedModel ¶ added in v0.7.0
func NewCachedModel(model Model, cache ModelResponseCache, opts ...CachedModelOpt) Model
NewCachedModel wraps a Model with response caching functionality. It stores responses in the provided ModelResponseCache implementation, returning cached results for identical input messages and salts to avoid redundant model calls.
func NewConcurrentLimitedModel ¶ added in v0.7.0
func NewConcurrentLimitedModel(model Model, limiter ConcurrentLimiter) Model
NewConcurrentLimitedModel wraps a Model with concurrency control. It ensures that only a limited number of concurrent calls can be made to the underlying model, using the provided ConcurrentLimiter to manage access.
func NewFakeReasoningModel ¶
func NewFakeReasoningModel(reasoner Model, answerer Model, opts ...FakeReasoningModelOpt) Model
NewFakeReasoningModel creates a model that uses two underlying models to simulate reasoning. It first calls the reasoner model to generate reasoning about the input messages, then passes that reasoning along with the original messages to the answerer model. The reasoning is included as a ReasoningRole message in the auxiliary messages output. Optional parameters allow customization of the reasoning prompt.
func NewGeminiModel ¶ added in v0.8.0
func NewGeminiModel(key, modelName string, opts ...GeminiModelOpt) Model
NewGeminiModel creates a Model that uses the Google Gemini API. It requires an API key and model name, with optional configuration via variadic options.
func NewLoggingModel ¶ added in v0.7.0
func NewLoggingModel(model Model, logger ModelLogger) Model
NewLoggingModel wraps a Model with logging functionality. It logs all interactions with the model using the provided ModelLogger. Each model call is logged with input messages, output messages, usage statistics, and timing information.
func NewOpenAIModel ¶ added in v0.7.0
func NewOpenAIModel(key, modelName string, opts ...OpenAIModelOpt) Model
NewOpenAIModel creates a Model that uses the OpenAI API. It requires an API key and model name, with optional configuration via variadic options.
func NewRateLimitedModel ¶ added in v0.9.0
func NewRetryChainModel ¶ added in v0.8.2
NewRetryChainModel creates a Model that tries a list of models in order, returning the result from the first one that doesn't fail. If all models fail, it returns a joined error containing all the errors.
func NewRetryModel ¶
func NewRetryModel(model Model, maxRetries int, opts ...RetryModelOpt) Model
NewRetryModel wraps a Model with retry functionality. If the underlying model returns an error, this wrapper will retry the operation up to a configurable number of times with an optional delay between retries.
func NewTimeoutModel ¶ added in v0.9.0
NewTimeoutModel creates a new model that will cause the context of the child to timeout, a specified duration after Respond is called. Caution: It only tells the context to timeout - it will not forecfully stop the child model if it does not respect the context.
func NewUsageCountingModel ¶ added in v0.7.0
func NewUsageCountingModel(model Model, counter *UsageCounter) Model
NewUsageCountingModel wraps a Model with token usage tracking functionality. It aggregates token usage statistics in the provided UsageCounter, which allows monitoring total token consumption across multiple model calls.
type ModelLogger ¶ added in v0.7.0
type ModelLogger interface {
ModelLog(ModelLoggingInfo) error
}
ModelLogger specifies a method of logging a call to a model.
func NewJsonModelLogger ¶ added in v0.7.0
func NewJsonModelLogger(to io.Writer) ModelLogger
NewJsonModelLogger creates a ModelLogger that outputs logs in JSON format. The logs are written to the provided io.Writer, with each log entry being a JSON object containing the model interaction details.
func NewSlogModelLogger ¶ added in v0.8.0
func NewSlogModelLogger(logFunc func(string, ...any), logMessages bool) ModelLogger
Logs calls made to the model to a slog-style logging function. Can optionally log the model messages too (this is very spammy).
type ModelLoggingInfo ¶ added in v0.6.0
type ModelLoggingInfo struct {
Messages []Message
ResponseAuxMessages []Message
ResponseFinalMessage Message
Usage Usage
Err error
Duration time.Duration
}
ModelLoggingInfo contains all information about a model interaction to be logged. It includes input messages, output messages, usage statistics, and any error that occurred.
type ModelResponse ¶ added in v0.8.0
type ModelResponse struct {
// Extra messages that are not the final response,
// but were used to build up the final response.
// For example, reasoning messages.
AuxiliaryMessages []Message
// The primary response to the users query.
// Usually the only response that matters.
PrimaryMessage Message
// The usage of making this call.
// This may be the sum of multiple LLM calls.
Usage Usage
}
func (ModelResponse) IncludingUsage ¶ added in v0.8.0
func (r ModelResponse) IncludingUsage(u Usage) ModelResponse
Utility to include another usage object in this response object
func (ModelResponse) OnlyUsage ¶ added in v0.8.0
func (r ModelResponse) OnlyUsage() ModelResponse
Utility to allow you to return the usage but 0 value messages when an error occurs.
type ModelResponseCache ¶ added in v0.6.0
type ModelResponseCache interface {
GetCachedResponse(ctx context.Context, salt string, inputs []Message) (bool, []Message, Message, error)
SetCachedResponse(ctx context.Context, salt string, inputs []Message, aux []Message, out Message) error
}
func NewFilePersistCache ¶ added in v0.9.0
func NewFilePersistCache(filename string) (ModelResponseCache, error)
NewFilePersistCache creates an in-memory cache that persists to the given filename. On creation, it loads the cache from the file (if it exists). Whenever SetCachedResponse is called, the entire cache is saved back to the file.
func NewInMemoryCache ¶ added in v0.6.0
func NewInMemoryCache() ModelResponseCache
NewInMemoryCache creates an in-memory implementation of ModelResponseCache. It stores model responses in memory using a hash of the input messages as a key.
func NewSQLCache ¶ added in v0.7.3
type OpenAIModelOpt ¶ added in v0.8.0
type OpenAIModelOpt interface {
// contains filtered or unexported methods
}
type Parser ¶ added in v0.9.0
Parser converts the LLM response into a structured piece of output data. When the LLM response is invalid, it should return ErrInvalidResponse (or an error joined on that).
func NewJsonParser ¶ added in v0.9.0
NewJsonParser creates a Parser that tries to parse a json object from the response. It can ONLY parse json objects with an OBJECT as top level (i.e. it cannot parse a list directly).
func NewStringParser ¶ added in v0.9.0
NewStringParser creates a Parser that returns the response as a raw string without modification.
func NewSubstringAfterParser ¶ added in v0.9.0
Wrap an existing Parser with one that takes only part of the response after the separator into account. If an error is detected when getting the substring, ErrInvalidResponse is raised.
func NewSubstringParser ¶ added in v0.9.0
Wrap an existing Parser with one that takes only the part of interest of the response into account. The part of interest is determined by the substring function. If an error is detected when getting the substring, ErrInvalidResponse is raised.
type Pipeline ¶ added in v0.9.0
Pipeline transforms input of type T into output of type U using an LLM. It handles the encoding of input, interaction with the LLM, and decoding of output.
func NewFallbackPipeline ¶ added in v0.9.0
func NewFallbackPipeline[T, U any]( encoder Encoder[T], parser Parser[U], validator Validator[T, U], models ...Model, ) Pipeline[T, U]
Creates a Pipeline that first tries to ask the first model, and if that produces an invalid format will try to ask the next models until a valid format is found. This is useful, for example, to try a second time with a model that overwrites the cache.
func NewFeedbackPipeline ¶ added in v0.9.0
func NewFeedbackPipeline[T, U any]( encoder Encoder[T], parser Parser[U], validator Validator[T, U], feedbackGenerator FeedbackGenerator, model Model, feedbackRole Role, maxRetries int, ) Pipeline[T, U]
NewFeedbackPipeline creates a Pipeline that first runs the encoder, then the model, finally parsing the response with the decoder. However, it adds feedback to the conversation when errors are detected. It will only add to the conversation if the error returned from the parser is an ErrInvalidResponse (using errors.Is).
type ReasoningEffort ¶
type ReasoningEffort uint8
ReasoningEffort defines how hard a reasoning model should think.
const ( LowReasoning ReasoningEffort = iota MediumReasoning HighReasoning )
type RetryModelOpt ¶ added in v0.8.0
type RetryModelOpt interface {
// contains filtered or unexported methods
}
type Role ¶
type Role uint8
Role is an enum specifying a role for a message. It is not 1:1 with openai roles (i.e. there is a reasoning role here).
type UsageCounter ¶ added in v0.6.0
type UsageCounter struct {
// contains filtered or unexported fields
}
Counts up the sum usage. Is completely concurrent-safe.
func NewUsageCounter ¶ added in v0.6.0
func NewUsageCounter() *UsageCounter
NewUsageCounter creates a new UsageCounter with zero initial usage. The counter is safe for concurrent use across multiple goroutines.
func (*UsageCounter) Add ¶ added in v0.6.0
func (u *UsageCounter) Add(usage Usage)
Add the given usage to the counter.
func (*UsageCounter) Get ¶ added in v0.6.0
func (u *UsageCounter) Get() Usage
Get the current usage in the counter.
type Validator ¶ added in v0.9.0
Validator takes a parsed LLM response and validates it against the input. When the LLM response is invalid, it should return ErrInvalidResponse (or an error joined on that).
type WithHTTPHeader ¶ added in v0.7.0
type WithJsonSchema ¶ added in v0.8.0
type WithMaxOutputTokens ¶ added in v0.8.0
type WithMaxOutputTokens struct{ X int }
type WithMessagePrefix ¶ added in v0.8.0
type WithMessagePrefix struct{ X string }
type WithPrediction ¶ added in v0.8.0
type WithPrediction struct{ X string }
type WithPresencePenalty ¶ added in v0.8.0
type WithPresencePenalty struct{ X float64 }
type WithReasoningAs ¶ added in v0.8.0
type WithReasoningEffort ¶ added in v0.7.0
type WithReasoningEffort struct{ X ReasoningEffort }
type WithReasoningPrompt ¶ added in v0.7.0
type WithReasoningPrompt struct{ X string }
type WithStreamResponse ¶ added in v0.9.0
type WithStreamResponse struct {
// Called when the stream begins, may be called multiple times if retries occur.
OnBegin func()
// Called when a new chunk of text is received.
OnText func(string)
}
type WithSystemAs ¶ added in v0.8.0
type WithTemperature ¶ added in v0.7.0
type WithTemperature struct{ X float64 }
type WithVerbosity ¶ added in v0.8.0
type WithVerbosity struct{ X Verbosity }
Source Files
¶
- cache.go
- cache_memory.go
- cache_persistmemory.go
- cache_sql.go
- encoder.go
- encoder_fixed.go
- encoder_template.go
- feedgen.go
- feedgen_rawmessage.go
- logger.go
- logger_json.go
- logger_slog.go
- messages.go
- model.go
- model_cached.go
- model_concurrent_limited.go
- model_fakereason.go
- model_gemini.go
- model_logging.go
- model_openai.go
- model_ratelimited.go
- model_retry.go
- model_retrychain.go
- model_timeout.go
- model_usage_counting.go
- parser.go
- parser_json.go
- parser_rawstring.go
- parser_substring.go
- pipeline.go
- pipeline_feedback.go
- pipeline_modelfallback.go
- pipeline_oneshot.go
- utils.go