Documentation
¶
Index ¶
- Constants
- func CosineSimilarity(vec1, vec2 []float64) (float64, error)
- func RunAgent[T, U any](model Model, agent Agent[T, U], initialState T) iter.Seq[AgentStep[T, U]]
- type Agent
- type AgentStep
- type Embedder
- type Function
- type Message
- type Model
- func NewFakeReasoningModel(reasoner Model, answerer Model, reasoningPrompt string) Model
- func NewReasoningOpenAIModel(key, modelName string, maxInput, maxOutput int, ...) Model
- func NewRetryModel(model Model, tries int, delay time.Duration) Model
- func NewStandardOpenAIModel(key, modelName string, maxInput, maxOutput int, temperature float64) Model
- type ParseError
- type ReasoningEffort
- type RetryFunction
- type Role
- type Usage
Constants ¶
const DefaultFakeReasoningPromptA = `` /* 525-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
func CosineSimilarity ¶
CosineSimilarity takes the cosine similarity between two vectors.
Types ¶
type Agent ¶
type Agent[T, U any] interface { // Function is the functin that is called to generate the next action of the agent. Function[T, U] // Handle integrates the given action into the state. // It returns a new state, a boolean that is tru if that action was terminal, and a terminal error (if any). Handle(T, U) (T, bool, error) }
Agent[state, action] defines some agentic behaviour. It does not contain any state or models itself, only configuration. Abstractly, an agent is somthing that can pick a next action from the given state, then apply that action to its state.
type AgentStep ¶
type AgentStep[T, U any] struct { // State is the newest state of the agent (after taking the action). State T // Action is the most recent action to be taken. Action U // Error is only populated when somthing unrecoverable happened (stopping iteration). Error error }
AgentStep[state, action] defines a step the agent has taken this iteration.
type Embedder ¶
Embedder defines an object that is capable of embedding a string into a vector.
func NewOpenAIEmbedder ¶
NewOpenAIEmbedder creates a new embedding model that uses the openai API.
type Function ¶
type Function[T, U any] interface { // Create the input messages from the input value BuildInputMessages(T) ([]Message, error) // Parse the raw LLM response into an output value, // returning a [ParseError] if the response cannot be parsed, or another error if somthing else. ParseResponseText(string) (U, error) }
Function[input, output] is a short-lived task-specific LLM configuration. They are intended to be used to perform single tasks, and should not be used for long-running conversations.
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 NewFakeReasoningModel ¶
Creates a new model that simulates reasoning by asking one model to reason and the other to answer.
func NewReasoningOpenAIModel ¶
func NewReasoningOpenAIModel(key, modelName string, maxInput, maxOutput int, reasoningEffort ReasoningEffort) Model
NewReasoningOpenAIModel creates a new reasoning model (i.e. o1, o3, ...) from openai.
func NewRetryModel ¶
NewRetryModel wraps a model such that it will retry calling it if an error occurs, with intermediate delays.
type ParseError ¶
type ParseError struct {
// The latest response of the LLM
Response string
// The error that occured
Err error
}
A parse error is a specific type of error that is created when a function fails to parse an LLM response
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type ReasoningEffort ¶
type ReasoningEffort uint8
ReasoningEffort defines how hard a reasoning model should think.
const ( LowReasoning ReasoningEffort = iota MediumReasoning HighReasoning )
type RetryFunction ¶
type RetryFunction[T, U any] interface { Function[T, U] // Format the feedback from this parse error FormatFeedback(*ParseError) string }
A retry function is a special type of function that can provide feedback to the LLM when the parse failed. It is still not intended for long-running conversations, however under the hood it does create a long conversation until the parse is sucsessful.
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 Usage ¶
Usage defines how many tokens were used when making calls to LLMs.
func RunOneShot ¶
Runs a function with one try, i.e. it asks the LLM once and tries to parse once.
func RunWithRetries ¶
func RunWithRetries[T, U any](model Model, f RetryFunction[T, U], maxRetries int, feedbackRole Role, input T) (U, Usage, error)
Runs a function with a number of retries, providing feedback at each parse fail, i.e. asks the llm the inital messages at the start of the conversation and continues to provide feedback until the answer is parseable.