Skip to content

[ENH] Add Cloudflare Worker AI Embedding Function #4389

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

Merged
merged 1 commit into from
Apr 26, 2025

Conversation

jairad26
Copy link
Contributor

@jairad26 jairad26 commented Apr 25, 2025

Description of changes

This PR adds support for cloudflare worker embedding function across both python and typescript, adds docs, and schema validation

Test plan

How are these changes tested?

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

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs section?

Copy link
Contributor Author

jairad26 commented Apr 25, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

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 (Readability, Modularity, Intuitiveness)

@jairad26 jairad26 marked this pull request as ready for review April 25, 2025 23:42
@jairad26 jairad26 changed the base branch from jai/python-close-clients to graphite-base/4389 April 26, 2025 00:01
@jairad26 jairad26 force-pushed the jai/cloudflare-workers-ef branch from fe8bef0 to 08d65ed Compare April 26, 2025 00:01
@jairad26 jairad26 force-pushed the graphite-base/4389 branch from c14ea0c to 7d01a2b Compare April 26, 2025 00:01
@jairad26 jairad26 changed the base branch from graphite-base/4389 to main April 26, 2025 00:01
if not self.api_key:
raise ValueError(f"The {api_key_env_var} environment variable is not set.")

self._api_url = f"https://api.cloudflare.com/client/v4/accounts/{self.account_id}/ai/run/{self.model_name}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit move base url to a const at top of file

return "cloudflare_workers_ai"

def default_space(self) -> Space:
return "l2"
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this the best default space?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea youre right, they use cosine, updated

api_key_env_var: str = "CHROMA_CLOUDFLARE_API_KEY",
):
"""
Initialize the CloudflareWorkersAIEmbeddingFunction.
Copy link
Collaborator

Choose a reason for hiding this comment

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

might be useful to link to CF docs - https://developers.cloudflare.com/workers-ai/models/

Copy link
Collaborator

@HammadB HammadB left a comment

Choose a reason for hiding this comment

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

Please see minor comments before merge

if not self.api_key:
raise ValueError(f"The {api_key_env_var} environment variable is not set.")

self._api_url = f"https://api.cloudflare.com/client/v4/accounts/{self.account_id}/ai/run/{self.model_name}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Cloudflare also supports AI Gateway, which allows for higher limits and more control - https://developers.cloudflare.com/ai-gateway/

Might be a good idea to also include that for completeness. The difference between the two is the endpoint composition.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, added a gateway_id

@jairad26 jairad26 force-pushed the jai/cloudflare-workers-ef branch from 08d65ed to 3ce0000 Compare April 26, 2025 17:20
@jairad26 jairad26 merged commit b604892 into main Apr 26, 2025
70 checks passed
warpbuild-benchmark-bot bot added a commit to WarpBuilds/chroma that referenced this pull request Apr 26, 2025
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