knowledge

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: AGPL-3.0 Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChunkText

func ChunkText(text string, maxChars, overlap int) []string

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

func EmbedTexts(ctx context.Context, cfg EmbedConfig, texts []string) ([][]float32, error)

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

func ExtractText(path string, raw []byte) (string, error)

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

type EmbedConfig struct {
	BaseURL string
	Model   string
	APIKey  string
	Dim     int
}

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 NewService(pg *db.DB) *Service

func (*Service) Categories

func (s *Service) Categories(taskID int64) ([]string, error)

Categories lists knowledge categories visible for the scope (global + task).

func (*Service) IndexDoc

func (s *Service) IndexDoc(ctx context.Context, docID int64) error

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

func (s *Service) Reindex(ctx context.Context) (int, error)

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

func (s *Service) SemanticReady() bool

SemanticReady reports whether vector search can run: pgvector present + embedder enabled + a key configured. When false, retrieval uses keyword search.

func (*Service) TestEmbedder

func (s *Service) TestEmbedder(ctx context.Context) (int, error)

TestEmbedder verifies the configured embedder by embedding a probe string. Returns the vector dimension actually returned.

Jump to

Keyboard shortcuts

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