Skip to content

Commit

Permalink
Merge pull request #53 from iryna-kondr/fix_annoy
Browse files Browse the repository at this point in the history
Make annoy an optional dependency
  • Loading branch information
iryna-kondr committed Aug 15, 2023
2 parents facbef2 + 950b8ca commit 3e7e19a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ Note: as the model is not being re-trained, but uses the training data during in

### Dynamic Few-Shot Text Classification

*To use this feature, you need to install `annoy` library:*

```bash
pip install scikit-llm[annoy]
```

`DynamicFewShotGPTClassifier` dynamically selects N samples per class to include in the prompt. This allows the few-shot classifier to scale to datasets that are too large for the standard context window of LLMs.

*How does it work?*
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ dependencies = [
"pandas>=1.5.0",
"openai>=0.27.0",
"tqdm>=4.60.0",
"annoy>=1.17.2",
"google-cloud-aiplatform>=1.27.0"
]
name = "scikit-llm"
version = "0.3.3"
version = "0.3.4"
authors = [
{ name="Oleg Kostromin", email="[email protected]" },
{ name="Iryna Kondrashchenko", email="[email protected]" },
Expand All @@ -29,6 +28,7 @@ classifiers = [

[project.optional-dependencies]
gpt4all = ["gpt4all>=1.0.0"]
annoy = ["annoy>=1.17.2"]

[tool.ruff]
select = [
Expand Down
10 changes: 9 additions & 1 deletion skllm/memory/_annoy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import tempfile
from typing import Any, List

from annoy import AnnoyIndex
try:
from annoy import AnnoyIndex
except (ImportError, ModuleNotFoundError):
AnnoyIndex = None

from numpy import ndarray

from skllm.memory.base import _BaseMemoryIndex
Expand All @@ -20,6 +24,10 @@ class AnnoyMemoryIndex(_BaseMemoryIndex):
"""

def __init__(self, dim: int, metric: str = "euclidean", **kwargs: Any) -> None:
if AnnoyIndex is None:
raise ImportError(
"Annoy is not installed. Please install annoy by running `pip install scikit-llm[annoy]`."
)
self._index = AnnoyIndex(dim, metric)
self.metric = metric
self.dim = dim
Expand Down

0 comments on commit 3e7e19a

Please sign in to comment.