-
Notifications
You must be signed in to change notification settings - Fork 744
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 support for Workers AI in local mode #4522
Add support for Workers AI in local mode #4522
Conversation
🦋 Changeset detectedLatest commit: 8e79be9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7103899088/npm-package-wrangler-4522 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7103899088/npm-package-wrangler-4522 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7103899088/npm-package-wrangler-4522 dev path/to/script.js Additional artifacts:npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7103899088/npm-package-miniflare-4522 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7103899088/npm-package-cloudflare-pages-shared-4522 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7103899088/npm-package-create-cloudflare-4522 Note that these links will no longer work once the GitHub Actions artifact expires.
| Please ensure constraints are pinned, and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! 🎉 Added a few comments...
packages/wrangler/src/ai/fetcher.ts
Outdated
import {getAccountId} from "../user"; | ||
import {performApiFetch} from "../cfetch/internal"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll likely need to format your code for checks to pass. Run pnpm run prettify
in the root of the repository.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formated
serviceBindings: config.serviceBindings, | ||
serviceBindings: { | ||
...config.serviceBindings, | ||
AI: AIFetcher |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should only be adding an AI binding if it's enabled. The binding may not be bound as AI
too. See bindings.ai
for the required configuration. If bindings.ai === undefined
, this binding shouldn't be set. If it's defined, AIFetcher
should be bound as bindings.ai.binding
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed the issue, please take a look now
"Workers AI is not currently supported in local mode. Please use --remote to work with it." | ||
); | ||
} | ||
|
||
if (!config.bindings.ai && config.bindings.vectorize?.length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll want to remove this check too, so we still warn that Vectorize bindings are unsupported if AI bindings are enabled.
if (!config.bindings.ai && config.bindings.vectorize?.length) { | |
if (config.bindings.vectorize?.length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
For testing this, I'd probably copy |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4522 +/- ##
==========================================
+ Coverage 75.46% 75.48% +0.02%
==========================================
Files 240 241 +1
Lines 12851 12867 +16
Branches 3309 3315 +6
==========================================
+ Hits 9698 9713 +15
- Misses 3153 3154 +1
|
290fc97
to
8b91f7d
Compare
14b20b1
to
1ac3363
Compare
Hey @mrbbot can you take a look at this pr again? |
@mrbbot all builds are now passing, can you merge this and give an approximate timeline for release? |
@G4brym @mrbbot this is great, thanks so much for working on it! i just upgraded to v3.20.0 to try it out, but when i try to use my worker that has AI bindings locally, i get the following error:
my invocation looks like: const ai = new Ai(context.env.AI);
// https://developers.cloudflare.com/workers-ai/models/text-generation/#available-embedding-models
const stream = await ai.run('@cf/meta/llama-2-7b-chat-int8', {
messages: [
{ content: PROMPT_CONTEXT, role: 'system' },
{ content: prompt, role: 'user' },
],
stream: true,
});
return new Response(stream, {
headers: { 'content-type': 'text/event-stream' },
}); note that the above code works 100% when running wrangler with the |
Hey @acusti can you run |
@G4brym that worked! thanks so much for the quick solution, and sorry for the trouble. |
Fixes AI-380
What this PR solves / how to test:
Adds support for Workers AI in local mode.
To test this run an example worker from the Workers AI Docs run wrangler in local mode and the script should be able to execute successfully.
Author has addressed the following:
will update the developer docs in a following pr to remove warnings about local mode.Note for PR author:
We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label
highlight pr review
so future reviewers can take inspiration and learn from it.