Skip to content

Feature/zvec vector db integration#168

Draft
esafwan wants to merge 8 commits into
developfrom
feature/zvec-vectorDB-integration
Draft

Feature/zvec vector db integration#168
esafwan wants to merge 8 commits into
developfrom
feature/zvec-vectorDB-integration

Conversation

@esafwan

@esafwan esafwan commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Zvec Portable Vector Database Integration

Summary

This PR integrates Zvec as an in-process, portable vector database for HUF's knowledge system. It sets up the backend infrastructure, adds LlamaIndex adapters, and registers Zvec as a Knowledge Source backend.

Additionally, due to Frappe 16's Python 3.13+ requirements, this PR switches the upstream zvec dependency to a custom fork (tridz-zvec) that publishes wheels for modern Python versions.

Features Added

1. Architectural Additions

  • Added base embedding infrastructure for vector backends.
  • Passed embedding configuration from the indexer down to the vector backends.

2. Zvec Integration

  • Implemented ZvecBackend for portable and fast local semantic search.
  • Added a LlamaIndex VectorStore adapter for Zvec.
  • Registered Zvec within the Knowledge Source DocType and backend registry, making it a selectable storage engine from the UI.

3. Build & Dependency Updates

  • Added tridz-zvec dependency to pyproject.toml.

🛠 Why a Custom Fork is Needed (tridz-zvec)

The Problem

Version Python Support
Frappe 16 requires Python >= 3.13 / 3.14
zvec 0.2.0 wheels for Python 3.10, 3.11, 3.12

zvec publishes binary wheels only (no source distribution). So pip install zvec fails outright on Python 3.13+. It’s still an alpha release (0.2.0) and hasn’t shipped wheels for Python 3.13 / 3.14 yet.

What’s Actually Blocking Python 3.13+

The C++ core (pybind11 + CMake) is fine, and its dependency numpy >= 1.23 supports Python 3.13+. The blocker is purely in zvec's pyproject.toml cibuildwheel configuration, which purposely stops at cp312-*.
See upstream issue: alibaba/zvec#131 (No response yet).

The Solution: tridz-zvec (TO BE DONE)

To unblock development temporarily, a fork under tridz-dev/zvec was created and published:

  1. Updated pyproject.toml to build for cp313-* and cp314-*.
  2. Added GitHub Actions to build and publish wheels to PyPI under the name tridz-zvec.
  3. The import path in HUF remains standard (import zvec), but the required package is now tridz-zvec.

Note: Once upstream alibaba/zvec adds Python 3.13 support, we can switch the dependency name back to zvec.


How to Test

  1. Rebuild and install dependencies.
  2. In the UI, navigate to Knowledge Source.
  3. Create a new source and verify Zvec is newly available as a backend vector.
  4. Add documents to the source and test index/search functionalities (they should seamlessly route to the new ZvecBackend).

esafwan and others added 8 commits February 18, 2026 07:42
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.
Alibaba's zvec only ships wheels for Python 3.10-3.12, which conflicts with Frappe 16's Python 3.13+ requirement. Switch to tridz-zvec, a fork of alibaba/zvec at github.com/tridz-dev/zvec that adds cp313 and cp314 wheel targets. The import name (import zvec) is unchanged.
@esafwan

esafwan commented Jul 19, 2026

Copy link
Copy Markdown
Contributor Author

TODO before this PR is taken up: align with the HUF knowledge-backend standard

Since this PR was opened, HUF adopted the cross-backend contract in PR #280 (pgvector + generic advanced_config schema). The zvec backend diff is largely identical to #149, and the two PRs should probably be consolidated or one closed.

Like #149, zvec has no LlamaIndex integration, so it must justify itself under the portable-exception clause (shareable local file, zero external services) or be dropped in favor of sqlite_vec, which already covers that niche. Git history shows zvec was previously added and then removed.

  • Remove the unrelated deletions of AI-ELEMENTS-USAGE.md and AI-ELEMENTS-WEBPREVIEW-ARTIFACT-GUIDE.md from this PR, or split them into a separate cleanup PR.
  • Decide exception-vs-drop with a concise rationale of what zvec offers over sqlite_vec.
  • If kept: conform huf/ai/knowledge/backends/zvec_backend.py to KnowledgeBackend and implement get_advanced_config_schema(); hand-rolled is fine under the exception.
  • Resolve the status of huf/ai/knowledge/backends/zvec_llamaindex.py — either wire it in or delete it.
  • Reconcile pyproject.toml with the dependency story in the PR description (the diff adds zvec; the description mentions a tridz-zvec fork).
  • Live end-to-end test bar per PR feat: add PGVector knowledge source #280 (UI save → index → query → verify artifact).

See the cross-backend audit (A3 / P2 reconcile historical vector backends) and use pgvector_backend.py / chroma_backend.py (PR #280) as reference implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant