Documentation ¶
Overview ¶
Package memory provides an interface for managing conversational data and a variety of implementations for storing and retrieving that data.
The main components of this package are: - ChatMessageHistory: a struct that stores chat messages. - ConversationBuffer: a simple form of memory that remembers previous conversational back and forth directly.
Index ¶
- Variables
- type ChatMessageHistory
- func (h *ChatMessageHistory) AddAIMessage(_ context.Context, text string) error
- func (h *ChatMessageHistory) AddMessage(_ context.Context, message llms.ChatMessage) error
- func (h *ChatMessageHistory) AddUserMessage(_ context.Context, text string) error
- func (h *ChatMessageHistory) Clear(_ context.Context) error
- func (h *ChatMessageHistory) Messages(_ context.Context) ([]llms.ChatMessage, error)
- func (h *ChatMessageHistory) SetMessages(_ context.Context, messages []llms.ChatMessage) error
- type ChatMessageHistoryOption
- type ConversationBuffer
- func (m *ConversationBuffer) Clear(ctx context.Context) error
- func (m *ConversationBuffer) GetMemoryKey(context.Context) string
- func (m *ConversationBuffer) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
- func (m *ConversationBuffer) MemoryVariables(context.Context) []string
- func (m *ConversationBuffer) SaveContext(ctx context.Context, inputValues map[string]any, outputValues map[string]any) error
- type ConversationBufferOption
- func WithAIPrefix(aiPrefix string) ConversationBufferOption
- func WithChatHistory(chatHistory schema.ChatMessageHistory) ConversationBufferOption
- func WithHumanPrefix(humanPrefix string) ConversationBufferOption
- func WithInputKey(inputKey string) ConversationBufferOption
- func WithMemoryKey(memoryKey string) ConversationBufferOption
- func WithOutputKey(outputKey string) ConversationBufferOption
- func WithReturnMessages(returnMessages bool) ConversationBufferOption
- type ConversationTokenBuffer
- func (tb *ConversationTokenBuffer) Clear(ctx context.Context) error
- func (tb *ConversationTokenBuffer) LoadMemoryVariables(ctx context.Context, inputs map[string]any) (map[string]any, error)
- func (tb *ConversationTokenBuffer) MemoryVariables(ctx context.Context) []string
- func (tb *ConversationTokenBuffer) SaveContext(ctx context.Context, inputValues map[string]any, outputValues map[string]any) error
- type ConversationWindowBuffer
- func (wb *ConversationWindowBuffer) Clear(ctx context.Context) error
- func (wb *ConversationWindowBuffer) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
- func (wb *ConversationWindowBuffer) MemoryVariables(ctx context.Context) []string
- func (wb *ConversationWindowBuffer) SaveContext(ctx context.Context, inputValues map[string]any, outputValues map[string]any) error
- type MongoDBChatMessageHistory
- func (h *MongoDBChatMessageHistory) AddAIMessage(ctx context.Context, text string) error
- func (h *MongoDBChatMessageHistory) AddMessage(ctx context.Context, message llms.ChatMessage) error
- func (h *MongoDBChatMessageHistory) AddUserMessage(ctx context.Context, text string) error
- func (h *MongoDBChatMessageHistory) Clear(ctx context.Context) error
- func (h *MongoDBChatMessageHistory) Messages(ctx context.Context) ([]llms.ChatMessage, error)
- func (h *MongoDBChatMessageHistory) SetMessages(ctx context.Context, messages []llms.ChatMessage) error
- type MongoDBChatMessageHistoryOption
- type Simple
- func (m Simple) Clear(context.Context) error
- func (m Simple) GetMemoryKey(context.Context) string
- func (m Simple) LoadMemoryVariables(context.Context, map[string]any) (map[string]any, error)
- func (m Simple) MemoryVariables(context.Context) []string
- func (m Simple) SaveContext(context.Context, map[string]any, map[string]any) error
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidInputValues = errors.New("invalid input values")
ErrInvalidInputValues is returned when input values given to a memory in save context are invalid.
Functions ¶
This section is empty.
Types ¶
type ChatMessageHistory ¶
type ChatMessageHistory struct {
// contains filtered or unexported fields
}
ChatMessageHistory is a struct that stores chat messages.
func NewChatMessageHistory ¶
func NewChatMessageHistory(options ...ChatMessageHistoryOption) *ChatMessageHistory
NewChatMessageHistory creates a new ChatMessageHistory using chat message options.
func (*ChatMessageHistory) AddAIMessage ¶
func (h *ChatMessageHistory) AddAIMessage(_ context.Context, text string) error
AddAIMessage adds an AIMessage to the chat message history.
func (*ChatMessageHistory) AddMessage ¶
func (h *ChatMessageHistory) AddMessage(_ context.Context, message llms.ChatMessage) error
func (*ChatMessageHistory) AddUserMessage ¶
func (h *ChatMessageHistory) AddUserMessage(_ context.Context, text string) error
AddUserMessage adds a user to the chat message history.
func (*ChatMessageHistory) Messages ¶
func (h *ChatMessageHistory) Messages(_ context.Context) ([]llms.ChatMessage, error)
Messages returns all messages stored.
func (*ChatMessageHistory) SetMessages ¶
func (h *ChatMessageHistory) SetMessages(_ context.Context, messages []llms.ChatMessage) error
type ChatMessageHistoryOption ¶
type ChatMessageHistoryOption func(m *ChatMessageHistory)
ChatMessageHistoryOption is a function for creating new chat message history with other than the default values.
func WithPreviousMessages ¶
func WithPreviousMessages(previousMessages []llms.ChatMessage) ChatMessageHistoryOption
WithPreviousMessages is an option for NewChatMessageHistory for adding previous messages to the history.
type ConversationBuffer ¶
type ConversationBuffer struct { ChatHistory schema.ChatMessageHistory ReturnMessages bool InputKey string OutputKey string HumanPrefix string AIPrefix string MemoryKey string }
ConversationBuffer is a simple form of memory that remembers previous conversational back and forth directly.
func NewConversationBuffer ¶
func NewConversationBuffer(options ...ConversationBufferOption) *ConversationBuffer
NewConversationBuffer is a function for crating a new buffer memory.
func (*ConversationBuffer) Clear ¶
func (m *ConversationBuffer) Clear(ctx context.Context) error
Clear sets the chat messages to a new and empty chat message history.
func (*ConversationBuffer) GetMemoryKey ¶
func (m *ConversationBuffer) GetMemoryKey(context.Context) string
func (*ConversationBuffer) LoadMemoryVariables ¶
func (m *ConversationBuffer) LoadMemoryVariables( ctx context.Context, _ map[string]any, ) (map[string]any, error)
LoadMemoryVariables returns the previous chat messages stored in memory. Previous chat messages are returned in a map with the key specified in the MemoryKey field. This key defaults to "history". If ReturnMessages is set to true the output is a slice of llms.ChatMessage. Otherwise, the output is a buffer string of the chat messages.
func (*ConversationBuffer) MemoryVariables ¶
func (m *ConversationBuffer) MemoryVariables(context.Context) []string
MemoryVariables gets the input key the buffer memory class will load dynamically.
func (*ConversationBuffer) SaveContext ¶
func (m *ConversationBuffer) SaveContext( ctx context.Context, inputValues map[string]any, outputValues map[string]any, ) error
SaveContext uses the input values to the llm to save a user message, and the output values of the llm to save an AI message. If the input or output key is not set, the input values or output values must contain only one key such that the function can know what string to add as a user and AI message. On the other hand, if the output key or input key is set, the input key must be a key in the input values and the output key must be a key in the output values. The values in the input and output values used to save a user and AI message must be strings.
type ConversationBufferOption ¶
type ConversationBufferOption func(b *ConversationBuffer)
ConversationBufferOption is a function for creating new buffer with other than the default values.
func WithAIPrefix ¶
func WithAIPrefix(aiPrefix string) ConversationBufferOption
WithAIPrefix is an option for specifying the AI prefix.
func WithChatHistory ¶
func WithChatHistory(chatHistory schema.ChatMessageHistory) ConversationBufferOption
WithChatHistory is an option for providing the chat history store.
func WithHumanPrefix ¶
func WithHumanPrefix(humanPrefix string) ConversationBufferOption
WithHumanPrefix is an option for specifying the human prefix.
func WithInputKey ¶
func WithInputKey(inputKey string) ConversationBufferOption
WithInputKey is an option for specifying the input key.
func WithMemoryKey ¶
func WithMemoryKey(memoryKey string) ConversationBufferOption
WithMemoryKey is an option for specifying the memory key.
func WithOutputKey ¶
func WithOutputKey(outputKey string) ConversationBufferOption
WithOutputKey is an option for specifying the output key.
func WithReturnMessages ¶
func WithReturnMessages(returnMessages bool) ConversationBufferOption
WithReturnMessages is an option for specifying should it return messages.
type ConversationTokenBuffer ¶
type ConversationTokenBuffer struct { ConversationBuffer LLM llms.Model MaxTokenLimit int }
ConversationTokenBuffer for storing conversation memory.
func NewConversationTokenBuffer ¶
func NewConversationTokenBuffer( llm llms.Model, maxTokenLimit int, options ...ConversationBufferOption, ) *ConversationTokenBuffer
NewConversationTokenBuffer is a function for crating a new token buffer memory.
func (*ConversationTokenBuffer) Clear ¶
func (tb *ConversationTokenBuffer) Clear(ctx context.Context) error
Clear uses ConversationBuffer method for clearing buffer memory.
func (*ConversationTokenBuffer) LoadMemoryVariables ¶
func (tb *ConversationTokenBuffer) LoadMemoryVariables( ctx context.Context, inputs map[string]any, ) (map[string]any, error)
LoadMemoryVariables uses ConversationBuffer method for loading memory variables.
func (*ConversationTokenBuffer) MemoryVariables ¶
func (tb *ConversationTokenBuffer) MemoryVariables(ctx context.Context) []string
MemoryVariables uses ConversationBuffer method for memory variables.
func (*ConversationTokenBuffer) SaveContext ¶
func (tb *ConversationTokenBuffer) SaveContext( ctx context.Context, inputValues map[string]any, outputValues map[string]any, ) error
SaveContext uses ConversationBuffer method for saving context and prunes memory buffer if needed.
type ConversationWindowBuffer ¶ added in v0.1.3
type ConversationWindowBuffer struct { ConversationBuffer ConversationWindowSize int }
ConversationWindowBuffer for storing conversation memory.
func NewConversationWindowBuffer ¶ added in v0.1.3
func NewConversationWindowBuffer( conversationWindowSize int, options ...ConversationBufferOption, ) *ConversationWindowBuffer
NewConversationWindowBuffer is a function for crating a new window buffer memory.
func (*ConversationWindowBuffer) Clear ¶ added in v0.1.3
func (wb *ConversationWindowBuffer) Clear(ctx context.Context) error
Clear uses ConversationBuffer method for clearing buffer memory.
func (*ConversationWindowBuffer) LoadMemoryVariables ¶ added in v0.1.3
func (wb *ConversationWindowBuffer) LoadMemoryVariables(ctx context.Context, _ map[string]any) (map[string]any, error)
LoadMemoryVariables uses ConversationBuffer method for loading memory variables.
func (*ConversationWindowBuffer) MemoryVariables ¶ added in v0.1.3
func (wb *ConversationWindowBuffer) MemoryVariables(ctx context.Context) []string
MemoryVariables uses ConversationBuffer method for memory variables.
func (*ConversationWindowBuffer) SaveContext ¶ added in v0.1.3
func (wb *ConversationWindowBuffer) SaveContext( ctx context.Context, inputValues map[string]any, outputValues map[string]any, ) error
SaveContext uses ConversationBuffer method for saving context and prunes memory buffer if needed.
type MongoDBChatMessageHistory ¶ added in v0.1.10
type MongoDBChatMessageHistory struct {
// contains filtered or unexported fields
}
func NewMongoDBChatMessageHistory ¶ added in v0.1.10
func NewMongoDBChatMessageHistory(ctx context.Context, options ...MongoDBChatMessageHistoryOption) (*MongoDBChatMessageHistory, error)
NewMongoDBChatMessageHistory creates a new MongoDBChatMessageHistory using chat message options.
func (*MongoDBChatMessageHistory) AddAIMessage ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) AddAIMessage(ctx context.Context, text string) error
AddAIMessage adds an AIMessage to the chat message history.
func (*MongoDBChatMessageHistory) AddMessage ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) AddMessage(ctx context.Context, message llms.ChatMessage) error
AddMessage adds a message to the store.
func (*MongoDBChatMessageHistory) AddUserMessage ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) AddUserMessage(ctx context.Context, text string) error
AddUserMessage adds a user to the chat message history.
func (*MongoDBChatMessageHistory) Clear ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) Clear(ctx context.Context) error
Clear clear session memory from MongoDB.
func (*MongoDBChatMessageHistory) Messages ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) Messages(ctx context.Context) ([]llms.ChatMessage, error)
Messages returns all messages stored.
func (*MongoDBChatMessageHistory) SetMessages ¶ added in v0.1.10
func (h *MongoDBChatMessageHistory) SetMessages(ctx context.Context, messages []llms.ChatMessage) error
SetMessages replaces existing messages in the store.
type MongoDBChatMessageHistoryOption ¶ added in v0.1.10
type MongoDBChatMessageHistoryOption func(m *MongoDBChatMessageHistory)
func WithCollectionName ¶ added in v0.1.10
func WithCollectionName(name string) MongoDBChatMessageHistoryOption
WithCollectionName is an option for specifying the collection name.
func WithConnectionURL ¶ added in v0.1.10
func WithConnectionURL(connectionURL string) MongoDBChatMessageHistoryOption
WithConnectionURL is an option for specifying the MongoDB connection URL. Must be set.
func WithDataBaseName ¶ added in v0.1.10
func WithDataBaseName(name string) MongoDBChatMessageHistoryOption
WithDataBaseName is an option for specifying the database name.
func WithSessionID ¶ added in v0.1.10
func WithSessionID(sessionID string) MongoDBChatMessageHistoryOption
WithSessionID is an arbitrary key that is used to store the messages of a single chat session, like user name, email, chat id etc. Must be set.
type Simple ¶
type Simple struct{}
Simple is a class that implement the memory interface, but does nothing. The class is used as default in multiple chains.