Documentation
¶
Index ¶
- Variables
- func VisitMessages(ctx context.Context, message *Message, mset MessageSet, ...) error
- type Chat
- type Message
- type MessageSet
- type Messages
- func (msgs Messages) GetByID(id string) *Message
- func (msgs Messages) Hydrate(ctx context.Context, graph *Chat)
- func (msgs Messages) Hydrated() bool
- func (msgs Messages) IDs() []string
- func (msgs Messages) Match(prFn func(*Message) bool) Messages
- func (msgs Messages) OpenAIChatMessages() []openai.ChatMessage
- func (msgs Messages) Search(ctx context.Context, query string) []*SearchResult
- func (msgs Messages) Summarize(ctx context.Context, client *openai.Client, model string) (string, error)
- func (msgs Messages) SummarizeWithSystemPrompt(ctx context.Context, client *openai.Client, model string, ...) (string, error)
- func (msgs Messages) Visit(ctx context.Context, fn func(*Message) error) error
- type SearchResult
Constants ¶
This section is empty.
Variables ¶
var DefaultSummaryPrompt = strings.Join( []string{ "You are an expert at summarization that answers as concisely as possible.", "Provide a summary of the given conversation, including all the key information (e.g. people, places, events, things, etc) to continue on the conversation.", "Do not include any unnecessary information, or a prefix in the output.", }, " ", )
DefaultSummaryPrompt is the default prompt used to summarize messages for the Summarize method.
Functions ¶
func VisitMessages ¶
func VisitMessages(ctx context.Context, message *Message, mset MessageSet, fn func(*Message) error) error
VisitMessages visits messages in a depth-first-search manner and calls the given function for each message. This function is useful as a foundation for other graph traversal algorithms.
Types ¶
type Chat ¶
Chat is a "chat graph" that contains a connected set of messages.
func (*Chat) GetMessageByID ¶
GetMessageByID returns a message by ID (first match) for the graph.
func (*Chat) GetMessages ¶
GetMessages returns a collection of messages by ID for the graph.
func (*Chat) HydrateMessages ¶
HydrateMessages fully hydrates the messages by adding the "in" and "out" messages to the message collections instead of just the message IDs.
This only need to be called when loaded from a serialized graph, since nested message collections are not fully serialized, only the message IDs.
type Message ¶
type Message struct { // ID is the unique identifier for the message. ID string `json:"id,omitempty"` // ChatMessage is the underlying OpenAI chat message, embedded // for some convenience to access the underlying fields (e.g. Role, Content). openai.ChatMessage // In is a collection of messages that are going "in" (←) to this message, // (e.g. referencing this message). // // Example, if this message is a response to another message, the // other message could be in the "in" collection. In Messages `json:"in,omitempty"` // Out is a collection of messages that are going "out" (→) from this message, // (e.g. referenced by this message). // // Example, if this message is a question, the response message could // be in the "out" collection. Out Messages `json:"out,omitempty"` }
Message is a single chat message that is connected to other messages.
This essentially a small wrapper around openai.ChatMessage to include additional functionality for graph traversal, storage, searching, etc.
In and Out ¶
What it means for other messages to be "in" or "out" is a bit arbitrary, and can be used for different purposes that are specific to your application.
For example, in a chat graph, "in" messages are messages that are referenced by this message, and "out" messages are messages that reference this message. But, in a different application, "in" messages could be messages that are "before" this message, and "out" messages could be messages that are "after" this message. It all depends on the application's requirements.
func (*Message) AddInOut ¶
AddInOut adds a message to the "in" messages, and adds this message to the "out" messages of the other message to create an easily traversalable bi-directional graph that is more strongly connected.
func (*Message) AddOutIn ¶
AddOutIn adds a message to the "out" messages, and adds this message to the "in" messages of the other message to create an easily traversalable bi-directional graph that is more strongly connected.
func (*Message) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for Message, which is like the normal json.Marshal, but only includes message IDs for the "in" and "out" collections, to reduce the size of the JSON.
func (*Message) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for Message, partially unmarshalling the "in" and "out" messages, and leaving the rest to the caller to do, if needed.
This can be done at the message set or the graph level.
type MessageSet ¶
type MessageSet map[*Message]struct{}
MessageSet is a collection of messages, used to track seen messages when traversing a graph to avoid infinite loops.
func NewMessageSet ¶
func NewMessageSet() MessageSet
NewMessageSet returns a new seen messages collection.
func (MessageSet) Add ¶
func (s MessageSet) Add(message *Message)
Add adds a message to the seen messages.
func (MessageSet) GetOrPut ¶
func (s MessageSet) GetOrPut(message *Message) *Message
GetOrPut returns the message if it has been seen, or adds it to the seen messages and returns it. This is useful for adding a message to the seen messages collection and returning it to be used in a graph traversal algorithm. It is a convenience function that combines the Add and Has functions into one.
func (MessageSet) Has ¶
func (s MessageSet) Has(message *Message) bool
Has returns true if the message has been seen.
type Messages ¶
type Messages []*Message
Messages is a collection of messages.
func (Messages) Hydrate ¶
Hydrate fully hydrates the messages by adding the "in" and "out" messages to the message collections instead of just the message IDs.
func (Messages) OpenAIChatMessages ¶
func (msgs Messages) OpenAIChatMessages() []openai.ChatMessage
OpenAIChatMessages returns a slice of OpenAI chat messages.
func (Messages) Search ¶
func (msgs Messages) Search(ctx context.Context, query string) []*SearchResult
Search searches the messages for matches to a given query.
func (Messages) Summarize ¶
func (msgs Messages) Summarize(ctx context.Context, client *openai.Client, model string) (string, error)
Summarize summarizes the messages using the OpenAI API.
type SearchResult ¶
type SearchResult struct { // The message that matched the search query. Message *Message `json:"message"` // MessageIndex is the index of the message in the chat history. MessageIndex int `json:"message_index"` // MatchStart is the index of the start of the match in the message. StartIndex int `json:"start_index"` // MatchEnd is the index of the end of the match in the message. EndIndex int `json:"end_index"` }
SearchResults is a collection of search results.