rag

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RecordEmbeddingRequest added in v0.3.0

func RecordEmbeddingRequest(userID int64, model string, embeddingType string, durationSeconds float64, success bool, tokens int, cost *float64)

RecordEmbeddingRequest записывает метрики embedding запроса. embeddingType: searchTypeTopics или searchTypeFacts

func RecordRAGCandidates added in v0.3.3

func RecordRAGCandidates(userID int64, searchType string, count int)

RecordRAGCandidates записывает количество кандидатов до фильтрации.

func RecordRAGEnrichment added in v0.3.7

func RecordRAGEnrichment(userID int64, durationSeconds float64)

RecordRAGEnrichment записывает время LLM вызова для обогащения запроса.

func RecordRAGLatency added in v0.3.6

func RecordRAGLatency(userID int64, source string, durationSeconds float64)

RecordRAGLatency записывает общее время RAG retrieval. source: "auto" для buildContext, "tool" для search_history tool.

func RecordRAGRetrieval added in v0.3.0

func RecordRAGRetrieval(userID int64, hasContext bool)

RecordRAGRetrieval записывает результат RAG retrieval.

func RecordRerankerCandidatesInput added in v0.4.1

func RecordRerankerCandidatesInput(userID int64, count int)

RecordRerankerCandidatesInput записывает кандидатов на входе.

func RecordRerankerCandidatesOutput added in v0.4.1

func RecordRerankerCandidatesOutput(userID int64, count int)

RecordRerankerCandidatesOutput записывает кандидатов на выходе.

func RecordRerankerCost added in v0.4.1

func RecordRerankerCost(userID int64, cost float64)

RecordRerankerCost записывает стоимость reranker.

func RecordRerankerDuration added in v0.4.1

func RecordRerankerDuration(userID int64, durationSeconds float64)

RecordRerankerDuration записывает время reranker операции.

func RecordRerankerFallback added in v0.4.1

func RecordRerankerFallback(userID int64, reason string)

RecordRerankerFallback записывает срабатывание fallback. reason: "timeout", "error", "max_tool_calls", "invalid_json", "requested_ids", "vector_top"

func RecordRerankerToolCalls added in v0.4.1

func RecordRerankerToolCalls(userID int64, count int)

RecordRerankerToolCalls записывает количество tool calls.

func RecordVectorSearch added in v0.3.0

func RecordVectorSearch(userID int64, searchType string, durationSeconds float64, vectorsScanned int)

RecordVectorSearch записывает метрики vector search.

func UpdateVectorIndexMetrics added in v0.3.0

func UpdateVectorIndexMetrics(topicsCount, factsCount int)

UpdateVectorIndexMetrics обновляет метрики размера индекса.

Types

type ActiveSessionInfo added in v0.3.0

type ActiveSessionInfo struct {
	UserID           int64
	MessageCount     int
	FirstMessageTime time.Time
	LastMessageTime  time.Time
	ContextSize      int // Total characters in session messages
}

ActiveSessionInfo contains information about unprocessed messages for a user.

type ExtractedTopic

type ExtractedTopic struct {
	Summary    string `json:"summary"`
	StartMsgID int64  `json:"start_msg_id"`
	EndMsgID   int64  `json:"end_msg_id"`
}

type FactVectorItem

type FactVectorItem struct {
	FactID    int64
	Embedding []float32
}

type ProcessingStats added in v0.3.0

type ProcessingStats struct {
	MessagesProcessed int `json:"messages_processed"`
	TopicsExtracted   int `json:"topics_extracted"`
	TopicsMerged      int `json:"topics_merged"`
	FactsCreated      int `json:"facts_created"`
	FactsUpdated      int `json:"facts_updated"`
	FactsDeleted      int `json:"facts_deleted"`
	// Usage stats from API calls
	PromptTokens     int      `json:"prompt_tokens"`
	CompletionTokens int      `json:"completion_tokens"`
	EmbeddingTokens  int      `json:"embedding_tokens"`
	TotalCost        *float64 `json:"total_cost,omitempty"` // Cost in USD
}

ProcessingStats contains detailed statistics about session processing.

func (*ProcessingStats) AddChatUsage added in v0.3.0

func (s *ProcessingStats) AddChatUsage(promptTokens, completionTokens int, cost *float64)

AddChatUsage adds usage from a chat completion response.

func (*ProcessingStats) AddEmbeddingUsage added in v0.3.0

func (s *ProcessingStats) AddEmbeddingUsage(tokens int, cost *float64)

AddEmbeddingUsage adds usage from an embedding response.

type ProgressCallback added in v0.3.0

type ProgressCallback func(event ProgressEvent)

