mirror of
https://github.com/hwchase17/langchain
synced 2024-11-11 19:11:02 +00:00
14f3014cce
Thank you for contributing to LangChain! **Description:** Adds Langchain support for Nomic Embed Vision **Twitter handle:** nomic_ai,zach_nussbaum - [x] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Lance Martin <122662504+rlancemartin@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
135 lines
3.8 KiB
Python
135 lines
3.8 KiB
Python
import os
|
|
from typing import List, Literal, Optional, overload
|
|
|
|
import nomic # type: ignore[import]
|
|
from langchain_core.embeddings import Embeddings
|
|
from nomic import embed
|
|
|
|
|
|
class NomicEmbeddings(Embeddings):
|
|
"""NomicEmbeddings embedding model.
|
|
|
|
Example:
|
|
.. code-block:: python
|
|
|
|
from langchain_nomic import NomicEmbeddings
|
|
|
|
model = NomicEmbeddings()
|
|
"""
|
|
|
|
@overload
|
|
def __init__(
|
|
self,
|
|
*,
|
|
model: str,
|
|
nomic_api_key: Optional[str] = ...,
|
|
dimensionality: Optional[int] = ...,
|
|
inference_mode: Literal["remote"] = ...,
|
|
):
|
|
...
|
|
|
|
@overload
|
|
def __init__(
|
|
self,
|
|
*,
|
|
model: str,
|
|
nomic_api_key: Optional[str] = ...,
|
|
dimensionality: Optional[int] = ...,
|
|
inference_mode: Literal["local", "dynamic"],
|
|
device: Optional[str] = ...,
|
|
):
|
|
...
|
|
|
|
@overload
|
|
def __init__(
|
|
self,
|
|
*,
|
|
model: str,
|
|
nomic_api_key: Optional[str] = ...,
|
|
dimensionality: Optional[int] = ...,
|
|
inference_mode: str,
|
|
device: Optional[str] = ...,
|
|
):
|
|
...
|
|
|
|
def __init__(
|
|
self,
|
|
*,
|
|
model: str,
|
|
nomic_api_key: Optional[str] = None,
|
|
dimensionality: Optional[int] = None,
|
|
inference_mode: str = "remote",
|
|
device: Optional[str] = None,
|
|
vision_model: Optional[str] = None,
|
|
):
|
|
"""Initialize NomicEmbeddings model.
|
|
|
|
Args:
|
|
model: model name
|
|
nomic_api_key: optionally, set the Nomic API key. Uses the NOMIC_API_KEY
|
|
environment variable by default.
|
|
dimensionality: The embedding dimension, for use with Matryoshka-capable
|
|
models. Defaults to full-size.
|
|
inference_mode: How to generate embeddings. One of `remote`, `local`
|
|
(Embed4All), or `dynamic` (automatic). Defaults to `remote`.
|
|
device: The device to use for local embeddings. Choices include
|
|
`cpu`, `gpu`, `nvidia`, `amd`, or a specific device name. See
|
|
the docstring for `GPT4All.__init__` for more info. Typically
|
|
defaults to CPU. Do not use on macOS.
|
|
"""
|
|
_api_key = nomic_api_key or os.environ.get("NOMIC_API_KEY")
|
|
if _api_key:
|
|
nomic.login(_api_key)
|
|
self.model = model
|
|
self.dimensionality = dimensionality
|
|
self.inference_mode = inference_mode
|
|
self.device = device
|
|
self.vision_model = vision_model
|
|
|
|
def embed(self, texts: List[str], *, task_type: str) -> List[List[float]]:
|
|
"""Embed texts.
|
|
|
|
Args:
|
|
texts: list of texts to embed
|
|
task_type: the task type to use when embedding. One of `search_query`,
|
|
`search_document`, `classification`, `clustering`
|
|
"""
|
|
|
|
output = embed.text(
|
|
texts=texts,
|
|
model=self.model,
|
|
task_type=task_type,
|
|
dimensionality=self.dimensionality,
|
|
inference_mode=self.inference_mode,
|
|
device=self.device,
|
|
)
|
|
return output["embeddings"]
|
|
|
|
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
|
"""Embed search docs.
|
|
|
|
Args:
|
|
texts: list of texts to embed as documents
|
|
"""
|
|
return self.embed(
|
|
texts=texts,
|
|
task_type="search_document",
|
|
)
|
|
|
|
def embed_query(self, text: str) -> List[float]:
|
|
"""Embed query text.
|
|
|
|
Args:
|
|
text: query text
|
|
"""
|
|
return self.embed(
|
|
texts=[text],
|
|
task_type="search_query",
|
|
)[0]
|
|
|
|
def embed_image(self, uris: List[str]) -> List[List[float]]:
|
|
return embed.image(
|
|
images=uris,
|
|
model=self.vision_model,
|
|
)["embeddings"]
|