graph

package
v0.0.0-...-029c3da Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MPL-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

type Chat struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Messages `json:"messages"`
}

Chat is a "chat graph" that contains a connected set of messages.

func (*Chat) GetMessageByID

func (graph *Chat) GetMessageByID(id string) *Message

GetMessageByID returns a message by ID (first match) for the graph.

func (*Chat) GetMessages

func (graph *Chat) GetMessages(ids ...string) Messages

GetMessages returns a collection of messages by ID for the graph.

func (*Chat) HydrateMessages

func (graph *Chat) HydrateMessages(ctx context.Context)

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.

func (*Chat) Visit

func (c *Chat) Visit(ctx context.Context, fn func(*Message) error) error

Visit visits the chat graph 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.

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) AddIn

func (m *Message) AddIn(msg *Message)

AddIn adds a message to the "in" messages.

func (*Message) AddInOut

func (m *Message) AddInOut(msg *Message)

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) AddOut

func (m *Message) AddOut(msg *Message)

AddOut adds a message to the "out" messages.

func (*Message) AddOutIn

func (m *Message) AddOutIn(msg *Message)

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

func (m *Message) MarshalJSON() ([]byte, error)

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) String

func (m *Message) String() string

String returns a string representation of the message.

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(b []byte) error

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) GetByID

func (msgs Messages) GetByID(id string) *Message

GetByID returns a message by ID (first match).

func (Messages) Hydrate

func (msgs Messages) Hydrate(ctx context.Context, graph *Chat)

Hydrate fully hydrates the messages by adding the "in" and "out" messages to the message collections instead of just the message IDs.

func (Messages) Hydrated

func (msgs Messages) Hydrated() bool

Hydrated returns true if the messages are fully hydrated.

func (Messages) IDs

func (msgs Messages) IDs() []string

IDs returns a slice of message IDs.

func (Messages) Match

func (msgs Messages) Match(prFn func(*Message) bool) Messages

Match returns a slice of messages that match the given predicate function.

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.

func (Messages) SummarizeWithSystemPrompt

func (msgs Messages) SummarizeWithSystemPrompt(ctx context.Context, client *openai.Client, model string, summarySystemPrompt string) (string, error)

Summarize summarizes the messages using the OpenAI API.

func (Messages) Visit

func (msgs Messages) Visit(ctx context.Context, fn func(*Message) error) error

Visit visits the 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.

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.

Jump to

Keyboard shortcuts

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