|
| 1 | +from enum import Enum |
| 2 | +import os |
| 3 | +from typing import Optional, cast |
| 4 | + |
| 5 | +from chromadb.api.types import Documents, Embeddings, EmbeddingFunction |
| 6 | + |
| 7 | + |
| 8 | +class TaskType(str, Enum): |
| 9 | + SEARCH_DOCUMENT = "search_document" |
| 10 | + SEARCH_QUERY = "search_query" |
| 11 | + CLASSIFICATION = "classification" |
| 12 | + CLUSTERING = "clustering" |
| 13 | + |
| 14 | + |
| 15 | +class LongTextMode(str, Enum): |
| 16 | + TRUNCATE = "truncate" |
| 17 | + MEAN = "mean" |
| 18 | + |
| 19 | + |
| 20 | +class NomicEmbeddingFunction(EmbeddingFunction[Documents]): # type: ignore[misc] |
| 21 | + """ |
| 22 | + Nomic Embedding Function using the Nomic Embedding API - https://docs.nomic.ai/atlas/models/text-embedding. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + api_key: Optional[str] = None, |
| 28 | + model_name: Optional[str] = "nomic-embed-text-v1.5", |
| 29 | + *, |
| 30 | + dimensionality: Optional[int] = 768, |
| 31 | + max_tokens_per_text: Optional[int] = 8192, |
| 32 | + long_text_mode: Optional[LongTextMode] = LongTextMode.TRUNCATE, |
| 33 | + task_type: Optional[TaskType] = TaskType.SEARCH_DOCUMENT, |
| 34 | + ) -> None: |
| 35 | + """ |
| 36 | + Initialize the Nomic Embedding Function. |
| 37 | +
|
| 38 | + Read more about the Nomic Embedding API here: https://docs.nomic.ai/reference/api/embed-text-v-1-embedding-text-post#request |
| 39 | +
|
| 40 | + Args: |
| 41 | + api_key (str): The API key to use for the Nomic Embedding API. |
| 42 | + model_name (str): The name of the model to use for text embeddings. E.g. "nomic-embed-text-v1.5" (see https://docs.nomic.ai/atlas/models/text-embedding for available models). |
| 43 | + dimensionality (int): The dimensionality of the embeddings. E.g. 768 for "nomic-embed-text-v1.5". |
| 44 | + max_tokens_per_text (int): The maximum number of tokens per text. E.g. 8192 for "nomic-embed-text-v1.5". |
| 45 | + long_text_mode (str): The mode to use for long texts. E.g. "truncate" or "mean". |
| 46 | + task_type (str): The task type to use for the Nomic Embedding API. E.g. "search_document", "search_query", "classification", and "clustering". |
| 47 | + """ |
| 48 | + try: |
| 49 | + import httpx |
| 50 | + except ImportError: |
| 51 | + raise ValueError( |
| 52 | + "The httpx python package is not installed. Please install it with `pip install httpx`" |
| 53 | + ) |
| 54 | + |
| 55 | + if not api_key and os.getenv("NOMIC_API_KEY") is None: |
| 56 | + raise ValueError( |
| 57 | + "No Nomic API key provided or NOMIC_API_KEY environment variable is not set" |
| 58 | + ) |
| 59 | + if not api_key: |
| 60 | + api_key = os.getenv("NOMIC_API_KEY") |
| 61 | + |
| 62 | + self._api_url = "https://api-atlas.nomic.ai/v1/embedding/text" |
| 63 | + self._model_name = model_name |
| 64 | + self._task_type = task_type |
| 65 | + self._dimensionality = dimensionality |
| 66 | + self._long_text_mode = long_text_mode |
| 67 | + self._max_tokens_per_text = max_tokens_per_text |
| 68 | + self._client = httpx.Client() |
| 69 | + self._client.headers.update( |
| 70 | + { |
| 71 | + "Content-Type": "application/json", |
| 72 | + "Authorization": f"Bearer {api_key}", |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + def __call__(self, input: Documents) -> Embeddings: |
| 77 | + """ |
| 78 | + Get the embeddings for a list of texts. |
| 79 | +
|
| 80 | + Args: |
| 81 | + input (Documents): A list of texts to get embeddings for. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + Embeddings: The embeddings for the texts. |
| 85 | +
|
| 86 | + Example: |
| 87 | + >>> from chromadbx.embeddings.nomic import NomicEmbeddingFunction |
| 88 | + >>> nomic_ef = NomicEmbeddingFunction(model_name="nomic-embed-text-v1.5") |
| 89 | + >>> texts = ["Hello, world!", "How are you?"] |
| 90 | + >>> embeddings = nomic_ef(texts) |
| 91 | + """ |
| 92 | + texts = input if isinstance(input, list) else [input] |
| 93 | + |
| 94 | + response = self._client.post( |
| 95 | + self._api_url, |
| 96 | + json={ |
| 97 | + "model": self._model_name, |
| 98 | + "texts": texts, |
| 99 | + "task_type": self._task_type.value if self._task_type else None, |
| 100 | + "dimensionality": self._dimensionality, |
| 101 | + "long_text_mode": self._long_text_mode.value |
| 102 | + if self._long_text_mode |
| 103 | + else None, |
| 104 | + "max_tokens_per_text": self._max_tokens_per_text, |
| 105 | + }, |
| 106 | + ) |
| 107 | + response.raise_for_status() |
| 108 | + response_json = response.json() |
| 109 | + if "embeddings" not in response_json: |
| 110 | + raise RuntimeError("Nomic API did not return embeddings") |
| 111 | + |
| 112 | + return cast(Embeddings, response_json["embeddings"]) |
0 commit comments