Skip to content

Commit

Permalink
nomic: implement local embeddings with the inference_mode parameter (#…
Browse files Browse the repository at this point in the history
…21934)

## Description

This PR implements local and dynamic mode in the Nomic Embed integration
using the inference_mode and device parameters. They work as documented
[here](https://docs.nomic.ai/reference/python-api/embeddings#local-inference).

<!-- If no one reviews your PR within a few days, please @-mention one
of baskaryan, efriis, eyurtsev, hwchase17. -->

---------

Co-authored-by: Erick Friis <erickfriis@gmail.com>
  • Loading branch information
cebtenzzre and efriis committed May 20, 2024
1 parent 0e72ed3 commit 25d1c1c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
52 changes: 49 additions & 3 deletions libs/partners/nomic/langchain_nomic/embeddings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
from typing import List, Optional
from typing import List, Literal, Optional, overload

import nomic # type: ignore
import nomic # type: ignore[import]
from langchain_core.embeddings import Embeddings
from nomic import embed # type: ignore
from nomic import embed


class NomicEmbeddings(Embeddings):
Expand All @@ -17,25 +17,69 @@ class NomicEmbeddings(Embeddings):
model = NomicEmbeddings()
"""

@overload
def __init__(
self,
*,
model: str,
dimensionality: Optional[int] = ...,
inference_mode: Literal["remote"] = ...,
):
...

@overload
def __init__(
self,
*,
model: str,
dimensionality: Optional[int] = ...,
inference_mode: Literal["local", "dynamic"],
device: Optional[str] = ...,
):
...

@overload
def __init__(
self,
*,
model: 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,
):
"""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

def embed(self, texts: List[str], *, task_type: str) -> List[List[float]]:
"""Embed texts.
Expand All @@ -51,6 +95,8 @@ def embed(self, texts: List[str], *, task_type: str) -> List[List[float]]:
model=self.model,
task_type=task_type,
dimensionality=self.dimensionality,
inference_mode=self.inference_mode,
device=self.device,
)
return output["embeddings"]

Expand Down
10 changes: 5 additions & 5 deletions libs/partners/nomic/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libs/partners/nomic/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "langchain-nomic"
version = "0.1.0"
version = "0.1.1"
description = "An integration package connecting Nomic and LangChain"
authors = []
readme = "README.md"
Expand All @@ -13,7 +13,7 @@ license = "MIT"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
langchain-core = ">=0.1.46,<0.3"
nomic = "^3.0.12"
nomic = "^3.0.29"

[tool.poetry.group.test]
optional = true
Expand Down

0 comments on commit 25d1c1c

Please sign in to comment.