Feature/implement zvec portable vectordb - #149
Closed
esafwan wants to merge 6 commits into
Closed
Conversation
Introduce huf/ai/knowledge/embedding.py with LiteLLM-based embedding generation: - get_embedding() for single text embedding - get_embeddings() for batch embedding with auto-chunking - resolve_embedding_config() to read model/provider from Knowledge Source DocType and resolve API keys from AI Provider
Add huf/ai/knowledge/backends/zvec_backend.py implementing the KnowledgeBackend ABC using Zvec (Alibaba Proxima-based engine): - initialize(): create/open .zvec collection with typed schema - add_chunks(): batch-embed text via embedding module, upsert docs - delete_chunks(): filter-based deletion by input_id - search(): embed query + approximate nearest-neighbor search - clear(): drop and recreate collection - get_stats(): doc count and on-disk size Collections stored in /private/files/knowledge/ for portability.
Add huf/ai/knowledge/backends/zvec_llamaindex.py bridging Zvec collections to LlamaIndex's BasePydanticVectorStore interface: - add(): convert LlamaIndex BaseNode to zvec.Doc and upsert - delete(): remove documents by ref_doc_id - query(): translate VectorStoreQuery to zvec.VectorQuery Optional adapter for LlamaIndex pipeline compatibility.
- Register 'zvec' backend type in get_backend() factory - Add 'zvec' option to knowledge_type field in Knowledge Source JSON - Add Vector Settings section with embedding_model, vector_dimension, and embedding_provider fields (visible only when knowledge_type=zvec) - Add validate_zvec_settings() in knowledge_source.py - Update knowledge_source.js: remove sqlite_fts-only restriction, add dynamic field visibility for vector settings
Add _build_backend_config() helper that constructs the config dict for backend.initialize(). For zvec sources it includes embedding_model, vector_dimension, and embedding_provider from the Knowledge Source doc. Used in both process_knowledge_input() and rebuild_knowledge_index().
Zvec is the in-process vector database engine (based on Alibaba Proxima) used by ZvecBackend for portable semantic search.
Contributor
Author
TODO before this PR is taken up: align with the HUF knowledge-backend standardSince this PR was opened, HUF adopted a cross-backend contract in PR #280 (pgvector + generic Zvec has no LlamaIndex integration, so under the new standard it must either justify itself under the portable-exception clause (shareable local file, zero external services) or be dropped in favor of
See the cross-backend audit (A3 / P2 reconcile historical vector backends) and use |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds Zvec as a portable, in-process vector database backend for the Huf Knowledge System, enabling semantic (vector similarity) search alongside the existing SQLite FTS keyword search.
Background: What is Zvec?
Zvec is a lightweight, in-process vector database built on Alibaba's Proxima approximate nearest-neighbor engine. It stores embeddings as portable
.zveccollection files — no external server process required.Why Zvec for Huf?
.zvecfiles in/private/files/knowledge/This mirrors the existing SQLite FTS pattern: one portable file per Knowledge Source, stored in Frappe's private files directory, shareable across agents and sites.
What Was Added
New Files
huf/ai/knowledge/embedding.pyget_embedding,get_embeddings,resolve_embedding_config)huf/ai/knowledge/backends/zvec_backend.pyZvecBackend(KnowledgeBackend)— full CRUD + vector searchhuf/ai/knowledge/backends/zvec_llamaindex.pyZvecVectorStore(BasePydanticVectorStore)adapter for LlamaIndex pipeline compatibilityModified Files
huf/ai/knowledge/backends/__init__.pyzvecinget_backend()factoryhuf/huf/doctype/knowledge_source/knowledge_source.jsonzvectoknowledge_typeoptions; add Vector Settings section (embedding_model,vector_dimension,embedding_provider)huf/huf/doctype/knowledge_source/knowledge_source.pyvalidate_zvec_settings()huf/huf/doctype/knowledge_source/knowledge_source.jssqlite_fts-only restriction; dynamic field visibility for vector settingshuf/ai/knowledge/indexer.py_build_backend_config()helper — passes embedding config to vector backendspyproject.tomlzvecdependencyArchitecture
The embedding infrastructure is provider-agnostic — it uses
litellm.embedding()and reads model/API key config from the Knowledge Source and its linked AI Provider document.How to Test
Prerequisites
text-embedding-3-small, Geminimodels/embedding-001)bench pip install zvec(orbench setup requirementsto install from updated pyproject.toml)Steps
Create a Knowledge Source
Knowledge Type=zvecEmbedding Model(e.g.,text-embedding-3-small)Vector Dimension(e.g.,1536for OpenAI,768for Gemini)Embedding ProviderValidate field visibility
Knowledge Typeback tosqlite_fts— vector settings should hidezvec— vector settings should reappear and become requiredAdd a Knowledge Input
.zvecfile is created in{site}/private/files/knowledge/Test search
Verify existing FTS still works — creating/using
sqlite_ftsKnowledge Sources should be unaffectedCommit History