ProgressCallback is called during processing to report progress.

type ProgressEvent added in v0.3.0

type ProgressEvent struct {
	Stage    string           `json:"stage"`           // "topics", "consolidation", "facts"
	Current  int              `json:"current"`         // Current item being processed
	Total    int              `json:"total"`           // Total items to process
	Message  string           `json:"message"`         // Human-readable status message
	Complete bool             `json:"complete"`        // True when processing is complete
	Stats    *ProcessingStats `json:"stats,omitempty"` // Final stats when complete
}

ProgressEvent represents a progress update during processing.

type RerankerResult added in v0.4.1

type RerankerResult struct {
	TopicIDs  []int64 // Final selected topic IDs
	PeopleIDs []int64 // For future Social Graph (v0.5)
}

RerankerResult contains the output of the reranker

type RetrievalDebugInfo

type RetrievalDebugInfo struct {
	OriginalQuery    string
	EnrichedQuery    string
	EnrichmentPrompt string
	EnrichmentTokens int
	Results          []TopicSearchResult
}

RetrievalDebugInfo contains trace data for RAG debugging

type RetrievalOptions

type RetrievalOptions struct {
	History        []storage.Message
	SkipEnrichment bool
	Source         string // "auto" for buildContext, "tool" for search_history
}

type SearchResult

type SearchResult struct {
	Message storage.Message
	Score   float32
}

SearchResult kept for backward compatibility if needed, but we are moving to TopicSearchResult. We remove it to force type errors in other files to fix them.

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(logger *slog.Logger, cfg *config.Config, topicRepo storage.TopicRepository, factRepo storage.FactRepository, factHistoryRepo storage.FactHistoryRepository, msgRepo storage.MessageRepository, logRepo storage.LogRepository, rerankerLogRepo storage.RerankerLogRepository, client openrouter.Client, memoryService *memory.Service, translator *i18n.Translator) *Service

func (*Service) FindSimilarFacts

func (s *Service) FindSimilarFacts(ctx context.Context, userID int64, embedding []float32, threshold float32) ([]storage.Fact, error)

func (*Service) ForceProcessUser

func (s *Service) ForceProcessUser(ctx context.Context, userID int64) (int, error)

func (*Service) ForceProcessUserWithProgress added in v0.3.0

func (s *Service) ForceProcessUserWithProgress(ctx context.Context, userID int64, onProgress ProgressCallback) (*ProcessingStats, error)

ForceProcessUserWithProgress processes all unprocessed messages for a user with progress reporting. Unlike ForceProcessUser, this runs consolidation and fact extraction synchronously.

func (*Service) GetActiveSessions added in v0.3.0

func (s *Service) GetActiveSessions() ([]ActiveSessionInfo, error)

GetActiveSessions returns information about unprocessed messages (active sessions) for all users.

func (*Service) LoadNewVectors added in v0.2.1

func (s *Service) LoadNewVectors() error

LoadNewVectors incrementally loads only new topics and facts since last load.

func (*Service) ReloadVectors

func (s *Service) ReloadVectors() error

func (*Service) Retrieve

func (s *Service) Retrieve(ctx context.Context, userID int64, query string, opts *RetrievalOptions) ([]TopicSearchResult, *RetrievalDebugInfo, error)

func (*Service) RetrieveFacts

func (s *Service) RetrieveFacts(ctx context.Context, userID int64, query string) ([]storage.Fact, error)

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

func (*Service) Stop

func (s *Service) Stop()

func (*Service) TriggerConsolidation

func (s *Service) TriggerConsolidation()

type TestMessageResult added in v0.3.0

type TestMessageResult struct {
	Response         string
	TimingTotal      time.Duration
	TimingEmbedding  time.Duration
	TimingSearch     time.Duration
	TimingLLM        time.Duration
	PromptTokens     int
	CompletionTokens int
	TotalCost        float64
	TopicsMatched    int
	FactsInjected    int
	ContextPreview   string
	RAGDebugInfo     *RetrievalDebugInfo
}

TestMessageResult contains the result of a test message sent through the bot pipeline. Used by the debug chat interface to display detailed metrics.

type TopicSearchResult

type TopicSearchResult struct {
	Topic    storage.Topic
	Score    float32
	Messages []storage.Message
}

TopicSearchResult represents a matched topic with its messages

type TopicVectorItem

type TopicVectorItem struct {
	TopicID   int64
	Embedding []float32
}

type UsageInfo added in v0.3.0

type UsageInfo struct {
	PromptTokens     int
	CompletionTokens int
	TotalTokens      int
	Cost             *float64
}

UsageInfo holds usage data from a single API call.

Jump to

Keyboard shortcuts

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