-
Notifications
You must be signed in to change notification settings - Fork 54
server: semantic similarity queries via sqlite-vec + canonical scoring via cq-schema #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
javiermtorres
wants to merge
20
commits into
main
Choose a base branch
from
21-semantic-similarity-queries
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
588ae9a
Add semantic search support to server store
javiermtorres 70c19b2
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres 9bceb0a
Remove default embeddings endpoint
javiermtorres ff6f201
Fix lint
javiermtorres 56cdba2
Make semsearch modulate current scoring
javiermtorres 16d538a
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres 7fad12d
Add semsearch to the dependencies in CI
javiermtorres 34e2330
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres 7900707
Fix query tests to async protocol
javiermtorres 5ef8132
Merge origin/main into 21-semantic-similarity-queries
peteski22 75070cb
server: replace local scoring module with cq.scoring from the SDK
peteski22 e6a8587
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres e968ee6
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres ab1af3d
docs: document TOKEN_EMBEDDING_URL and SEMSEARCH_EMBEDDING_DIM in ser…
Copilot a05f6dd
tests: add semsearch unit tests with monkey-patched _get_embeddings
Copilot 27b3671
Align with main
javiermtorres 471d155
Fix distance calculation
javiermtorres 44341c0
Fix lint
javiermtorres 763dc4f
Merge branch 'main' into 21-semantic-similarity-queries
javiermtorres 35a9b90
Fix uv.lock
javiermtorres File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| """Semantic-search helpers backed by sqlite-vec and remote token embeddings. | ||
|
|
||
| This module provides optional semantic indexing and retrieval for knowledge units. | ||
| Semantic search is enabled only when `TOKEN_EMBEDDING_URL` is configured and the | ||
| embedding dependencies are installed. | ||
| """ | ||
|
|
||
| import logging | ||
| import os | ||
| import sqlite3 | ||
| from sqlalchemy.sql.expression import TextClause, text | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _ENABLED = False | ||
| _DIM = int(os.environ.get("SEMSEARCH_EMBEDDING_DIM", 768)) | ||
|
|
||
|
|
||
| _TOKEN_EMBEDDING_URL = os.environ.get("TOKEN_EMBEDDING_URL") | ||
| if _TOKEN_EMBEDDING_URL: | ||
| try: | ||
| import sqlite_vec | ||
| import numpy as np | ||
| from httpx import AsyncClient | ||
|
|
||
| _ENABLED = True | ||
|
|
||
| logger.info(f"Token embedding enabled using encoderfile endpoint at {_TOKEN_EMBEDDING_URL}") | ||
| except ImportError: | ||
| logger.warning( | ||
| "TOKEN_EMBEDDING_URL is set but required packages are not installed; " | ||
| "semantic search will be unavailable. To enable, install cq with " | ||
| "the 'embedding' extra: pip install cq-sdk[embedding]", | ||
| exc_info=True, | ||
| ) | ||
|
|
||
|
|
||
| # We have avoided using vec0 table since we won't be doing knn-style | ||
| # search, but rather filtering by domain and then ranking by distance. | ||
| # The syntax for a vec0 table would be: | ||
| # CREATE VIRTUAL TABLE IF NOT EXISTS knowledge_units_vec | ||
| # USING vec0( | ||
| # id TEXT PRIMARY KEY, | ||
| # embedding float[{dim}] | ||
| # ); | ||
| _VEC_SCHEMA_SQL = """ | ||
| CREATE TABLE IF NOT EXISTS knowledge_units_vec( | ||
| id TEXT PRIMARY KEY, | ||
| embedding float[{dim}] | ||
| check( | ||
| typeof(embedding) == 'blob' | ||
| and vec_length(embedding) == {dim} | ||
| ) | ||
| ); | ||
| """ | ||
|
|
||
|
|
||
| _VEC_SEARCH_SQL = """ | ||
| SELECT | ||
| ku.data, | ||
| vec_distance_cosine(vec.embedding, :query_embedding) as distance | ||
| FROM knowledge_units_vec vec | ||
| JOIN knowledge_units ku ON ku.id = vec.id | ||
| WHERE ku.status = 'approved' | ||
| ORDER BY distance | ||
| LIMIT :limit | ||
| """ | ||
|
|
||
| _QUERY_VEC_COMBINED_SQL = """ | ||
| SELECT | ||
| ku.data, | ||
| vec_distance_cosine(vec.embedding, :query_embedding) as distance | ||
| FROM knowledge_units ku | ||
| JOIN knowledge_units_vec vec ON ku.id = vec.id | ||
| WHERE ku.status = 'approved' | ||
| AND ku.id IN ( | ||
| SELECT DISTINCT unit_id | ||
| FROM knowledge_unit_domains | ||
| WHERE domain IN :domains | ||
| ) | ||
| ORDER BY distance LIMIT :limit | ||
| """ | ||
|
|
||
| _VEC_DELETE_SQL = "DELETE FROM knowledge_units_vec WHERE id = :unit_id" | ||
| _VEC_INSERT_SQL = "INSERT INTO knowledge_units_vec (id, embedding) VALUES (:unit_id, :embedding)" | ||
|
|
||
|
|
||
| def is_enabled() -> bool: | ||
| """Return whether semantic search dependencies are available.""" | ||
| return _ENABLED | ||
|
|
||
|
|
||
| def load(conn, _) -> None: | ||
| """Load the sqlite-vec extension into an SQLite connection.""" | ||
| if not _ENABLED: | ||
| return | ||
| conn.enable_load_extension(True) | ||
| sqlite_vec.load(conn) | ||
| conn.enable_load_extension(False) | ||
| ensure_schema(conn) | ||
|
|
||
|
|
||
| def ensure_schema(conn) -> None: | ||
| """Create semantic search virtual table if embedding is enabled.""" | ||
| if not _ENABLED: | ||
| return | ||
| conn.executescript(_VEC_SCHEMA_SQL.format(dim=_DIM)) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.