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

cloudflare[minor]: use native Cloudflare Workers AI binding #5287

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Cherry
Copy link

@Cherry Cherry commented May 5, 2024

As of 2024-04-11, there's no need to use the @cloudflare/ai package and it's deprecated:

https://developers.cloudflare.com/workers-ai/changelog/

This swaps the code over to use the native binding directly, and updates @cloudflare/workers-types which now ships the Ai type. No other code changes are needed.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label May 5, 2024
Copy link

vercel bot commented May 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 13, 2024 10:49pm
langchainjs-docs ✅ Ready (Inspect) Visit Preview May 13, 2024 10:49pm

@dosubot dosubot bot added the auto:improvement Medium size change to existing code to handle new use-cases label May 5, 2024
@@ -38,12 +38,11 @@
"author": "LangChain",
Copy link

Choose a reason for hiding this comment

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

Hey there! I noticed that this PR includes a change in dependencies, specifically the removal of "@cloudflare/ai" as a hard dependency and an update to the version of "@cloudflare/workers-types" as a dev dependency. I'm flagging this for your review to ensure it aligns with our dependency management strategy. Thanks!

Copy link
Collaborator

@bracesproul bracesproul left a comment

Choose a reason for hiding this comment

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

Thanks for taking this on, I have a couple comments to prevent breaking changes.

"@langchain/core": "~0.1",
"uuid": "^9.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20231218.0",
"@cloudflare/workers-types": "^4.20240502.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we're replacing @cloudflare/ai with this, it needs to be moved out of dev deps and into standard deps

Copy link
Author

@Cherry Cherry May 6, 2024

Choose a reason for hiding this comment

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

workers-types only provides the types for the Ai binding - there's nothing runtime that would need this to be a dependency and not a devDependency. The runtime for the AI binding is handled entirely in workerd via wrangler.

@@ -14,7 +13,7 @@ type AiTextEmbeddingsOutput = {

export interface CloudflareWorkersAIEmbeddingsParams extends EmbeddingsParams {
/** Binding */
binding: Fetcher;
binding: Ai;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will be a breaking change. Instead of this we should deprecate the binding input, and make it optional. With that we can add an additional (also optional) arg which is typed with the new Ai type.

Copy link
Author

Choose a reason for hiding this comment

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

Hmm, good thoughts. The confusion here might come from the fact that the new AI binding is more of a binding than the old one (the old one is often referred to as a "wrapped binding" in the cf dev ecosystem), so I don't have a good name for this.

Do you have a suggestion for what this could be called? Or would it make sense to just cut a breaking change given how this is in 0.0.x versions right now?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, let's make this a minor version.

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense to me!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this also break people using this outside Cloudflare Workers?

We'll also want to update the docs but I can take care of that.

Copy link
Author

@Cherry Cherry May 13, 2024

Choose a reason for hiding this comment

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

The external usage outside of Cloudflare Workers seems to be in https://github.com/Cherry/langchainjs/blob/4b9192176440b4acab9eac60a140200640d8bee2/libs/langchain-cloudflare/src/chat_models.ts#L78, which hasn't changed. This update to the embeddings.ts via CloudflareWorkersAIEmbeddingsParams seems to have only ever been intended for use inside of Cloudflare Workers, since it requires a Fetcher as the binding, which is a Cloudflare thing, unless I'm missing something?

@bracesproul bracesproul added the in progress PRs that are in progress but not ready to merge label May 6, 2024
@Cherry Cherry changed the title cloudflare[patch]: use native Cloudflare Workers AI binding cloudflare[minor]: use native Cloudflare Workers AI binding May 13, 2024
@jacoblee93
Copy link
Collaborator

Hey so just got wind that the Rest API methods will be available in the main SDK:

https://www.npmjs.com/package/cloudflare

We should wait to land this until that is generally ready

@Cherry
Copy link
Author

Cherry commented May 13, 2024

The general cloudflare SDK makes sense outside of Workers, but bindings inside of Workers are faster and more secure - no additional authentication required, so using it inside a Worker for anything that has native bindings doesn't make a lot of sense in my experience.

Updating https://github.com/Cherry/langchainjs/blob/4b9192176440b4acab9eac60a140200640d8bee2/libs/langchain-cloudflare/src/chat_models.ts#L78 to use the official SDK once the AI endpoints are available there makes sense, but https://github.com/Cherry/langchainjs/blob/fix/workers-ai-native-binding/libs/langchain-cloudflare/src/embeddings.ts seems to be intended for use only within a Worker.

@jacoblee93
Copy link
Collaborator

jacoblee93 commented May 13, 2024

Yeah for sure but ideally we don't lose functionality for people using it outside workers - I've done that myself in demos.

It should accept either a binding or a config object/client IMO.

@Cherry
Copy link
Author

Cherry commented May 13, 2024

Agreed that would be ideal, but it doesn't look like it does today: https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-cloudflare/src/embeddings.ts.

No functionality here will be lost, so perhaps that improvement would be better served in another PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:improvement Medium size change to existing code to handle new use-cases in progress PRs that are in progress but not ready to merge size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants