models

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

func NewNotFoundError added in v0.9.0

func NewNotFoundError(resource string) error

Types

type AppState

type AppState struct {
	LLMClient     ZepLLM
	MemoryStore   MemoryStore[any]
	DocumentStore DocumentStore[any]
	Config        *config.Config
}

AppState is a struct that holds the state of the application Use cmd.NewAppState to create a new instance

type CreateDocumentCollectionRequest added in v0.9.0

type CreateDocumentCollectionRequest struct {
	Name                string                 `json:"name"                 validate:"required,alphanum,min=3,max=40"`
	Description         string                 `json:"description"          validate:"omitempty,max=1000"`
	Metadata            map[string]interface{} `json:"metadata,omitempty"`
	EmbeddingDimensions int                    `json:"embedding_dimensions" validate:"required,numeric,min=8,max=2000"`
	// these needs to be pointers so that we can distinguish between false and unset when validating
	IsAutoEmbedded *bool `json:"is_auto_embedded"     validate:"required,boolean"`
}

type CreateDocumentRequest added in v0.9.0

type CreateDocumentRequest struct {
	DocumentID string                 `json:"document_id,omitempty" validate:"omitempty,printascii,max=100"`
	Content    string                 `json:"content,omitempty"     validate:"required_without=Embedding,omitempty"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
	Embedding  []float32              `json:"embedding,omitempty"   validate:"required_without=Content,omitempty"`
}

type DocEmbeddingTask added in v0.9.0

type DocEmbeddingTask struct {
	UUID           uuid.UUID `json:"uuid"`
	CollectionName string    `json:"collection_name"`
	Content        string    `json:"content"`
}

type DocEmbeddingUpdate added in v0.9.0

type DocEmbeddingUpdate struct {
	UUID           uuid.UUID `json:"uuid"`
	CollectionName string    `json:"collection_name"`
	ProcessedAt    time.Time `json:"time"`
	Embedding      []float32 `json:"embedding,omitempty" bun:"type:vector,nullzero"`
}

type Document added in v0.9.0

type Document struct {
	DocumentBase
	Embedding []float32 `bun:"type:vector,nullzero" json:"embedding,omitempty"`
}

type DocumentBase added in v0.9.0

type DocumentBase struct {
	UUID       uuid.UUID              `bun:",pk,type:uuid,default:gen_random_uuid()"`
	CreatedAt  time.Time              `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"`
	UpdatedAt  time.Time              `bun:"type:timestamptz,nullzero,default:current_timestamp"`
	DeletedAt  time.Time              `bun:"type:timestamptz,soft_delete,nullzero"`
	DocumentID string                 `bun:",unique,nullzero"`
	Content    string                 `bun:",nullzero"`
	Metadata   map[string]interface{} `bun:"type:jsonb,nullzero,json_use_number"`
	IsEmbedded bool                   `bun:",nullzero"`
}

type DocumentCollection added in v0.6.5

type DocumentCollection struct {
	UUID                uuid.UUID              `bun:",pk,type:uuid,default:gen_random_uuid()"`
	CreatedAt           time.Time              `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"`
	UpdatedAt           time.Time              `bun:"type:timestamptz,nullzero,default:current_timestamp"`
	Name                string                 `bun:",notnull,unique"`
	Description         string                 `bun:",notnull"`
	Metadata            map[string]interface{} `bun:"type:jsonb,nullzero,json_use_number"`
	TableName           string                 `bun:",notnull"`
	EmbeddingModelName  string                 `bun:",notnull"`
	EmbeddingDimensions int                    `bun:",notnull"`
	IsAutoEmbedded      bool                   `bun:",notnull"` // Is the collection automatically embedded by Zep?
	DistanceFunction    string                 `bun:",notnull"` // Distance function to use for index
	IsNormalized        bool                   `bun:",notnull"` // Are the embeddings normalized?
	IsIndexed           bool                   `bun:",notnull"` // Has an index been created on the collection table?
	ListCount           int                    `bun:",notnull"` // Number of lists in the collection index
	ProbeCount          int                    `bun:",notnull"` // Number of probes to use when searching the index
	*DocumentCollectionCounts
}

