memory

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2023 License: MIT Imports: 5 Imported by: 32

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 forths directly.

Index

Constants

This section is empty.

Variables

View Source
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 schema.ChatMessage) error

func (*ChatMessageHistory) AddUserMessage

func (h *ChatMessageHistory) AddUserMessage(_ context.Context, text string) error

AddUserMessage adds an user to the chat message history.

func (*ChatMessageHistory) Clear

func (*ChatMessageHistory) Messages

Messages returns all messages stored.

func (*ChatMessageHistory) SetMessages

func (h *ChatMessageHistory) SetMessages(_ context.Context, messages []schema.ChatMessage) error

type ChatMessageHistoryOption

type ChatMessageHistoryOption func(m *ChatMessageHistory)

ChatMessageHistoryOption is a function for creating new chat message history with other then the default values.

func WithPreviousMessages

func WithPreviousMessages(previousMessages []schema.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 forths 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 schema.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 a 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 then 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.LanguageModel
	MaxTokenLimit int
}

ConversationTokenBuffer for storing conversation memory.

func NewConversationTokenBuffer

func NewConversationTokenBuffer(
	llm llms.LanguageModel,
	maxTokenLimit int,
	options ...ConversationBufferOption,
) *ConversationTokenBuffer

NewConversationTokenBuffer is a function for crating a new token buffer memory.

func (*ConversationTokenBuffer) Clear

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 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.

func NewSimple

func NewSimple() Simple

func (Simple) Clear

func (m Simple) Clear(context.Context) error

func (Simple) GetMemoryKey

func (m Simple) GetMemoryKey(context.Context) string

func (Simple) LoadMemoryVariables

func (m Simple) LoadMemoryVariables(context.Context, map[string]any) (map[string]any, error)

func (Simple) MemoryVariables

func (m Simple) MemoryVariables(context.Context) []string

func (Simple) SaveContext

func (m Simple) SaveContext(context.Context, map[string]any, map[string]any) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL