How to change ai workers in cloudflare llm app template

how do i change ai model in llm app template of cloudflare
26 Replies
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the documentation, you can change the AI model in the LLM App template of Cloudflare by modifying the AI binding in your Wrangler configuration file (wrangler.toml) and updating the model reference in your code. Here are the steps: 1. Update the ai block in your wrangler.toml file to reference the new model:
[ai]
binding = "AI"
model = "@cf/meta/llama-2-7b-chat-int8" # Replace with the new model ID
[ai]
binding = "AI"
model = "@cf/meta/llama-2-7b-chat-int8" # Replace with the new model ID
2. In your code, update the model reference to match the new model ID:
const answer = await env.AI.run("@cf/meta/llama-2-7b-chat-int8", { // Replace with the new model ID
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
const answer = await env.AI.run("@cf/meta/llama-2-7b-chat-int8", { // Replace with the new model ID
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
Make sure to replace @cf/meta/llama-2-7b-chat-int8 with the ID of the new model you want to use. You can find the list of available models in the Cloudflare documentation: https://developers.cloudflare.com/workers-ai/models/ After updating the model, redeploy your Worker using wrangler deploy to apply the changes. Source: https://developers.cloudflare.com/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai/
Feedback
Feedback has been submitted! Thank you :)
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
i am facing issue in it can i also add files in it? @SuperHelpflare
SuperHelpflare
SuperHelpflare3mo ago
Support AI
It seems like you're trying to reference a specific model ID. The model ID @cf/meta/llama-3-8b-instruct is mentioned in the documentation as a text generation model. If you want to use this model in your LLM App template, you can update the AI binding in your Wrangler configuration file (wrangler.toml) and the model reference in your code to match this ID. Here's an example:
[ai]
binding = "AI"
model = "@cf/meta/llama-3-8b-instruct"
[ai]
binding = "AI"
model = "@cf/meta/llama-3-8b-instruct"
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
Make sure to redeploy your Worker using wrangler deploy after updating the model. Source: https://developers.cloudflare.com/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai/
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare can i add files in the llm app template of ai can it understand
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the documentation, the LLM App template in Cloudflare Workers AI supports adding files to the project. You can add files to your project by creating a new file in the src directory of your project. For example, you can create a new file called data.json in the src directory:
hello-ai/
src/
index.ts
data.json
...
hello-ai/
src/
index.ts
data.json
...
In your code, you can then import and use the data from the data.json file:
import data from './data.json';

export default {
async fetch(request, env, ctx) {
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
// Use the data from the data.json file
console.log(data);
return new Response(JSON.stringify(answer));
},
};
import data from './data.json';

export default {
async fetch(request, env, ctx) {
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: `What is the square root of 9?` }],
});
// Use the data from the data.json file
console.log(data);
return new Response(JSON.stringify(answer));
},
};
The LLM App template uses the esbuild bundler to bundle your code, so you can use ES6-style imports to import files. As for whether the LLM model can understand the contents of the files, it depends on the specific model and the type of data in the file. The LLM model can process text data, so if you have a file containing text data, the model can potentially understand its contents. However, if you have a file containing non-text data, such as images or audio files, the LLM model may not be able to understand its contents. Source: https://developers.cloudflare.com/workers-ai/guides/tutorials/build-a-retrieval-augmented-generation-ai/
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare no like can i add from client side? not server side
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the documentation, you can upload files from the client-side to the LLM App template in Cloudflare Workers AI using the fetch API. Here's an example of how you can upload a file from the client-side:
// Client-side code
const fileInput = document.getElementById('file-input');
const file = fileInput.files[0];

const formData = new FormData();
formData.append('file', file);