type DocumentCollectionCounts added in v0.9.0

type DocumentCollectionCounts struct {
	DocumentCount         int `bun:"document_count"          json:"document_count"`          // Number of documents in the collection
	DocumentEmbeddedCount int `bun:"document_embedded_count" json:"document_embedded_count"` // Number of documents with embeddings
}

type DocumentCollectionResponse added in v0.9.0

type DocumentCollectionResponse struct {
	UUID                uuid.UUID              `json:"uuid"`
	CreatedAt           time.Time              `json:"created_at"`
	UpdatedAt           time.Time              `json:"updated_at"`
	Name                string                 `json:"name"`
	Description         string                 `json:"description"`
	Metadata            map[string]interface{} `json:"metadata,omitempty"`
	EmbeddingModelName  string                 `json:"embedding_model_name,omitempty"`
	EmbeddingDimensions int                    `json:"embedding_dimensions"`
	IsAutoEmbedded      bool                   `json:"is_auto_embedded"`
	IsNormalized        bool                   `json:"is_normalized"`
	IsIndexed           bool                   `json:"is_indexed"`
	*DocumentCollectionCounts
}

type DocumentResponse added in v0.9.0

type DocumentResponse struct {
	UUID       uuid.UUID              `json:"uuid"`
	CreatedAt  time.Time              `json:"created_at"`
	UpdatedAt  time.Time              `json:"updated_at"`
	DocumentID string                 `json:"document_id"`
	Content    string                 `json:"content"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
	Embedding  []float32              `json:"embedding"`
	IsEmbedded bool                   `json:"is_embedded"`
}

type DocumentSearchPayload added in v0.9.0

type DocumentSearchPayload struct {
	CollectionName string                 `json:"collection_name"`
	Text           string                 `json:"text,omitempty"`
	Embedding      []float32              `json:"embedding,omitempty"`
	Metadata       map[string]interface{} `json:"metadata,omitempty"`
}

type DocumentSearchResult added in v0.9.0

type DocumentSearchResult struct {
	*DocumentResponse
	Score float64 `json:"score"`
}

type DocumentSearchResultPage added in v0.9.0

type DocumentSearchResultPage struct {
	Results     []DocumentSearchResult `json:"results"`
	QueryVector []float32              `json:"query_vector"`
	ResultCount int                    `json:"result_count"`
	TotalPages  int                    `json:"total_pages"`
	CurrentPage int                    `json:"current_page"`
}

type DocumentStore added in v0.9.0

type DocumentStore[T any] interface {
	// CreateCollection creates a new DocumentCollection.
	// If a collection with the same name already exists, it will be overwritten.
	CreateCollection(
		ctx context.Context,
		collection DocumentCollection,
	) error
	UpdateCollection(
		ctx context.Context,
		collection DocumentCollection,
	) error
	// GetCollection retrieves a DocumentCollection by name.
	GetCollection(
		ctx context.Context,
		collectionName string,
	) (DocumentCollection, error)
	// GetCollectionList retrieves the list of DocumentCollection.
	GetCollectionList(
		ctx context.Context,
	) ([]DocumentCollection, error)
	// DeleteCollection deletes a DocumentCollection by name.
	DeleteCollection(
		ctx context.Context,
		collectionName string,
	) error
	// CreateDocuments creates a batch of Documents.
	CreateDocuments(
		ctx context.Context,
		collectionName string,
		documents []Document,
	) ([]uuid.UUID, error)
	// UpdateDocuments updates a batch of Documents.
	// The provided Document UUIDs must match existing documents.
	UpdateDocuments(
		ctx context.Context,
		collectionName string,
		documents []Document,
	) error
	// GetDocuments retrieves a Document by UUID.
	GetDocuments(
		ctx context.Context,
		collectionName string,
		uuids []uuid.UUID,
		DocumentID []string,
	) ([]Document, error)
	// DeleteDocuments deletes a Document by UUID.
	DeleteDocuments(
		ctx context.Context,
		collectionName string,
		documentUUIDs []uuid.UUID,
	) error
	// SearchCollection retrieves a collection of DocumentSearchResultPage based on the provided search query.
	// It accepts an optional limit for the total number of results, as well as parameters for pagination: pageNumber and pageSize.
	// Parameters:
	// - limit: Defines the maximum number of results returned. If it's 0, all the results will be returned.
	// - pageNumber: Specifies the current page number in the pagination scheme.
	// - pageSize: Determines the number of results per page. If it's -1, all results are returned on a single page.
	// The mmr parameter is used to enable/disable the MMR algorithm for search results.
	// The function will return the results in pages as determined by pageSize.
	SearchCollection(
		ctx context.Context,
		query *DocumentSearchPayload,
		limit int,
		withMMR bool,
		pageNumber int,
		pageSize int,
	) (*DocumentSearchResultPage, error)
	// CreateCollectionIndex creates an index on the collection. Manually calling this function will drop and
	// recreate the index, if it exists.
	// force: If true, the index will be created even if there are too few documents in the collection.
	CreateCollectionIndex(ctx context.Context, collectionName string, force bool) error
	// OnStart is called when the application starts. This is a good place to initialize any resources or configs that
	// are required by the MemoryStore implementation.
	OnStart(ctx context.Context) error
	// Shutdown is called when the application is shutting down. This is a good place to clean up any resources or configs
	Shutdown(ctx context.Context) error
	// GetClient returns the underlying storage client
	GetClient() any
}

DocumentStore interface

type EmbeddingModel added in v0.6.5

type EmbeddingModel struct {
	Service      string `json:"service"`
	Dimensions   int    `json:"dimensions"`
	IsNormalized bool   `json:"normalized"`
}

type Entity added in v0.5.0

type Entity struct {
	Name    string        `json:"name"`
	Label   string        `json:"label"`
	Matches []EntityMatch `json:"matches"`
}

type EntityMatch added in v0.5.0

type EntityMatch struct {
	Start int    `json:"start"`
	End   int    `json:"end"`
	Text  string `json:"text"`
}

type EntityRequest added in v0.5.0

type EntityRequest struct {
	Texts []EntityRequestRecord `json:"texts"`
}

type EntityRequestRecord added in v0.5.0

type EntityRequestRecord struct {
	UUID     string `json:"uuid"`
	Text     string `json:"text"`
	Language string `json:"language"`
}

type EntityResponse added in v0.5.0

type EntityResponse struct {
	Texts []EntityResponseRecord `json:"texts"`
}

type EntityResponseRecord added in v0.5.0

type EntityResponseRecord struct {
	UUID     string   `json:"uuid"`
	Entities []Entity `json:"entities"`
}

type Extractor

type Extractor interface {
	Extract(
		ctx context.Context,
		appState *AppState,
		messageEvents *MessageEvent,
	) error
	Notify(ctx context.Context, appState *AppState, messageEvents *MessageEvent) error
}

Extractor is an interface that defines the methods that must be implemented by an Extractor

type GetDocumentListRequest added in v0.9.0

type GetDocumentListRequest struct {
	UUIDs       []uuid.UUID `json:"uuids"        validate:"required_without=DocumentIDs"`
	DocumentIDs []string    `json:"document_ids" validate:"required_without=UUIDs"`
}

type GetDocumentRequest added in v0.9.0

type GetDocumentRequest struct {
	UUID       uuid.UUID `json:"uuid"        validate:"required_without=DocumentID,uuid,omitempty"`
	DocumentID string    `json:"document_id" validate:"required_without=UUID,alphanum,max=40,omitempty"`
}

type Intent added in v0.7.0

type Intent struct {
	UUID      uuid.UUID `json:"uuid,omitempty"`
	Name      string    `json:"name,omitempty"`
	Documents []string  `json:"documents"`
}

type IntentCollection added in v0.7.0

type IntentCollection struct {
	UUID    uuid.UUID `json:"uuid,omitempty"`
	Name    string    `json:"name,omitempty"`
	Intents []Intent  `json:"intents"`
}

type IntentPromptTemplateData added in v0.7.0

type IntentPromptTemplateData struct {
	Input string
}

type IntentResponse added in v0.7.0

type IntentResponse struct {
	Intent string `json:"intent"`
}

type Memory

type Memory struct {
	Messages []Message              `json:"messages"`
	Summary  *Summary               `json:"summary,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type MemorySearchPayload added in v0.6.0

type MemorySearchPayload struct {
	Text     string                 `json:"text"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type MemorySearchResult added in v0.6.0

type MemorySearchResult struct {
	Message  *Message               `json:"message"`
	Summary  *Summary               `json:"summary"` // reserved for future use
	Metadata map[string]interface{} `json:"metadata,omitempty"`
	Dist     float64                `json:"dist"`
}

type MemoryStore

type MemoryStore[T any] interface {
	// GetMemory returns the most recent Summary and a list of messages for a given sessionID.
	// GetMemory returns:
	//   - the most recent Summary, if one exists
	//   - the lastNMessages messages, if lastNMessages > 0
	//   - all messages since the last SummaryPoint, if lastNMessages == 0
	//   - if no Summary (and no SummaryPoint) exists and lastNMessages == 0, returns
	//     all undeleted messages
	GetMemory(ctx context.Context,
		appState *AppState,
		sessionID string,
		lastNMessages int) (*Memory, error)
	// GetSummary retrieves the most recent Summary for a given sessionID. The Summary return includes the UUID of the
	// SummaryPoint, which the most recent Message in the collection of messages that was used to generate the Summary.
	GetSummary(ctx context.Context,
		appState *AppState,
		sessionID string) (*Summary, error)
	// PutMemory stores a Memory for a given sessionID. If the SessionID doesn't exist, a new one is created.
	PutMemory(ctx context.Context,
		appState *AppState,
		sessionID string,
		memoryMessages *Memory,
		skipNotify bool) error // skipNotify is used to prevent loops when calling NotifyExtractors.
	// PutSummary stores a new Summary for a given sessionID.
	PutSummary(ctx context.Context,
		appState *AppState,
		sessionID string,
		summary *Summary) error
	// PutMessageMetadata creates, updates, or deletes metadata for a given message, and does not
	// update the message itself.
	// isPrivileged indicates whether the caller is privileged to add or update system metadata.
	PutMessageMetadata(ctx context.Context,
		appState *AppState,
		sessionID string,
		messages []Message,
		isPrivileged bool) error
	// PutMessageVectors stores a collection of MessageEmbedding for a given sessionID.
	PutMessageVectors(ctx context.Context,
		appState *AppState,
		sessionID string,
		embeddings []MessageEmbedding) error
	// GetMessageVectors retrieves a collection of MessageEmbedding for a given sessionID.
	GetMessageVectors(ctx context.Context,
		appState *AppState,
		sessionID string) ([]MessageEmbedding, error)
	// SearchMemory retrieves a collection of SearchResults for a given sessionID and query. Currently, The
	// MemorySearchResult structure can include both Messages and Summaries. Currently, we only search Messages.
	SearchMemory(
		ctx context.Context,
		appState *AppState,
		sessionID string,
		query *MemorySearchPayload,
		limit int) ([]MemorySearchResult, error)
	// DeleteSession deletes all records for a given sessionID. This is a soft delete. Hard deletes will be handled
	// by a separate process or left to the implementation.
	DeleteSession(ctx context.Context, sessionID string) error
	// GetSession retrieves a Session for a given sessionID.
	GetSession(
		ctx context.Context,
		appState *AppState,
		sessionID string,
	) (*Session, error)
	// PutSession creates or updates a Session for a given sessionID.
	PutSession(
		ctx context.Context,
		appState *AppState,
		session *Session,
	) error
	OnStart(ctx context.Context, appState *AppState) error
	// Attach is used by Extractors to register themselves with the MemoryStore. This allows the MemoryStore to notify
	// the Extractors when new occur.
	Attach(observer Extractor)
	// NotifyExtractors notifies all registered Extractors of a new MessageEvent.
	NotifyExtractors(
		ctx context.Context,
		appState *AppState,
		eventData *MessageEvent,
	)
	// PurgeDeleted hard deletes all deleted data in the MemoryStore.
	PurgeDeleted(ctx context.Context) error
	// Close is called when the application is shutting down. This is a good place to clean up any resources used by
	// the MemoryStore implementation.
	Close() error
}

MemoryStore interface

type Message

type Message struct {
	UUID       uuid.UUID              `json:"uuid"`
	CreatedAt  time.Time              `json:"created_at"`
	Role       string                 `json:"role"`
	Content    string                 `json:"content"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
	TokenCount int                    `json:"token_count"`
}

type MessageEmbedding added in v0.9.0

type MessageEmbedding struct {
	TextUUID  uuid.UUID `json:"uuid,omitempty"` // MemoryStore's unique ID associated with this text.
	Text      string    `json:"text"`
	Embedding []float32 `json:"embedding,omitempty"`
	Language  string    `json:"language"`
}

type MessageEmbeddingCollection added in v0.9.0

type MessageEmbeddingCollection struct {
	UUID       uuid.UUID          `json:"uuid,omitempty"`
	Name       string             `json:"name,omitempty"`
	Embeddings []MessageEmbedding `json:"documents"`
}

type MessageEvent

type MessageEvent struct {
	SessionID string                 `json:"sessionId"`
	Messages  []Message              `json:"messages"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

type NotFoundError added in v0.9.0

type NotFoundError struct {
	Resource string
}

func (*NotFoundError) Error added in v0.9.0

func (e *NotFoundError) Error() string

func (*NotFoundError) Unwrap added in v0.9.0

func (e *NotFoundError) Unwrap() error

type SearchDocumentQuery added in v0.9.0

type SearchDocumentQuery struct {
	*Document
	Score float64 `json:"score" bun:"score"`
}

type Session

type Session struct {
	UUID      uuid.UUID              `json:"uuid"`
	CreatedAt time.Time              `json:"created_at"`
	UpdatedAt time.Time              `json:"updated_at"`
	DeletedAt *time.Time             `json:"deleted_at"`
	SessionID string                 `json:"session_id"`
	Metadata  map[string]interface{} `json:"metadata"`
}

type Summary

type Summary struct {
	UUID             uuid.UUID              `json:"uuid"`
	CreatedAt        time.Time              `json:"created_at"`
	Content          string                 `json:"content"`
	SummaryPointUUID uuid.UUID              `json:"recent_message_uuid"` // The most recent message UUID that was used to generate this summary
	Metadata         map[string]interface{} `json:"metadata,omitempty"`
	TokenCount       int                    `json:"token_count"`
}

type UpdateDocumentCollectionRequest added in v0.9.0

type UpdateDocumentCollectionRequest struct {
	Description string                 `json:"description"        validate:"max=1000"`
	Metadata    map[string]interface{} `json:"metadata,omitempty"`
}

type UpdateDocumentListRequest added in v0.9.0

type UpdateDocumentListRequest struct {
	UUID uuid.UUID `json:"uuid" validate:"required"`
	UpdateDocumentRequest
}

type UpdateDocumentRequest added in v0.9.0

type UpdateDocumentRequest struct {
	DocumentID string                 `json:"document_id"        validate:"printascii,max=40,omitempty"`
	Metadata   map[string]interface{} `json:"metadata,omitempty" validate:"omitempty"`
}

type ZepLLM added in v0.10.0

type ZepLLM interface {
	// Call runs the LLM chat completion against the prompt
	// this version of Call uses the chat endpoint of an LLM, but
	// we pass in a simple string prompt
	Call(
		ctx context.Context,
		prompt string,
		options ...llms.CallOption,
	) (string, error)
	// EmbedTexts embeds the given texts
	EmbedTexts(ctx context.Context, texts []string) ([][]float32, error)
	// GetTokenCount returns the number of tokens in the given text
	GetTokenCount(text string) (int, error)
	// Init initializes the LLM
	Init(ctx context.Context, cfg *config.Config) error
}

Jump to

Keyboard shortcuts

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