rag

package
v0.0.0-...-46fd052 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: AGPL-3.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserRole      = "user"
	AssistantRole = "assistant"
	SystemRole    = "system"
	Temperature   = 0.3   // LLM parameter - Sampling temperature, lower is more deterministic, higher is more creative.
	TopP          = 1     // LLM parameter - Alternative to temperature, take the tokens with the top p probability.
	LogProbs      = false // LLM parameter - Whether to return log probabilities of the output tokens.
)
View Source
const (
	// BatchSize is the maximal number of documents manipulated at once by the
	// worker.
	BatchSize = 100
	// MaxBatchRetries caps the attempts at a batch that keeps failing on
	// retryable errors, so one poisoned file cannot stall a trigger. The
	// worker retries a failed job MaxExecCount times (3, see worker/rag), and
	// every execution consumes one attempt: 15 is about five trigger firings.
	MaxBatchRetries = 15
)
View Source
const (
	StatusSuccess      = "success"
	StatusError        = "error"
	StatusNotSupported = "notsupported"
)
View Source
const DocTypeVersion = "1"

DocTypeVersion represents the doctype version. Each time this document structure is modified, update this value

View Source
const IndexStatusPath = "/ai/index/status"

IndexStatusPath is the path of the route on which the RAG indexer posts the indexation status of a file. It is what inst.PageURL takes to build the callback_url given to the indexer.

Variables

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

ErrAssistantNotFound is returned when a conversation references an assistant that is gone (deleted, or never created). The query must fail: answering anyway would silently widen a possibly folder-scoped conversation to whole-instance retrieval.

View Source
var ErrUnknownFolder = errors.New("the folder is in no assistant's knowledge base")

ErrUnknownFolder is returned by Reconcile when the given folder is in no assistant's knowledge base: there is nothing to index in it.

View Source
var ErrWorkspaceMissing = errors.New("the knowledge base folder is not indexed yet")

ErrWorkspaceMissing is returned by checkWorkspace when the knowledge base folder has no workspace on openRAG: no rag-index job has reconciled it yet.

Functions

func CallRAGQuery

func CallRAGQuery(inst *instance.Instance, method string, payload []byte, path string, contentType string) (*http.Response, error)

func CleanInstance

func CleanInstance(inst *instance.Instance) error

func DeleteIndexStatus

func DeleteIndexStatus(inst *instance.Instance, docID string) error

DeleteIndexStatus removes the indexation status of a document. One that never had a status is not an error.

func Index

func Index(inst *instance.Instance, logger logger.Logger, msg IndexMessage) error

Index is the entry point of the rag-index worker.

func Purge

func Purge(inst *instance.Instance) error

Purge deletes everything openRAG holds for the instance: its workspaces, its files and the partition itself. The index statuses are dropped, and so is the rag-index checkpoint, so that the next run indexes the whole instance again from the beginning of the changes feed.

func Query

func Query(inst *instance.Instance, logger logger.Logger, query QueryMessage) error

func Reconcile

func Reconcile(inst *instance.Instance, dirID string) (int, error)

Reconcile pushes a reconcile job for the knowledge base folder, or one per knowledge base folder when dirID is empty. It returns the number of jobs pushed.

func Reset

func Reset(inst *instance.Instance) error

Reset drops the checkpoint and launches a rag-index job, forcing a full re-index from the beginning of the changes feed.

func SetIndexStatus

func SetIndexStatus(inst *instance.Instance, docID, newStatus, rev string) error

Types

type ChatConversation

type ChatConversation struct {
	DocID        string                  `json:"_id"`
	DocRev       string                  `json:"_rev,omitempty"`
	Messages     []ChatMessage           `json:"messages"`
	CozyMetadata *metadata.CozyMetadata  `json:"cozyMetadata"`
	Rels         jsonapi.RelationshipMap `json:"relationships,omitempty"`
}

func Chat

func Chat(inst *instance.Instance, payload ChatPayload) (*ChatConversation, error)

func (*ChatConversation) Clone

func (c *ChatConversation) Clone() couchdb.Doc

func (*ChatConversation) DocType

func (c *ChatConversation) DocType() string

func (*ChatConversation) ID

func (c *ChatConversation) ID() string

func (*ChatConversation) Included

func (c *ChatConversation) Included() []jsonapi.Object
func (c *ChatConversation) Links() *jsonapi.LinksList

func (*ChatConversation) Relationships

func (c *ChatConversation) Relationships() jsonapi.RelationshipMap

func (*ChatConversation) Rev

