-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Add support for NVIDIA AI LLMs and embedding models (#1293)
- Loading branch information
Showing
18 changed files
with
195 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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 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
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains 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 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 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,26 @@ | ||
import logging | ||
import os | ||
from typing import Optional | ||
|
||
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings | ||
|
||
from embedchain.config import BaseEmbedderConfig | ||
from embedchain.embedder.base import BaseEmbedder | ||
from embedchain.models import VectorDimensions | ||
|
||
|
||
class NvidiaEmbedder(BaseEmbedder): | ||
def __init__(self, config: Optional[BaseEmbedderConfig] = None): | ||
if "NVIDIA_API_KEY" not in os.environ: | ||
raise ValueError("NVIDIA_API_KEY environment variable must be set") | ||
|
||
super().__init__(config=config) | ||
|
||
model = self.config.model or "nvolveqa_40k" | ||
logging.info(f"Using NVIDIA embedding model: {model}") | ||
embedder = NVIDIAEmbeddings(model=model) | ||
embedding_fn = BaseEmbedder._langchain_default_concept(embedder) | ||
self.set_embedding_fn(embedding_fn=embedding_fn) | ||
|
||
vector_dimension = self.config.vector_dimension or VectorDimensions.NVIDIA_AI.value | ||
self.set_vector_dimension(vector_dimension=vector_dimension) |
This file contains 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 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,47 @@ | ||
import os | ||
from collections.abc import Iterable | ||
from typing import Optional, Union | ||
|
||
from langchain.callbacks.manager import CallbackManager | ||
from langchain.callbacks.stdout import StdOutCallbackHandler | ||
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler | ||
|
||
try: | ||
from langchain_nvidia_ai_endpoints import ChatNVIDIA | ||
except ImportError: | ||
raise ImportError( | ||
"NVIDIA AI endpoints requires extra dependencies. Install with `pip install langchain-nvidia-ai-endpoints`" | ||
) from None | ||
|
||
from embedchain.config import BaseLlmConfig | ||
from embedchain.helpers.json_serializable import register_deserializable | ||
from embedchain.llm.base import BaseLlm | ||
|
||
|
||
@register_deserializable | ||
class NvidiaLlm(BaseLlm): | ||
def __init__(self, config: Optional[BaseLlmConfig] = None): | ||
if "NVIDIA_API_KEY" not in os.environ: | ||
raise ValueError("NVIDIA_API_KEY environment variable must be set") | ||
|
||
super().__init__(config=config) | ||
|
||
def get_llm_model_answer(self, prompt): | ||
return self._get_answer(prompt=prompt, config=self.config) | ||
|
||
@staticmethod | ||
def _get_answer(prompt: str, config: BaseLlmConfig) -> Union[str, Iterable]: | ||
callback_manager = [StreamingStdOutCallbackHandler()] if config.stream else [StdOutCallbackHandler()] | ||
model_kwargs = config.model_kwargs or {} | ||
labels = model_kwargs.get("labels", None) | ||
params = {"model": config.model} | ||
if config.system_prompt: | ||
params["system_prompt"] = config.system_prompt | ||
if config.temperature: | ||
params["temperature"] = config.temperature | ||
if config.top_p: | ||
params["top_p"] = config.top_p | ||
if labels: | ||
params["labels"] = labels | ||
llm = ChatNVIDIA(**params, callback_manager=CallbackManager(callback_manager)) | ||
return llm.invoke(prompt).content if labels is None else llm.invoke(prompt, labels=labels).content |
This file contains 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 |
---|---|---|
|
@@ -9,3 +9,4 @@ class VectorDimensions(Enum): | |
HUGGING_FACE = 384 | ||
GOOGLE_AI = 768 | ||
MISTRAL_AI = 1024 | ||
NVIDIA_AI = 1024 |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "embedchain" | ||
version = "0.1.88" | ||
version = "0.1.89" | ||
description = "Simplest open source retrieval(RAG) framework" | ||
authors = [ | ||
"Taranjeet Singh <[email protected]>", | ||
|