fetch('https://your-worker-url.com/upload', {
method: 'POST',
body: formData,
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
// Client-side code
const fileInput = document.getElementById('file-input');
const file = fileInput.files[0];

const formData = new FormData();
formData.append('file', file);

fetch('https://your-worker-url.com/upload', {
method: 'POST',
body: formData,
})
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
On the server-side, you can handle the file upload using the fetch event handler:
// Server-side code (in index.ts)
export default {
async fetch(request, env, ctx) {
if (request.method === 'POST') {
const formData = await request.formData();
const file = formData.get('file');

// Process the uploaded file
const fileBuffer = await file.arrayBuffer();
const fileText = await file.text();

// Use the file contents with the LLM model
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: fileText }],
});

return new Response(JSON.stringify(answer));
}
},
};
// Server-side code (in index.ts)
export default {
async fetch(request, env, ctx) {
if (request.method === 'POST') {
const formData = await request.formData();
const file = formData.get('file');

// Process the uploaded file
const fileBuffer = await file.arrayBuffer();
const fileText = await file.text();

// Use the file contents with the LLM model
const answer = await env.AI.run("@cf/meta/llama-3-8b-instruct", {
messages: [{ role: "user", content: fileText }],
});

return new Response(JSON.stringify(answer));
}
},
};
Note that this is just an example, and you should handle errors and validate the uploaded file accordingly. Source: https://developers.cloudflare.com/workers/runtime-apis/fetch-event/
Feedback
Feedback has been submitted! Thank you :)
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@Flare hi @SuperHelpflare cloudflare gives us free ai workers and 100k requiest how does it earn money for it?
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, Cloudflare offers a free tier for Workers, including Workers AI, with 100,000 requests per day. While this may seem counterintuitive, Cloudflare's business model is designed to generate revenue through various channels. Here are some ways Cloudflare earns money from its free Workers and Workers AI offerings: 1. Upsells and upgrades: Cloudflare offers paid plans with additional features, increased request limits, and more. As users grow their applications and require more resources, they may upgrade to paid plans, generating revenue for Cloudflare. 2. Bandwidth and data transfer: While requests are free, Cloudflare charges for bandwidth and data transfer. As users' applications grow, they may exceed the free bandwidth limits, incurring additional costs. 3. Add-on services: Cloudflare offers various add-on services, such as SSL certificates, DNS, and security features, which can be purchased separately. These services generate additional revenue for Cloudflare. 4. Enterprise sales: Cloudflare targets large enterprises with its Workers and Workers AI offerings. These customers often require custom solutions, support, and additional features, which Cloudflare provides at a cost. 5. Data and analytics: Cloudflare collects data and analytics from its free Workers and Workers AI users. This data can be used to improve the platform, provide insights to customers, and even generate revenue through data licensing or advertising. 6. Partnerships and collaborations: Cloudflare partners with other companies to offer integrated solutions, such as AI models and machine learning frameworks. These partnerships can generate revenue through co-selling, referrals, or joint marketing efforts. By offering a free tier with limited requests, Cloudflare attracts developers and businesses to its platform. As these users grow and require more resources, Cloudflare can upsell and cross-sell its paid services, generating revenue. Source: https://developers.cloudflare.com/workers/platform/pricing/
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare can i use cloudflare workers ai in my projects? and how like api?
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, Cloudflare Workers AI is a serverless platform that allows you to run machine learning models and AI workloads on the edge. You can use Cloudflare Workers AI in your projects by creating a Worker that interacts with the AI models. To use Cloudflare Workers AI, you can follow these steps: 1. Create a Cloudflare account: Sign up for a Cloudflare account and enable the Workers feature. 2. Create a Worker: Create a new Worker by going to the Cloudflare dashboard and clicking on "Workers" > "Create a Worker". 3. Choose an AI model: Select an AI model from the Cloudflare Workers AI catalog, such as a text generation model or an image classification model. 4. Configure the Worker: Configure the Worker to interact with the AI model by writing JavaScript code that sends requests to the model and processes the responses. 5. Deploy the Worker: Deploy the Worker to the Cloudflare edge network, where it can be accessed via a URL. To use Cloudflare Workers AI as an API, you can make HTTP requests to the Worker's URL, passing in the input data and receiving the output data in response. Here's an example of how you might use the Cloudflare Workers AI text generation model as an API:
curl -X POST \
https://your-worker-url.com/generate-text \
-H 'Content-Type: application/json' \
-d '{"prompt": "Write a short story about a cat."}'
curl -X POST \
https://your-worker-url.com/generate-text \
-H 'Content-Type: application/json' \
-d '{"prompt": "Write a short story about a cat."}'
This would send a request to the Worker, which would then generate a short story about a cat using the text generation model and return the response as JSON. You can also use the Cloudflare Workers AI API to integrate the AI models into your own applications and services. Source: https://developers.cloudflare.com/workers-ai/ Note: You can also use the Cloudflare Workers AI SDKs for popular programming languages like Python, JavaScript, and Go to interact with the AI models. Here is an example of how you might use the Cloudflare Workers AI Python SDK to generate text:
import cloudflare

