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

Add Amazon Bedrock Embedding function #1361

Merged
merged 5 commits into from
Dec 20, 2023
Merged

Conversation

chezou
Copy link
Contributor

@chezou chezou commented Nov 9, 2023

https://docs.aws.amazon.com/bedrock/latest/userguide/embeddings.html

Description of changes

  • New functionality
    • Support Amazon Bedrock embedding function

Test plan

  • Tests pass locally with pytest for python, yarn test for js

Tested locally by given profile_name with appropreate ~/.aws/config

>>> import boto3
>>> from chromadb.utils.embedding_functions import AmazonBedrockEmbeddingFunction
>>> session = boto3.Session(profile_name="myprofile", region_name="us-east-1")
>>> ef = AmazonBedrockEmbeddingFunction(session=session)
>>> ef(["Hello Bedrock"])
[[-0.73046875, 0.390625, 0.24511719, 0.111816406, 0.83203125, 0.79296875,...,]]

Documentation Changes

Written docstrings as much as possible.

Copy link

github-actions bot commented Nov 9, 2023

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readbility, Modularity, Intuitiveness)

Copy link
Contributor

@beggers beggers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sick, thanks for expanding Chroma's functionality. I have a few notes but once we get them sorted I will be very happy to merge this in.

Returns:
Embeddings: The embeddings for the texts.

Example:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this example into the docstring for the class instead of its __call__ method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 0b4bfd0

embeddings.append(self._invoke(text))
return embeddings

def _invoke(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need __call__() to be a thin wrapper around _invoke(). Would you mind moving this logic into __call__ directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 0b4bfd0

contentType=content_type,
)
embedding = json.load(response.get("body")).get("embedding")
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove this and the re-throw? I want clients to be able to handle exceptions differently depending on the type of exception, e.g. trying again in the case of a timeout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 0b4bfd0

self,
profile_name: Optional[str] = None,
region: Optional[str] = None,
model_name: str = "amazon.titan-embed-text-v1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a cleaner API boundary here and I'd like to hear your thoughts.

This constructor could accept a boto3.Session and optional kwargs to pass into the client() call. That way we give users full access to the configuration options for both their Session and their client, and we have less code to maintain (we don't need to read from os.environ for example). I may be missing something here -- is there a reason we need to accept these and construct the Session ourselves? If not could we accept a Session and config kwargs?

Copy link
Contributor Author

@chezou chezou Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with just passing a boto3.Session and necessary kwargs for the client() call instead of creating a session within the class.

I don't have a specific reason to create a boto3.Session inside the class. I tried to align with the existing codes. Looking at the other classes creates a client internally, and HuggingFaceEmbeddingFunction creates requests.Session within the class.

Copy link
Contributor Author

@chezou chezou Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, do you think we should support all the possible config kwargs as much as possible?

Looking at the boto3 document, I think most of the kwargs are covered by the session, so we may need to pass api_version, use_ssl, verify, and endpoint_url, at least. Or, we may postpone introducing those arguments at this moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tentatively, I removed most of the redundant arguments without adding client-specific arguments. Let me know your thoughts on it.
eedf49d

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already looking way better -- thank you!

One more small ask and then we can check it in: could we have the constructor accept **kwargs and pass them directly to the client() we create? That would give us three wins:
1 - We don't have to manage the retry_config which I imagine some folks will want control over.
2 - Users can specify whatever they want to override in their Session config without us needing to make any code changes.
3 - We don't need to import boto3 in this codebase anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. Fixed by using **kwargs a53e415

- Remove unnecessary function
- Remove re-throw an exception
- Move comment to the right under the class
@chezou chezou removed their assignment Dec 19, 2023
Copy link
Contributor

@beggers beggers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 🚀 🚀

@beggers beggers merged commit 2202df8 into chroma-core:main Dec 20, 2023
95 checks passed
@chezou chezou deleted the bedrock branch December 21, 2023 01:36
@tazarov
Copy link
Contributor

tazarov commented Dec 21, 2023

@chezou, it would be really nice if you can raise a PR against the docs repo - https://github.com/chroma-core/docs/pulls to update the embedding functions and how to use them - https://docs.trychroma.com/embeddings

>>> texts = ["Hello, world!", "How are you?"]
>>> embeddings = bedrock(texts)
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chezou, it would be nice to check if boto3 package is installed and if not guide to user with appropriate error message. Check other EFs as ref.

Copy link
Contributor Author

@chezou chezou Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally did checking it, but I followed @beggers suggestion to remove the dependency of boto3 from chroma itself.
See also #1361 (comment)

)

def __call__(self, input: Documents) -> Embeddings:
import json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to import json here instead of at top-level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, #1562

@tazarov
Copy link
Contributor

tazarov commented Dec 21, 2023

@chezou, on second thought it'd be absolutely awesome if you can also add the JS client equivalent of the EF.

@chezou
Copy link
Contributor Author

chezou commented Jan 20, 2024

@tazarov I made a PR for the docs repo. Please have a look when your team has a chance chroma-core/docs#204

For JS client, I'm not familiar with the JS AWS client, so it may take a while.

[edit] I made a PR for JS client #1659

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants