For Workes & Pages, what is the name of the domain?
What is the error message?
Error: Ai binding is undefined. Please provide a valid binding.
What is the issue or error you’re encountering
When I am trying to use Ai worker that is deployed to cloudflare I see this error
What are the steps to reproduce the issue?
My wrangler file:
name = “my-harry-potter-ai”
compatibility_date = “2023-05-18”
[ai]
binding = “AI”
And my AI worker function:
import { Ai } from “@cloudflare/ai”;
export async function onRequest(context) {
try {
const ai = new Ai(context.env.AI);
if (context.request.method !== "POST") {
return new Response("Send a POST request with a message in the body", { status: 405 });
}
const { message } = await context.request.json();
console.log("Received message:", message);
const response = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
messages: [
{ role: "system", content: "You are Harry Potter, the famous wizard. Respond to all questions as if you were Harry Potter, using his mannerisms and knowledge of the wizarding world." },
{ role: "user", content: message },
],
});
console.log("AI response:", response);
return Response.json({ response: response.response });
} catch (error) {
console.error(“Error in Worker:”, error);
return Response.json({ error: error.message }, { status: 500 });
}
}
When I run my project locally using " wrangler pages dev – npm start" all is good.
But when I deploy it - I see Error: Ai binding is undefined. Please provide a valid binding.