Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: AmazonBedrockEmbeddingFunction support for Cohere Embed models #1675

Closed
wants to merge 5 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 42 additions & 15 deletions chromadb/utils/embedding_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,27 +742,54 @@ def __init__(
"""

self._model_name = model_name
self._model_provider = self._model_name.split('.')[0]

self._bedrock_client = session.client(
service_name="bedrock",
**kwargs
)

self._model_details = self._bedrock_client.get_foundation_model(modelIdentifier=self._model_name)['modelDetails']
assert "EMBEDDING" in self._model_details['outputModalities'], f"{self._model_name} doesn't have embedding modality output!"
JGalego marked this conversation as resolved.
Show resolved Hide resolved

self._client = session.client(
self._bedrock_runtime_client = session.client(
service_name="bedrock-runtime",
**kwargs,
)

def call_model(self, body) -> dict:
body = json.dumps(body)
response = self._bedrock_runtime_client.invoke_model(
body=body,
modelId=self._model_name,
accept="application/json",
contentType="application/json",
)
return response

def __call__(self, input: Documents) -> Embeddings:
accept = "application/json"
content_type = "application/json"
embeddings = []
for text in input:
input_body = {"inputText": text}
body = json.dumps(input_body)
response = self._client.invoke_model(
body=body,
modelId=self._model_name,
accept=accept,
contentType=content_type,
)
embedding = json.load(response.get("body")).get("embedding")
embeddings.append(embedding)
if self._model_provider == "amazon":
embeddings = []
for text in input:
input_body = {
"inputText": text
}
response = self.call_model(input_body)
embedding = json.load(response.get("body")).get("embedding")
embeddings.append(embedding)
elif self._model_provider == "cohere":
# See Amazon Bedrock User Guide > Cohere Embed models for more information
# https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-embed.html
assert len(input) <= 128, f"Input texts exceeds max size (Got: {len(input)}, Expected: <=128)"
JGalego marked this conversation as resolved.
Show resolved Hide resolved
assert all(len(text) <= 2048 for text in input), f"Input contains texts exceeding max length (2048)"
input_body = {
"texts": input,
JGalego marked this conversation as resolved.
Show resolved Hide resolved
"input_type": "search_document"
JGalego marked this conversation as resolved.
Show resolved Hide resolved
}
response = self.call_model(input_body)
embeddings = json.load(response.get("body")).get("embeddings")
else:
JGalego marked this conversation as resolved.
Show resolved Hide resolved
raise NotImplementedError(f"Model {self._model_name} is not supported!")
return embeddings


Expand Down