From 8e12af581175c55aacdab6dec908d6eb796c98c1 Mon Sep 17 00:00:00 2001 From: Govind S B Date: Wed, 6 Mar 2024 18:09:28 +0530 Subject: [PATCH 1/3] added together ai embeddings endpoint support --- chromadb/utils/embedding_functions.py | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/chromadb/utils/embedding_functions.py b/chromadb/utils/embedding_functions.py index f54ab88c42e..f874f86a90a 100644 --- a/chromadb/utils/embedding_functions.py +++ b/chromadb/utils/embedding_functions.py @@ -50,6 +50,44 @@ def _verify_sha256(fname: str, expected_sha256: str) -> bool: return sha256_hash.hexdigest() == expected_sha256 +class TogetherAIEmbeddingFunction(EmbeddingFunction[Documents]): + # Together AI Embeddings Quick Start Reference + # https://docs.together.ai/docs/embeddings-rest + # Models List + # https://docs.together.ai/docs/embedding-models + + def __init__( + self, + api_key: str, + model_name: str = "togethercomputer/m2-bert-80M-8k-retrieval", + ): + self._api_url = "https://api.together.xyz/v1/embeddings" + self._model_name = model_name + self._session = requests.Session() + self._session.headers.update({ + "Authorization": f"Bearer {api_key}", + "Content-Type": "application/json" + }) + + def __call__(self, input: Documents) -> Embeddings: + embeddings = [] + for text in input: + response = self._session.post( + self._api_url, + json={ + "input": text, + "model": self._model_name + } + ) + + if response.status_code == 200: + response_data = response.json() + embedding = response_data.get("data", [])[0].get("embedding", []) + embeddings.append(embedding) + + return embeddings + + class SentenceTransformerEmbeddingFunction(EmbeddingFunction[Documents]): # Since we do dynamic imports we have to type this as Any models: Dict[str, Any] = {} From bd447848cd7d9b125abb8a2f026461acbd8cb119 Mon Sep 17 00:00:00 2001 From: "Govind.S.B" Date: Mon, 11 Mar 2024 01:07:58 +0000 Subject: [PATCH 2/3] issue resolved --- chromadb/utils/embedding_functions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chromadb/utils/embedding_functions.py b/chromadb/utils/embedding_functions.py index f874f86a90a..2d1a89e8252 100644 --- a/chromadb/utils/embedding_functions.py +++ b/chromadb/utils/embedding_functions.py @@ -60,8 +60,9 @@ def __init__( self, api_key: str, model_name: str = "togethercomputer/m2-bert-80M-8k-retrieval", + api_url = "https://api.together.xyz/v1/embeddings" ): - self._api_url = "https://api.together.xyz/v1/embeddings" + self._api_url = api_url self._model_name = model_name self._session = requests.Session() self._session.headers.update({ @@ -84,6 +85,8 @@ def __call__(self, input: Documents) -> Embeddings: response_data = response.json() embedding = response_data.get("data", [])[0].get("embedding", []) embeddings.append(embedding) + else: + raise ValueError(f"The API request to Together AI Endpoint failed with the status code : {response.status_code}. Refer https://docs.together.ai/reference/embeddings more details") return embeddings From 67220124c661b2e23f631f1cf01d497f3af5564d Mon Sep 17 00:00:00 2001 From: "Govind.S.B" Date: Wed, 10 Apr 2024 02:46:07 +0000 Subject: [PATCH 3/3] added api key source --- chromadb/utils/embedding_functions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/chromadb/utils/embedding_functions.py b/chromadb/utils/embedding_functions.py index 2d1a89e8252..22d7810710b 100644 --- a/chromadb/utils/embedding_functions.py +++ b/chromadb/utils/embedding_functions.py @@ -55,6 +55,8 @@ class TogetherAIEmbeddingFunction(EmbeddingFunction[Documents]): # https://docs.together.ai/docs/embeddings-rest # Models List # https://docs.together.ai/docs/embedding-models + # You can get your API Keys from here : + # https://api.together.xyz/settings/api-keys def __init__( self,