Documentation
¶
Index ¶
- Variables
- func CosineSimilarity(vec1, vec2 []float64) (float64, error)
- func HashMessages(msgs []Message) string
- type Cache
- type ConcurrentLimiter
- type Embedder
- type EmbedderResponseCache
- type FeedbackGenerator
- type ImageAttachment
- type MapFunc
- type Message
- type MessageEncoder
- type Model
- func NewCachedModel(model Model, cache ModelResponseCache) Model
- func NewConcurrentLimitedModel(model Model, limiter ConcurrentLimiter) Model
- func NewFakeReasoningModel(reasoner Model, answerer Model, opts ...fakeReasoningModelOpt) Model
- func NewLoggingModel(model Model, logger ModelLogger) Model
- func NewOpenAIModel(key, modelName string, opts ...openAIModelOpt) Model
- func NewRetryModel(model Model, opts ...retryModelOpt) Model
- func NewSystemReasonModel(model Model, opts ...systemReasonOpt) Model
- func NewUsageCountingModel(model Model, counter *UsageCounter) Model
- type ModelLogger
- type ModelLoggingInfo
- type ModelResponseCache
- type ReasoningEffort
- type ResponseDecoder
- type Role
- type Usage
- type UsageCounter
- type WithDelay
- type WithHTTPHeader
- type WithReasoningEffort
- type WithReasoningPrefix
- type WithReasoningPrompt
- type WithRetries
- type WithTemperature
- type WithURL
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidResponse = errors.New("llm produced an invalid response")
)
Functions ¶
func CosineSimilarity ¶
CosineSimilarity takes the cosine similarity between two vectors.
func HashMessages ¶ added in v0.6.0
Types ¶
type Cache ¶ added in v0.7.3
type Cache interface {
ModelResponseCache
EmbedderResponseCache
}
func NewInMemoryCache ¶ added in v0.6.0
func NewInMemoryCache() Cache
NewInMemoryCache creates an in-memory implementation of ModelResponseCache. It stores model responses in memory using a hash of the input messages as a key.
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 Embedder ¶
Embedder defines an object that is capable of embedding a string into a vector.
func NewCachedEmbedder ¶ added in v0.7.3
func NewCachedEmbedder(emb Embedder, cache EmbedderResponseCache) Embedder
func NewOpenAIEmbedder ¶
type EmbedderResponseCache ¶ added in v0.7.3
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 ImageAttachment ¶ added in v0.7.0
func (*ImageAttachment) ToBase64Encoded ¶ added in v0.7.0
func (i *ImageAttachment) ToBase64Encoded(useCompression bool) (string, error)
type MapFunc ¶ added in v0.6.0
MapFunc 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 NewFeedbackMapFunc ¶ added in v0.6.0
func NewFeedbackMapFunc[T, U any]( enc MessageEncoder[T], pars ResponseDecoder[U], fed FeedbackGenerator, model Model, feedbackRole Role, maxRetries int, ) MapFunc[T, U]
NewFeedbackMapFunc creates a MapFunc that 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).
func NewOneShotMapFunc ¶ added in v0.6.0
func NewOneShotMapFunc[T, U any](enc MessageEncoder[T], pars ResponseDecoder[U], model Model) MapFunc[T, U]
type Message ¶
type Message struct {
Role Role
Content string
Images []ImageAttachment
}
Message defines a text message to/from an LLM.
type MessageEncoder ¶ added in v0.6.0
MessageEncoder encodes a structured piece of data into a set of messages for an LLM.
func NewRawStringMessageEncoder ¶ added in v0.6.0
func NewRawStringMessageEncoder(systemPrompt string) MessageEncoder[string]
NewRawStringMessageEncoder creates a MessageEncoder that encodes a system prompt and user input as raw string messages.
func NewTemplateMessageEncoder ¶ added in v0.7.0
func NewTemplateMessageEncoder[T any](systemTemplate, userTemplate string) MessageEncoder[T]
NewTemplateMessageEncoder creates a MessageEncoder 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 Model ¶
type Model interface {
// Tokens specifies how many tokens are allowed to be sent.
Tokens() (int, int)
// Responds to a set of input messages, with a set of auxilliary messages and a final message.
// There may be no auxilliary messages, or things like tool calls, function calls, and reasoning may go in the auxilliary messages,
Respond([]Message) ([]Message, Message, Usage, error)
}
Model defines an interface to an LLM.
func NewCachedModel ¶ added in v0.7.0
func NewCachedModel(model Model, cache ModelResponseCache) Model
NewCachedModel wraps a Model with response caching functionality. It stores responses in the provided ModelResponseCache implementation, returning cached results for identical input messages 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 ¶
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 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
NewOpenAIModel creates a Model that uses the OpenAI API. It requires an API key and model name, with optional configuration via variadic options.
func NewRetryModel ¶
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 NewSystemReasonModel ¶ added in v0.7.0
NewSystemReasonModel converts ReasoningRole messages to SystemRole messages. This allows using models that don't natively support a reasoning role by converting reasoning messages into system messages with a customizable prefix. Options: - WithReasoningPrefix: customizes the prefix text added before reasoning content (default provided)
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.
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 ModelResponseCache ¶ added in v0.6.0
type ReasoningEffort ¶
type ReasoningEffort uint8
ReasoningEffort defines how hard a reasoning model should think.
const ( LowReasoning ReasoningEffort = iota MediumReasoning HighReasoning )
type ResponseDecoder ¶ added in v0.6.0
ResponseDecoder converts an LLM response into a structured piece of data. When the LLM response is invalid, it should return ErrInvalidResponse (or an error joined on that).
func NewJsonResponseDecoder ¶ added in v0.7.0
func NewJsonResponseDecoder[T any]() ResponseDecoder[T]
NewJsonResponseDecoder creates a ResponseDecoder 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 NewRawStringResponseDecoder ¶ added in v0.6.0
func NewRawStringResponseDecoder() ResponseDecoder[string]
NewRawStringResponseDecoder creates a ResponseDecoder that returns the response as a raw string without modification.
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 WithHTTPHeader ¶ added in v0.7.0
type WithReasoningEffort ¶ added in v0.7.0
type WithReasoningEffort struct{ X ReasoningEffort }
type WithReasoningPrefix ¶ added in v0.7.0
type WithReasoningPrefix struct{ X string }
type WithReasoningPrompt ¶ added in v0.7.0
type WithReasoningPrompt struct{ X string }
type WithRetries ¶ added in v0.7.0
type WithRetries struct{ X int }
type WithTemperature ¶ added in v0.7.0
type WithTemperature struct{ X float64 }
Source Files
¶
- cache.go
- cache_memory.go
- cache_sql.go
- embedder.go
- embedder_cached.go
- embedder_openai.go
- feedgen.go
- feedgen_rawmessage.go
- mapfunc.go
- mapfunc_feedback.go
- mapfunc_oneshot.go
- messages.go
- model.go
- model_cached.go
- model_concurrent_limited.go
- model_fakereason.go
- model_logging.go
- model_openai.go
- model_retry.go
- model_systemreason.go
- model_usage_counting.go
- msgenc.go
- msgenc_rawstring.go
- msgenc_template.go
- respdec.go
- respdec_json.go
- respdec_rawstring.go