Documentation
¶
Index ¶
- type Chat
- func (chat *Chat) AddAssistantContent(content any) *Message
- func (chat *Chat) AddAssistantToolCall(toolCalls []ToolCall) *Message
- func (chat *Chat) AddMessage(message *Message)
- func (chat *Chat) AddMessageOnce(message *Message)
- func (chat *Chat) AddRoleContent(role string, content any) *Message
- func (chat *Chat) AddToolContent(name string, toolCallID string, content any) error
- func (chat *Chat) AddToolRawContent(name string, toolCallID string, content any) *Message
- func (chat *Chat) AddUserContent(content any) *Message
- func (chat *Chat) ClearMessages()
- func (chat *Chat) Delete(ctx context.Context, key string) error
- func (chat *Chat) LastMessage() *Message
- func (chat *Chat) LastMessageByRole(role string) *Message
- func (chat *Chat) LastMessageByType(contentType string) *Message
- func (chat *Chat) LastMessageRole() string
- func (chat *Chat) Load(ctx context.Context, key string) error
- func (chat *Chat) MessageCount() int
- func (chat *Chat) MessageCountByRole(role string) int
- func (chat *Chat) PopMessage() *Message
- func (chat *Chat) PopMessageIfRole(role string) *Message
- func (chat *Chat) Range(fn func(msg *Message) error) error
- func (chat *Chat) RangeByRole(role string, fn func(msg *Message) error) error
- func (chat *Chat) RangePendingToolCalls(fn func(toolCallContext *ToolCallContext) error) error
- func (chat *Chat) RemoveLastMessage() *Message
- func (chat *Chat) Save(ctx context.Context, key string) error
- func (chat *Chat) SetSystemContent(content any) *Message
- func (chat *Chat) SetSystemMessage(msg *Message) *Message
- func (chat *Chat) ShiftMessages() *Message
- func (chat *Chat) UnshiftMessages(msg *Message)
- type Function
- type Message
- type Meta
- type Options
- type Parameters
- type Part
- type Property
- type S3
- type Storage
- type Tool
- type ToolCall
- type ToolCallContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chat ¶
type Chat struct {
// ID is the unique identifier for the chat session
ID string `json:"id,omitempty"`
// Key is the storage key used for persistence
Key string `json:"key,omitempty"`
// Messages contains the chronological history of chat messages
Messages []*Message `json:"messages"`
// Created is the timestamp when the chat session was created
Created time.Time `json:"created"`
// LastUpdated is the timestamp of the most recent message or modification
LastUpdated time.Time `json:"last_updated"`
// Meta stores arbitrary session-related data
Meta map[string]any `json:"meta,omitempty"`
// Options contains the configuration for these chat sessions
Options Options `json:"-"`
}
Chat represents a chat session with message history
func (*Chat) AddAssistantContent ¶
AddAssistantContent adds an assistant message to the chat
func (*Chat) AddAssistantToolCall ¶
AddAssistantToolCall adds an assistant message with tool calls
func (*Chat) AddMessage ¶ added in v1.1.0
AddMessage adds a message to the chat
func (*Chat) AddMessageOnce ¶ added in v1.3.0
AddMessageOnce adds a message to the chat (idempotent)
func (*Chat) AddRoleContent ¶
AddRoleContent adds a role and content to the chat
func (*Chat) AddToolContent ¶
AddToolContent adds a tool content to the chat
func (*Chat) AddToolRawContent ¶
AddToolRawContent adds a raw content to the chat
func (*Chat) AddUserContent ¶
AddUserContent adds a user message to the chat
func (*Chat) ClearMessages ¶ added in v1.0.1
func (chat *Chat) ClearMessages()
ClearMessages removes all messages from the chat
func (*Chat) LastMessage ¶
LastMessage returns the last message in the chat
func (*Chat) LastMessageByRole ¶ added in v1.0.1
LastMessageByRole returns the last message in the chat by role
func (*Chat) LastMessageByType ¶ added in v1.0.1
LastMessageByType returns the last message in the chat with the given content type
func (*Chat) LastMessageRole ¶
LastMessageRole returns the role of the last message in the chat
func (*Chat) MessageCount ¶ added in v1.0.1
MessageCount returns the total number of messages in the chat
func (*Chat) MessageCountByRole ¶ added in v1.0.1
MessageCountByRole returns the number of messages with a specific role
func (*Chat) PopMessage ¶ added in v1.1.2
PopMessage removes and returns the last message from the chat
func (*Chat) PopMessageIfRole ¶ added in v1.1.2
PopMessageIfRole removes and returns the last message from the chat if it matches the role
func (*Chat) RangeByRole ¶ added in v1.0.1
RangeByRole iterates through messages with a specific role
func (*Chat) RangePendingToolCalls ¶
func (chat *Chat) RangePendingToolCalls(fn func(toolCallContext *ToolCallContext) error) error
RangePendingToolCalls iterates through messages to find and process tool calls that haven't received a response. It performs two passes: first to identify which tool calls have responses, then to process pending calls. The provided function is called for each pending tool call.
func (*Chat) RemoveLastMessage ¶ added in v1.0.1
RemoveLastMessage removes and returns the last message from the chat
func (*Chat) SetSystemContent ¶ added in v1.1.2
SetSystemContent sets or updates the system message at the beginning of the chat. If the first message is a system message, it updates its content. Otherwise, it inserts a new system message at the beginning.
func (*Chat) SetSystemMessage ¶ added in v1.1.2
SetSystemMessage sets the system message at the beginning of the chat
func (*Chat) ShiftMessages ¶ added in v1.1.2
ShiftMessages shifts all messages to the left by one index
func (*Chat) UnshiftMessages ¶ added in v1.1.2
UnshiftMessages unshifts all messages to the right by one index
type Function ¶
type Function struct {
Name string `yaml:"name" json:"name"`
Description string `yaml:"description,omitempty" json:"description,omitempty"`
// Arguments field is used by LLM to call tools
Arguments string `yaml:"arguments,omitempty" json:"arguments,omitempty"`
// Parameters field is provides available tool calling schema to LLM
Parameters Parameters `yaml:"parameters,omitempty" json:"parameters,omitempty"`
}
Function represents a function definition
func (*Function) ArgumentsMap ¶
ArgumentsMap parses the Arguments JSON string into a map[string]interface{}
type Message ¶
type Message struct {
Role string `json:"role"`
Content any `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Name string `json:"name,omitempty"` // For tool responses
ToolCallID string `json:"tool_call_id,omitempty"` // For tool responses
// contains filtered or unexported fields
}
Message represents a chat message in the session
func (*Message) ContentParts ¶
ContentParts returns the parts of a multipart message content (text and images). Returns nil for both parts and error if the content is not multipart. Returns error if the content cannot be properly marshaled/unmarshaled.
func (*Message) ContentString ¶
ContentString returns the content of the message as a string if the content is a simple string. Returns an empty string if the content is not a string type (e.g., multipart content).
type Meta ¶ added in v1.3.2
type Meta struct {
// contains filtered or unexported fields
}
Meta represents metadata for a message.
func (*Meta) Get ¶ added in v1.3.2
Get retrieves a metadata value for the message. Returns nil if the key does not exist.
func (*Meta) MarshalJSON ¶ added in v1.3.3
MarshalJSON implements the json.Marshaler interface.
type Options ¶
type Options struct {
S3 S3
}
Options contains configuration options for Chat sessions. S3 provides storage capabilities for persisting chat sessions.
type Parameters ¶ added in v1.1.3
type Parameters struct {
Type string `yaml:"type" json:"type"`
Properties map[string]Property `yaml:"properties" json:"properties"`
Required []string `yaml:"required" json:"required"`
}
Parameters defines the structure of function parameters
type Part ¶
type Part struct {
Type string `json:"type"`
Text string `json:"text,omitempty"`
ImageURL struct {
URL string `json:"url"`
Detail string `json:"detail,omitempty"`
} `json:"image_url,omitempty"`
}
Part represents a part of a message content that can be either text or image
type Property ¶ added in v1.1.3
type Property struct {
Type string `yaml:"type" json:"type"`
Description string `yaml:"description" json:"description"`
}
Property contains the individual parameter definitions
type S3 ¶
type S3 interface {
// Get retrieves data from storage
Get(ctx context.Context, key string) (io.ReadCloser, error)
// Put stores data
Put(ctx context.Context, key string, data io.Reader) error
// Delete deletes data from storage
Delete(ctx context.Context, key string) error
}
S3 represents a storage interface for sessions
type Storage ¶
type Storage struct {
Options Options
}
Storage represents a storage interface for chat sessions
type Tool ¶ added in v1.1.3
type Tool struct {
Type string `yaml:"type" json:"type"`
Function Function `yaml:"function" json:"function"`
}
Tool represents a single tool
type ToolCall ¶
type ToolCall struct {
ID string `json:"id,omitempty"`
Type string `json:"type"`
Function Function `json:"function"`
}
ToolCall represents a call to an external tool or function
type ToolCallContext ¶
ToolCallContext represents a tool call within a chat context, managing the lifecycle of a single tool invocation including its execution and response handling.
func (*ToolCallContext) Arguments ¶
func (tcc *ToolCallContext) Arguments() (map[string]any, error)
Arguments returns the arguments to the function as a map
func (*ToolCallContext) Name ¶
func (tcc *ToolCallContext) Name() string
Name returns the name of the function