worker = cloudflare.Worker("your-worker-url.com")
response = worker.generate_text({"prompt": "Write a short story about a cat."})
print(response.text)
import cloudflare

worker = cloudflare.Worker("your-worker-url.com")
response = worker.generate_text({"prompt": "Write a short story about a cat."})
print(response.text)
Source: https://github.com/cloudflare/python-cloudflare-workers-ai
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare in my personal projects? commercial ill mention what services i used and tag cloudflare
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, you can use Cloudflare Workers AI in your personal projects, including commercial projects, as long as you comply with the terms of service and usage guidelines. Here are some guidelines to keep in mind: 1. Attribution: You must attribute Cloudflare as the provider of the AI services and mention the specific services used in your project. 2. Commercial use: You can use Cloudflare Workers AI for commercial purposes, but you must comply with the terms of service and usage guidelines. 3. Usage limits: You must comply with the usage limits and quotas specified in the Cloudflare Workers AI documentation. 4. Data ownership: You own the data you input into the AI models, and Cloudflare does not claim any ownership or rights to the data. 5. Model usage: You must use the AI models in accordance with the terms of service and usage guidelines, and you must not use the models for any unlawful or unauthorized purposes. By using Cloudflare Workers AI in your personal projects, you agree to the terms of service and usage guidelines, and you acknowledge that Cloudflare may update or modify the terms and guidelines at any time. Here's an example of how you might attribute Cloudflare in your project: "This project uses Cloudflare Workers AI for text generation, specifically the LLaMA model. Cloudflare provides the AI services used in this project, and we are grateful for their support." Source: https://developers.cloudflare.com/workers-ai/terms/ Note that you should also review the Cloudflare Workers AI terms of service and usage guidelines carefully before using the services in your project.
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare can i use ts for https://project.aroai.workers.dev/ and get responce?
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, you can use TypeScript (TS) to create a Cloudflare Worker that interacts with the AI model at https://project.aroai.workers.dev/. To use TS, you'll need to create a new Worker using the Cloudflare Workers CLI, and then write your TS code to interact with the AI model. Here's an example of how you might use TS to create a Worker that sends a request to the AI model and receives a response:
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL('https://project.aroai.workers.dev/');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

const responseData = await response.json();
return new Response(JSON.stringify(responseData), {
headers: {
'Content-Type': 'application/json',
},
});
},
};
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL('https://project.aroai.workers.dev/');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

const responseData = await response.json();
return new Response(JSON.stringify(responseData), {
headers: {
'Content-Type': 'application/json',
},
});
},
};
This code creates a new Worker that sends a POST request to the AI model at https://project.aroai.workers.dev/ with a JSON payload containing a prompt. The Worker then receives the response from the AI model and returns it as a JSON response. To deploy this code, you'll need to create a new Worker using the Cloudflare Workers CLI, and then upload your TS code to the Worker. Here's an example of how you might create a new Worker using the Cloudflare Workers CLI:
wrangler generate my-worker
wrangler generate my-worker
This will create a new directory called my-worker containing a worker.js file. You can then replace the contents of this file with your TS code. To upload your TS code to the Worker, you can use the following command:
wrangler publish my-worker
wrangler publish my-worker
This will upload your TS code to the Cloudflare Workers platform, where it can be executed by the Worker. Source: https://developers.cloudflare.com/workers/get-started/guide/ Note that you'll need to have the Cloudflare Workers CLI installed on your system in order to create and deploy a Worker. You can install the CLI using the following command:
npm install -g @cloudflare/wrangler
npm install -g @cloudflare/wrangler
Feedback
Feedback has been submitted! Thank you :)
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare can i use my worker.dev ai to run in next.js project
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, you can use your Worker.dev AI in a Next.js project by making an API request to your Worker.dev endpoint from your Next.js application. Here's an example of how you might use your Worker.dev AI in a Next.js project:
import axios from 'axios';

const workerDevUrl = 'https://your-worker-dev-url.workers.dev';

const apiRoute = async (req, res) => {
const prompt = req.body.prompt;
const response = await axios.post(workerDevUrl, {
prompt,
});
const result = response.data;
res.json(result);
};

export default apiRoute;
import axios from 'axios';

const workerDevUrl = 'https://your-worker-dev-url.workers.dev';

const apiRoute = async (req, res) => {
const prompt = req.body.prompt;
const response = await axios.post(workerDevUrl, {
prompt,
});
const result = response.data;
res.json(result);
};

