Documentation
¶
Index ¶
- Variables
- type AssistantMessage
- type DeveloperMessage
- type Encoder
- type FeedbackGenerator
- type ImageAttachment
- type Message
- type MessageDTO
- type MessageRole
- type Model
- type ModelLogger
- type ModelLoggingInfo
- type ModelResponse
- type ModelResponseCache
- type ModelResponseKwargs
- type ModelResponseOpt
- type ModelStreamer
- type Parser
- type Pipeline
- type PipelineResponse
- type SystemMessage
- type ToolArg
- type ToolArgType
- type ToolCall
- type ToolResultMessage
- type ToolSchema
- type Usage
- type UserMessage
- type Validator
Constants ¶
This section is empty.
Variables ¶
var (
ErrInvalidResponse = errors.New("llm produced an invalid response")
)
Functions ¶
This section is empty.
Types ¶
type AssistantMessage ¶ added in v0.10.0
AssistantMessage represents a message from the model to the user.
func (AssistantMessage) Eq ¶ added in v0.10.0
func (m AssistantMessage) Eq(other Message) bool
func (AssistantMessage) String ¶ added in v0.10.0
func (m AssistantMessage) String() string
type DeveloperMessage ¶ added in v0.10.0
type DeveloperMessage struct {
Content string
}
DeveloperMessage represents a message from the developer (basically system) to the model.
func (DeveloperMessage) Eq ¶ added in v0.10.0
func (m DeveloperMessage) Eq(other Message) bool
func (DeveloperMessage) String ¶ added in v0.10.0
func (m DeveloperMessage) String() string
type Encoder ¶ added in v0.9.0
Encoder encodes a structured piece of data into a set of messages for an LLM.
type FeedbackGenerator ¶ added in v0.6.0
type FeedbackGenerator interface {
FormatFeedback(AssistantMessage, error) string
}
FeedbackGenerator takes an error and converts it to a piece of text feedback to send to the LLM.
type ImageAttachment ¶ added in v0.7.0
ImageAttachment is an image that is attached as additional information to a message.
func (*ImageAttachment) ToBase64Encoded ¶ added in v0.7.0
func (i *ImageAttachment) ToBase64Encoded(useCompression bool) (string, error)
type Message ¶
type Message interface {
String() string
Eq(Message) bool
// contains filtered or unexported methods
}
Message is a sum type of the different messages that can be sent to Models.
type MessageDTO ¶ added in v0.11.0
type MessageDTO struct {
Role MessageRole `json:"role"`
// Text content, for user, assistant, developer and system messages.
Content string `json:"content,omitempty"`
// Images attached to a user message, each as a "data:image/png;base64,..." URI.
// ToMessage decodes these back into image.Image values; see the type doc for the
// round-trip caveats.
Images []string `json:"images,omitempty"`
// Requested tool calls, for assistant messages.
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
// The id of the tool call being responded to, for tool result messages.
CallID string `json:"call_id,omitempty"`
// The tool output, for tool result messages.
Result string `json:"result,omitempty"`
}
MessageDTO is a JSON-serialisable representation of a Message.
Populate it from a Message with LoadMessage, and convert it back with ToMessage. Only the fields relevant to the Role are set; the rest stay at their zero value and are omitted from JSON.
Round-tripping is not exactly lossless in two ways:
- Tool call arguments pass through JSON, so numeric values come back as float64 regardless of how they went in - the same normalisation the agent framework already applies to arguments returned by a model.
- Image attachments on a UserMessage are re-encoded as base64 PNG data URIs by LoadMessage and decoded into fresh image.Image values by ToMessage. The pixels survive, but any non-PNG source encoding does not, and because the decoded image is a new value the resulting UserMessage will not compare Eq to the original.
func (*MessageDTO) LoadMessage ¶ added in v0.11.0
func (d *MessageDTO) LoadMessage(msg Message) error
LoadMessage populates the DTO in place from msg, replacing any existing contents.
func (*MessageDTO) ToMessage ¶ added in v0.11.0
func (d *MessageDTO) ToMessage() (Message, error)
ToMessage converts the DTO into the concrete Message that its Role describes.
type MessageRole ¶ added in v0.11.0
type MessageRole string
MessageRole identifies which concrete Message type a MessageDTO represents.
const ( MessageRoleUser MessageRole = "user" MessageRoleAssistant MessageRole = "assistant" MessageRoleDeveloper MessageRole = "developer" MessageRoleSystem MessageRole = "system" MessageRoleToolResult MessageRole = "tool_result" )
type Model ¶
type Model interface {
// Responds to a set of input messages.
Respond(context.Context, []Message, ...ModelResponseOpt) (ModelResponse, error)
}
Model defines an interface to an LLM.
type ModelLogger ¶ added in v0.7.0
type ModelLogger interface {
ModelLog(ModelLoggingInfo) error
}
ModelLogger specifies a method of logging a call to a model.
type ModelLoggingInfo ¶ added in v0.6.0
type ModelLoggingInfo struct {
InputMessages []Message
ResultMessage AssistantMessage
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 {
// The response to the input messages.
Message AssistantMessage
// 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 ModelResponseKwargs ¶ added in v0.10.0
type ModelResponseKwargs struct {
Streamer ModelStreamer
OutputFormat any
ToolSchemas []ToolSchema
}
func GetModelResponseKwargs ¶ added in v0.10.0
func GetModelResponseKwargs(opts ...ModelResponseOpt) ModelResponseKwargs
type ModelResponseOpt ¶ added in v0.10.0
type ModelResponseOpt func(*ModelResponseKwargs)
func WithOutputFormat ¶ added in v0.10.0
func WithOutputFormat(format any) ModelResponseOpt
Set the output format (structured output). This should be a struct, as it will be processed into the correct API model output format by the model itself.
func WithStreamResponse ¶ added in v0.9.0
func WithStreamResponse(streamer ModelStreamer) ModelResponseOpt
Stream the model's response to the streamer. This will override any previously set streamers.
func WithToolSchemas ¶ added in v0.10.0
func WithToolSchemas(schemas ...ToolSchema) ModelResponseOpt
Add the provided tool schemas (additive, not replace) to the model. If there is at least one tool schema, tool calling will be enabled, so the model needs to support tool calling in this case.
type ModelStreamer ¶ added in v0.10.0
type ModelStreamer interface {
OnMessageBegin()
OnMessageText(text string)
OnMessageReset()
}
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).
type Pipeline ¶ added in v0.9.0
type Pipeline[T, U any] interface { Call(context.Context, T) (PipelineResponse[U], error) }
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.
type PipelineResponse ¶ added in v0.10.0
type PipelineResponse[U any] struct { // The parsed and validated result. Result U // The usage of making this call. // This may be the sum of multiple LLM calls. Usage Usage }
func (PipelineResponse[U]) IncludingUsage ¶ added in v0.10.0
func (r PipelineResponse[U]) IncludingUsage(u Usage) PipelineResponse[U]
Utility to include another usage object in this response object
func (PipelineResponse[U]) OnlyUsage ¶ added in v0.10.0
func (r PipelineResponse[U]) OnlyUsage() PipelineResponse[U]
Utility to allow you to return the usage but 0 value messages when an error occurs.
type SystemMessage ¶ added in v0.10.0
type SystemMessage struct {
Content string
}
SystemMessage represents a message from the system to the model, to set up its task, personality.
func (SystemMessage) Eq ¶ added in v0.10.0
func (m SystemMessage) Eq(other Message) bool
func (SystemMessage) String ¶ added in v0.10.0
func (m SystemMessage) String() string
type ToolArg ¶ added in v0.10.0
type ToolArg struct {
Name string
Description string
Type ToolArgType
Required bool
}
type ToolArgType ¶ added in v0.10.0
type ToolArgType uint8
const ( ToolArgInt ToolArgType = iota ToolArgFloat ToolArgString )
type ToolResultMessage ¶ added in v0.10.0
ToolResultMessage represents a result from calling a tool.
func (ToolResultMessage) Eq ¶ added in v0.10.0
func (m ToolResultMessage) Eq(other Message) bool
func (ToolResultMessage) String ¶ added in v0.10.0
func (m ToolResultMessage) String() string
type ToolSchema ¶ added in v0.10.0
type UserMessage ¶ added in v0.10.0
type UserMessage struct {
Content string
Images []ImageAttachment
}
UserMessage represents a message from the user to the model.
func (UserMessage) Eq ¶ added in v0.10.0
func (m UserMessage) Eq(other Message) bool
func (UserMessage) String ¶ added in v0.10.0
func (m UserMessage) String() string
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).