-
Notifications
You must be signed in to change notification settings - Fork 656
feat: add Python 3.13 support with optional annoy dependency #1718
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
cluster2600
wants to merge
13
commits into
NVIDIA-NeMo:main
Choose a base branch
from
cluster2600:feat/python-3.14-support
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 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e7f2f78
feat: add Python 3.14 support with optional annoy dependency
cluster2600 e36a173
fix: handle edge case when n >= item count in NumpyAnnoyIndex
cluster2600 ae08b33
fix: scope to Python 3.13 support (3.14 blocked by langchain)
cluster2600 636874d
style: fix black formatting in kb.py
cluster2600 c4c6e9b
Address review feedback: promote log level and document edge cases
cluster2600 86d9aed
Update nemoguardrails/kb/kb.py
cluster2600 7db842b
Address second round of review feedback
cluster2600 2434ec0
Fix return type annotation and guard save() against unbuilt index
cluster2600 c315832
Update nemoguardrails/kb/kb.py
cluster2600 3a29d19
Tighten NumpyAnnoyIndex defensiveness and clean up kb.py
cluster2600 15f9a70
Update nemoguardrails/embeddings/numpy_index.py
cluster2600 9684e21
Update nemoguardrails/kb/kb.py
cluster2600 3aacd37
Update nemoguardrails/embeddings/numpy_index.py
cluster2600 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Numpy-based drop-in replacement for annoy.AnnoyIndex. | ||
|
|
||
| This module provides a pure-numpy alternative to the Annoy library for | ||
| nearest-neighbour search over embedding vectors. It is used as a fallback | ||
| when annoy is not installed (e.g. on Python 3.13+ where the annoy C++ | ||
| extension triggers a SIGILL). | ||
|
|
||
| For the typical guardrails index sizes (tens to hundreds of items) the | ||
| brute-force cosine search is more than fast enough. | ||
| """ | ||
|
|
||
| from typing import List, Optional, Tuple, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
||
| class NumpyAnnoyIndex: | ||
| """A numpy-backed nearest-neighbour index that exposes the same API surface | ||
| as ``annoy.AnnoyIndex`` for the subset used by NeMo Guardrails. | ||
|
|
||
| Supported operations: | ||
| * ``add_item(i, vector)`` | ||
| * ``build(n_trees)`` (no-op -- kept for interface compatibility) | ||
| * ``get_nns_by_vector(vector, n, include_distances=False)`` | ||
| * ``save(path)`` / ``load(path)`` | ||
|
|
||
| The metric is *angular* distance, matching Annoy's default for text | ||
| embeddings. Angular distance is defined as | ||
| ``sqrt(2 * (1 - cos_sim))`` so that it is ``0`` for identical vectors | ||
| and ``2`` for diametrically opposed ones. | ||
| """ | ||
|
|
||
| def __init__(self, embedding_size: int, metric: str = "angular"): | ||
| self._embedding_size = embedding_size | ||
| self._metric = metric | ||
| # Sparse storage during build phase (id -> vector) | ||
| self._vectors_dict: dict = {} | ||
| # Dense numpy matrix after build() | ||
| self._vectors: Optional[np.ndarray] = None | ||
| self._built = False | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Build interface | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def add_item(self, i: int, vector) -> None: | ||
| """Add a single vector with integer id *i*.""" | ||
| self._vectors_dict[i] = np.asarray(vector, dtype=np.float32) | ||
|
|
||
| def build(self, n_trees: int = 10) -> None: | ||
| """Finalise the index. The *n_trees* parameter is ignored (kept | ||
| for API compatibility with Annoy).""" | ||
| if not self._vectors_dict: | ||
| self._vectors = np.empty((0, self._embedding_size), dtype=np.float32) | ||
| else: | ||
| max_id = max(self._vectors_dict.keys()) | ||
| self._vectors = np.zeros( | ||
| (max_id + 1, self._embedding_size), dtype=np.float32 | ||
| ) | ||
| for idx, vec in self._vectors_dict.items(): | ||
| self._vectors[idx] = vec | ||
| self._built = True | ||
cluster2600 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Query interface | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def get_nns_by_vector( | ||
| self, vector, n: int, include_distances: bool = False | ||
| ) -> Union[List[int], Tuple[List[int], List[float]]]: | ||
| """Return the *n* nearest neighbours of *vector*. | ||
|
|
||
| When *include_distances* is ``True`` the return value is a tuple | ||
| ``(ids, distances)``; otherwise just ``ids``. | ||
| """ | ||
| if self._vectors is None or len(self._vectors) == 0: | ||
| return ([], []) if include_distances else [] | ||
|
|
||
| query = np.asarray(vector, dtype=np.float32) | ||
|
|
||
| # Cosine similarity via normalised dot product | ||
| norms = np.linalg.norm(self._vectors, axis=1, keepdims=True) | ||
| # Avoid division by zero for zero-vectors | ||
| safe_norms = np.where(norms == 0, 1.0, norms) | ||
| normed = self._vectors / safe_norms | ||
|
|
||
| query_norm = np.linalg.norm(query) | ||
| if query_norm == 0: | ||
| query_normed = query | ||
| else: | ||
| query_normed = query / query_norm | ||
|
|
||
| cos_sim = normed @ query_normed # shape: (num_items,) | ||
|
|
||
| # Angular distance (matches Annoy's definition) | ||
| cos_sim_clipped = np.clip(cos_sim, -1.0, 1.0) | ||
| distances = np.sqrt(2.0 * (1.0 - cos_sim_clipped)) | ||
|
|
||
| # Get top-n indices (lowest distance first) | ||
| n = min(n, len(distances)) | ||
| if n == len(distances): | ||
| # All items requested -- just argsort the whole array | ||
| top_indices = np.argsort(distances)[:n] | ||
| else: | ||
| top_indices = np.argpartition(distances, n)[:n] | ||
| top_indices = top_indices[np.argsort(distances[top_indices])] | ||
|
|
||
| ids = top_indices.tolist() | ||
| if include_distances: | ||
| return ids, distances[top_indices].tolist() | ||
| return ids | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Persistence | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def save(self, path: str) -> None: | ||
| """Save the index to disk as a ``.npy`` file. | ||
|
|
||
| If the caller supplies a path ending in ``.ann`` (the annoy | ||
| convention), we silently swap the extension to ``.npy`` so that | ||
| both backends can coexist in the same cache directory. | ||
|
|
||
| Note: ``numpy.save`` automatically appends ``.npy`` when the | ||
| path does not already end with that suffix, so callers should | ||
| always pass either an ``.ann`` path (which is converted here) | ||
| or an explicit ``.npy`` path. | ||
| """ | ||
| if not self._built: | ||
| raise RuntimeError( | ||
| "NumpyAnnoyIndex.save() called before build(); call build() first." | ||
| ) | ||
| if path.endswith(".ann"): | ||
| path = path[:-4] + ".npy" | ||
| if self._vectors is not None: | ||
| np.save(path, self._vectors) | ||
cluster2600 marked this conversation as resolved.
Show resolved
Hide resolved
cluster2600 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def load(self, path: str) -> None: | ||
| """Load a previously saved index from disk.""" | ||
| if path.endswith(".ann"): | ||
| path = path[:-4] + ".npy" | ||
| self._vectors_dict = {} # discard any pre-build state | ||
| self._vectors = np.load(path).astype(np.float32) | ||
| self._built = True | ||
cluster2600 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.