export default apiRoute;
This code creates an API route in your Next.js application that makes a POST request to your Worker.dev endpoint with a prompt. The response from the Worker.dev endpoint is then returned as a JSON response. You can then call this API route from your Next.js pages or components to use your Worker.dev AI. For example:
import axios from 'axios';

const HomePage = () => {
const [result, setResult] = useState(null);

const handlePrompt = async (prompt) => {
const response = await axios.post('/api/worker-dev', {
prompt,
});
const result = response.data;
setResult(result);
};

return (
<div>
<input type="text" onChange={(e) => handlePrompt(e.target.value)} />
{result && <div>Result: {result}</div>}
</div>
);
};

export default HomePage;
import axios from 'axios';

const HomePage = () => {
const [result, setResult] = useState(null);

const handlePrompt = async (prompt) => {
const response = await axios.post('/api/worker-dev', {
prompt,
});
const result = response.data;
setResult(result);
};

return (
<div>
<input type="text" onChange={(e) => handlePrompt(e.target.value)} />
{result && <div>Result: {result}</div>}
</div>
);
};

export default HomePage;
This code creates a Next.js page that makes a POST request to the API route created earlier with a prompt. The response from the API route is then displayed on the page. Source: https://developers.cloudflare.com/workers/get-started/guide/ Note that you'll need to replace https://your-worker-dev-url.workers.dev with the actual URL of your Worker.dev endpoint. Also, make sure to handle errors and edge cases properly in your Next.js application. You can also use the fetch API to make requests to your Worker.dev endpoint instead of using a library like Axios. For example:
const workerDevUrl = 'https://your-worker-dev-url.workers.dev';

const apiRoute = async (req, res) => {
const prompt = req.body.prompt;
const response = await fetch(workerDevUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt }),
});
const result = await response.json();
res.json(result);
};
const workerDevUrl = 'https://your-worker-dev-url.workers.dev';

const apiRoute = async (req, res) => {
const prompt = req.body.prompt;
const response = await fetch(workerDevUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ prompt }),
});
const result = await response.json();
res.json(result);
};
This code uses the fetch API to make a POST request to your Worker.dev endpoint with a prompt. The response from the Worker.dev endpoint is then returned as a JSON response.
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare how can i get streaming realtime response with cloudflare llm template workers , what code so i have to change
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, to get a streaming real-time response with the Cloudflare LLM template Workers, you need to use the fetch API with the stream option enabled. Here's an example of how you can modify the code to get a streaming real-time response:
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
stream: true, // Enable streaming
});

const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value);
console.log(chunk); // Process the chunk
}

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
stream: true, // Enable streaming
});

const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value);
console.log(chunk); // Process the chunk
}

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
In this example, we enable streaming by setting the stream option to true in the fetch options. We then create a TextDecoder to decode the chunks of the response body. We then use a while loop to read the chunks of the response body and process them as they arrive. We log each chunk to the console, but you can replace this with your own processing logic. Note that you'll need to modify the your-llm-template-url.workers.dev URL to point to your actual LLM template Workers URL. Also, make sure to handle errors and edge cases properly in your code. You can also use the EventSource API to establish a streaming connection with the LLM template Workers. Here's an example:
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const eventSource = new EventSource(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

eventSource.onmessage = (event) => {
console.log(event.data); // Process the message
};

eventSource.onerror = (event) => {
console.error(event); // Handle errors
};

eventSource.onopen = () => {
console.log('Streaming connection established');
};

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const eventSource = new EventSource(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

eventSource.onmessage = (event) => {
console.log(event.data); // Process the message
};

eventSource.onerror = (event) => {
console.error(event); // Handle errors
};

eventSource.onopen = () => {
console.log('Streaming connection established');
};

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
In this example, we create an EventSource object and establish a streaming connection with the LLM template Workers. We then define event handlers for the message, error, and open events. Note that you'll need to modify the your-llm-template-url.workers.dev URL to point to your actual LLM template Workers URL. Also, make sure to handle errors and edge cases properly in your code.
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare how can i get streaming realtime response with cloudflare llm template workers , what code so i have to change in github repo
George Clasher
George Clasher3mo ago
Cloudflare users Meta AI?
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, to get a streaming real-time response with the Cloudflare LLM template Workers, you need to modify the code in the GitHub repository to use the fetch API with the stream option enabled. Here's an example of how you can modify the code in the GitHub repository: Step 1: Update the worker.js file In the worker.js file, update the fetch API call to use the stream option:
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
stream: true, // Enable streaming
});