func (c *ChatConversation) Rev() string

func (*ChatConversation) SetID

func (c *ChatConversation) SetID(id string)

func (*ChatConversation) SetRev

func (c *ChatConversation) SetRev(rev string)

type ChatMessage

type ChatMessage struct {
	ID            string    `json:"id"`
	Role          string    `json:"role"`
	Content       string    `json:"content"`
	Sources       []Source  `json:"sources,omitempty"`
	AttachmentIDs []string  `json:"attachmentIDs,omitempty"`
	CreatedAt     time.Time `json:"createdAt"`
}

type ChatPayload

type ChatPayload struct {
	ChatConversationID string
	Query              string   `json:"q"`
	Stream             *bool    `json:"stream"`
	WebSearch          *bool    `json:"websearch"`
	AssistantID        string   `json:"assistantID,omitempty"`
	AttachmentIDs      []string `json:"attachmentIDs,omitempty"`
}

type IndexMessage

type IndexMessage struct {
	Doctype        string `json:"doctype"`
	ReconcileDirID string `json:"reconcile_dir_id,omitempty"`
}

IndexMessage is the message of rag-index triggers and jobs. Without ReconcileDirID the job processes the changes feed; with it, the job walks the folder's subtree (initial indexing of a new knowledge base folder).

type IndexStatus

type IndexStatus struct {
	CouchID  string `json:"_id,omitempty"`
	CouchRev string `json:"_rev,omitempty"`

	Indexed bool   `json:"indexed"`
	Status  string `json:"status,omitempty"`
	// Revision of the io.cozy.files document this status describes. Callbacks
	// are ordered on it.
	DocRev          string     `json:"docRev,omitempty"`
	LastSuccessDate *time.Time `json:"lastSuccessDate,omitempty"`
	LastErrorDate   *time.Time `json:"lastErrorDate,omitempty"`

	// Read it with relData["_id"], not with Relationship.ResourceIdentifier():
	// the latter looks for "id"/"type" and would silently return an empty
	// reference.
	Rels jsonapi.RelationshipMap `json:"relationships,omitempty"`
}

IndexStatus is the RAG indexation status of a document. Its identifier is the identifier of the document it describes.

func NewIndexStatus

func NewIndexStatus(docID string) *IndexStatus

func (*IndexStatus) Clone

func (s *IndexStatus) Clone() couchdb.Doc

func (*IndexStatus) DocType

func (s *IndexStatus) DocType() string

func (*IndexStatus) ID

func (s *IndexStatus) ID() string

func (*IndexStatus) Rev

func (s *IndexStatus) Rev() string

func (*IndexStatus) SetID

func (s *IndexStatus) SetID(id string)

func (*IndexStatus) SetRev

func (s *IndexStatus) SetRev(rev string)

type PruneResult

type PruneResult struct {
	FilesScanned      int `json:"files_scanned"`
	FilesDeleted      int `json:"files_deleted"`
	WorkspacesDeleted int `json:"workspaces_deleted"`
}

PruneResult summarizes a Prune run.

func Prune

func Prune(inst *instance.Instance) (PruneResult, error)

Prune deletes from openRAG what no knowledge base folder claims any more: the files gone or trashed in the VFS, the files outside every knowledge base folder, and the workspaces of the folders that are in no knowledge base. The index status of a deleted file is dropped too.

type QueryMessage

type QueryMessage struct {
	Task          string   `json:"task"`
	DocID         string   `json:"doc_id"`
	Stream        bool     `json:"stream"`
	WebSearch     bool     `json:"websearch"`
	AttachmentIDs []string `json:"attachmentIDs,omitempty"`
}

type Source

type Source struct {
	SourceType string `json:"sourceType"`
	// Document source fields
	ID             string `json:"id,omitempty"`
	DocType        string `json:"doctype,omitempty"`
	Filename       string `json:"filename,omitempty"`
	FileURL        string `json:"fileUrl,omitempty"`
	ChunkURL       string `json:"chunkUrl,omitempty"`
	Page           int    `json:"page,omitempty"`
	EmailPreview   string `json:"email.preview,omitempty"`
	RelationshipID string `json:"relationship_id,omitempty"`
	ParentID       string `json:"parent_id,omitempty"`
	Subject        string `json:"email.subject,omitempty"`
	Datetime       string `json:"datetime,omitempty"`
	// Web source fields
	URL     string `json:"url,omitempty"`
	Title   string `json:"title,omitempty"`
	Snippet string `json:"snippet,omitempty"`
}

Jump to

Keyboard shortcuts

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