Documentation
¶
Overview ¶
Package knowledge implements the two-layer RAG knowledge base (global + per-task): document ingestion (chunk → embed), semantic + keyword retrieval, and the OpenAI-compatible embeddings client. It degrades to keyword search when no embedder is configured or pgvector is unavailable.
Index ¶
- func ChunkText(text string, maxChars, overlap int) []string
- func EmbedTexts(ctx context.Context, cfg EmbedConfig, texts []string) ([][]float32, error)
- func ExtractText(path string, raw []byte) (string, error)
- type EmbedConfig
- type Service
- func (s *Service) Categories(taskID int64) ([]string, error)
- func (s *Service) IndexDoc(ctx context.Context, docID int64) error
- func (s *Service) Reindex(ctx context.Context) (int, error)
- func (s *Service) Search(ctx context.Context, query string, taskID int64, topK int) ([]db.KnowledgeHit, string, error)
- func (s *Service) SearchAndLog(ctx context.Context, query string, taskID int64, topK int, agentKey string) ([]db.KnowledgeHit, string, error)
- func (s *Service) SemanticReady() bool
- func (s *Service) TestEmbedder(ctx context.Context) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChunkText ¶
ChunkText splits document text into chunks of roughly maxChars runes, packing whole paragraphs together and hard-splitting any single oversized paragraph (with `overlap` runes carried across the hard-split boundary). Paragraph packing keeps semantic units intact; overlap only kicks in on the rare huge paragraph.
func EmbedTexts ¶
EmbedTexts calls the /embeddings endpoint once for the given inputs (keep the batch modest — callers chunk large sets). Preserves input order via the response's index field.
func ExtractText ¶
ExtractText returns the plain text of an uploaded document. Text formats are read raw; .docx/.pdf go through a best-effort python extractor. The python is chosen by PROBING candidates for the needed library (see findPythonWith), so a misordered PATH (e.g. a homebrew python3 without python-docx shadowing a pyenv one that has it) no longer breaks parsing.
Types ¶
type EmbedConfig ¶
EmbedConfig is one OpenAI-compatible embeddings endpoint (works with 智谱/ SiliconFlow/OpenAI/Ollama — all expose POST {base}/embeddings).
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service orchestrates ingestion (chunk → embed → store) and retrieval over the two-layer knowledge base. It holds only a *db.DB; embedder config is read fresh each call so a settings change takes effect without a restart.
func NewService ¶
func (*Service) Categories ¶
Categories lists knowledge categories visible for the scope (global + task).
func (*Service) IndexDoc ¶
IndexDoc chunks + embeds a document and stores its chunks. No-op when semantic search isn't ready (keyword mode reads doc content directly, no chunks needed).
func (*Service) Reindex ¶
Reindex re-embeds every document (after an embedder/model change). Requires semantic search to be ready.
func (*Service) Search ¶
func (s *Service) Search(ctx context.Context, query string, taskID int64, topK int) ([]db.KnowledgeHit, string, error)
Search retrieves the top-k relevant chunks/docs for a query across global + the given task. Returns the hits and the mode used ("semantic" | "keyword").
func (*Service) SearchAndLog ¶
func (s *Service) SearchAndLog(ctx context.Context, query string, taskID int64, topK int, agentKey string) ([]db.KnowledgeHit, string, error)
SearchAndLog runs Search and records a retrieval-log row (used by agent tools).
func (*Service) SemanticReady ¶
SemanticReady reports whether vector search can run: pgvector present + embedder enabled + a key configured. When false, retrieval uses keyword search.