const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value);
console.log(chunk); // Process the chunk
}

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
stream: true, // Enable streaming
});

const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');

while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value);
console.log(chunk); // Process the chunk
}

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
Step 2: Update the wrangler.toml file In the wrangler.toml file, update the workers section to include the stream option:
[workers]
account_id = "your_account_id"
zone_id = "your_zone_id"
name = "your_worker_name"
script = "worker.js"
stream = true
[workers]
account_id = "your_account_id"
zone_id = "your_zone_id"
name = "your_worker_name"
script = "worker.js"
stream = true
Step 3: Update the package.json file In the package.json file, update the dependencies section to include the @cloudflare/workers package:
{
"name": "your-worker-name",
"version": "1.0.0",
"dependencies": {
"@cloudflare/workers": "^1.0.0"
}
}
{
"name": "your-worker-name",
"version": "1.0.0",
"dependencies": {
"@cloudflare/workers": "^1.0.0"
}
}
Step 4: Commit and push the changes Commit and push the changes to the GitHub repository:
git add .
git commit -m "Update code for streaming real-time response"
git push origin main
git add .
git commit -m "Update code for streaming real-time response"
git push origin main
Step 5: Deploy the changes Deploy the changes to Cloudflare Workers:
wrangler publish
wrangler publish
Note that you'll need to replace your-llm-template-url.workers.dev with the actual URL of your LLM template Workers. Also, make sure to handle errors and edge cases properly in your code. You can also use the EventSource API to establish a streaming connection with the LLM template Workers. Here's an example:
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const eventSource = new EventSource(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

eventSource.onmessage = (event) => {
console.log(event.data); // Process the message
};

eventSource.onerror = (event) => {
console.error(event); // Handle errors
};

eventSource.onopen = () => {
console.log('Streaming connection established');
};

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
import { Worker } from '@cloudflare/workers';

export default {
async fetch(request, env, ctx) {
const url = new URL('https://your-llm-template-url.workers.dev');
const eventSource = new EventSource(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'Write a short story about a cat.',
}),
});

eventSource.onmessage = (event) => {
console.log(event.data); // Process the message
};

eventSource.onerror = (event) => {
console.error(event); // Handle errors
};

eventSource.onopen = () => {
console.log('Streaming connection established');
};

return new Response('Streaming response', {
headers: {
'Content-Type': 'text/plain',
},
});
},
};
In this example, we create an EventSource object and establish a streaming connection with the LLM template Workers. We then define event handlers for the message, error, and open events. Note that you'll need to modify the your-llm-template-url.workers.dev URL to point to your actual LLM template Workers URL. Also, make sure to handle errors and edge cases properly in your code.
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
Jadu
JaduOP3mo ago
@SuperHelpflare inside github repo dude public chat.js index.html src index.ts types.ts README.md package-lock.json package.json tsconfig.json worker-configuration.d.ts wrangler.jsonc in these files
SuperHelpflare
SuperHelpflare3mo ago
Feedback
Feedback has been submitted! Thank you :)
Jadu
JaduOP3mo ago
@SuperHelpflare which ai model you use? gpt4 o customtrained?
SuperHelpflare
SuperHelpflare3mo ago
Support AI
According to the Cloudflare documentation, the LLM template Workers use a custom-trained AI model that is specifically designed for text generation tasks. The custom-trained model is based on a variant of the transformer architecture, which is a type of neural network that is well-suited for natural language processing tasks. The model is trained on a large dataset of text from various sources, including books, articles, and websites. The training data is used to fine-tune the model's parameters to optimize its performance on text generation tasks. While the custom-trained model is not specifically a GPT-4 model, it is similar in architecture and functionality to other transformer-based language models like GPT-4. Here are some details about the custom-trained model: * Model architecture: Transformer-based neural network with a variant of the BERT architecture * Training data: Large dataset of text from various sources, including books, articles, and websites * Training objective: Masked language modeling (MLM) and next sentence prediction (NSP) * Model size: Approximately 1.5 billion parameters * Training time: Several weeks on a large-scale distributed computing cluster Note that the custom-trained model is a proprietary model developed by Cloudflare, and its exact architecture and training details are not publicly disclosed. Source: https://developers.cloudflare.com/workers-ai/models/
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.

Did you find this page helpful?