Documentation
¶
Overview ¶
Package memory provides memory implementations for maintaining conversation state across interactions.
Index ¶
- type ChatMessageHistory
- func (h *ChatMessageHistory) AddAIMessage(_ context.Context, content string)
- func (h *ChatMessageHistory) AddMessage(_ context.Context, msg core.Message)
- func (h *ChatMessageHistory) AddUserMessage(_ context.Context, content string)
- func (h *ChatMessageHistory) Clear(_ context.Context)
- func (h *ChatMessageHistory) GetMessages(_ context.Context) []core.Message
- func (h *ChatMessageHistory) SetMessages(_ context.Context, msgs []core.Message)
- type ConversationBufferMemory
- func (m *ConversationBufferMemory) Clear(ctx context.Context) error
- func (m *ConversationBufferMemory) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
- func (m *ConversationBufferMemory) MemoryVariables() []string
- func (m *ConversationBufferMemory) SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
- type ConversationWindowMemory
- func (m *ConversationWindowMemory) Clear(ctx context.Context) error
- func (m *ConversationWindowMemory) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
- func (m *ConversationWindowMemory) MemoryVariables() []string
- func (m *ConversationWindowMemory) SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
- type Memory
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChatMessageHistory ¶
type ChatMessageHistory struct {
// contains filtered or unexported fields
}
ChatMessageHistory stores chat messages in memory. It is the backing store used by conversation memory implementations.
func NewChatMessageHistory ¶
func NewChatMessageHistory() *ChatMessageHistory
NewChatMessageHistory creates a new in-memory chat message history.
func (*ChatMessageHistory) AddAIMessage ¶
func (h *ChatMessageHistory) AddAIMessage(_ context.Context, content string)
AddAIMessage adds an AI message.
func (*ChatMessageHistory) AddMessage ¶
func (h *ChatMessageHistory) AddMessage(_ context.Context, msg core.Message)
AddMessage adds a message to the history.
func (*ChatMessageHistory) AddUserMessage ¶
func (h *ChatMessageHistory) AddUserMessage(_ context.Context, content string)
AddUserMessage adds a human message.
func (*ChatMessageHistory) Clear ¶
func (h *ChatMessageHistory) Clear(_ context.Context)
Clear removes all messages from the history.
func (*ChatMessageHistory) GetMessages ¶
func (h *ChatMessageHistory) GetMessages(_ context.Context) []core.Message
GetMessages returns all messages in the history.
func (*ChatMessageHistory) SetMessages ¶
func (h *ChatMessageHistory) SetMessages(_ context.Context, msgs []core.Message)
SetMessages replaces the entire message history.
type ConversationBufferMemory ¶
type ConversationBufferMemory struct {
// ChatHistory is the backing message store.
ChatHistory *ChatMessageHistory
// MemoryKey is the key used to store/retrieve messages. Default: "history".
MemoryKey string
// InputKey is the key for the human input. Default: "input".
InputKey string
// OutputKey is the key for the AI output. Default: "output".
OutputKey string
// ReturnMessages controls whether to return messages or a formatted string.
ReturnMessages bool
// HumanPrefix is the prefix for human messages in string output.
HumanPrefix string
// AIPrefix is the prefix for AI messages in string output.
AIPrefix string
// MaxMessages is the maximum number of messages to retain in history.
// 0 means unlimited. When exceeded, the oldest messages are dropped.
MaxMessages int
}
ConversationBufferMemory stores the entire conversation history. It implements the Memory interface.
func NewConversationBufferMemory ¶
func NewConversationBufferMemory() *ConversationBufferMemory
NewConversationBufferMemory creates a new ConversationBufferMemory.
func (*ConversationBufferMemory) Clear ¶
func (m *ConversationBufferMemory) Clear(ctx context.Context) error
Clear resets the conversation history.
func (*ConversationBufferMemory) LoadMemoryVariables ¶
func (m *ConversationBufferMemory) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
LoadMemoryVariables loads the conversation history.
func (*ConversationBufferMemory) MemoryVariables ¶
func (m *ConversationBufferMemory) MemoryVariables() []string
MemoryVariables returns the keys this memory produces.
func (*ConversationBufferMemory) SaveContext ¶
func (m *ConversationBufferMemory) SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
SaveContext saves the input and output messages.
type ConversationWindowMemory ¶
type ConversationWindowMemory struct {
// ChatHistory is the backing message store.
ChatHistory *ChatMessageHistory
// K is the number of recent conversation turns (pairs of messages) to keep.
K int
// MemoryKey is the key used to store/retrieve messages. Default: "history".
MemoryKey string
// InputKey is the key for the human input.
InputKey string
// OutputKey is the key for the AI output.
OutputKey string
// ReturnMessages controls whether to return messages or a formatted string.
ReturnMessages bool
// HumanPrefix is the prefix for human messages in string output.
HumanPrefix string
// AIPrefix is the prefix for AI messages in string output.
AIPrefix string
}
ConversationWindowMemory stores a sliding window of the most recent K conversation turns. It implements the Memory interface.
func NewConversationWindowMemory ¶
func NewConversationWindowMemory(k int) *ConversationWindowMemory
NewConversationWindowMemory creates a new ConversationWindowMemory with K turns.
func (*ConversationWindowMemory) Clear ¶
func (m *ConversationWindowMemory) Clear(ctx context.Context) error
Clear resets the conversation history.
func (*ConversationWindowMemory) LoadMemoryVariables ¶
func (m *ConversationWindowMemory) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
LoadMemoryVariables loads the last K turns of conversation.
func (*ConversationWindowMemory) MemoryVariables ¶
func (m *ConversationWindowMemory) MemoryVariables() []string
MemoryVariables returns the keys this memory produces.
func (*ConversationWindowMemory) SaveContext ¶
func (m *ConversationWindowMemory) SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
SaveContext saves the input and output messages.
type Memory ¶
type Memory interface {
// MemoryVariables returns the keys this memory will add to chain inputs.
MemoryVariables() []string
// LoadMemoryVariables returns key-value pairs to be added to chain inputs.
LoadMemoryVariables(ctx context.Context, inputs map[string]any) (map[string]any, error)
// SaveContext saves the input and output of a chain run to memory.
SaveContext(ctx context.Context, inputs map[string]any, outputs map[string]any) error
// Clear resets the memory.
Clear(ctx context.Context) error
}
Memory is the interface for conversation memory. Memory loads relevant context before a chain runs and saves context after.