import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Cloudflare is a web performance and security company that provides content delivery network (CDN), DDoS protection, Internet security, and distributed domain name server services. Cloudflare launched Workers AI, which allows developers to run LLMs models powered by serverless GPUs on Cloudflare’s global network.
To learn more about Mistral models on Workers AI you can read the dedicated Cloudflare documentation page.
Set-up
To set-up Workers AI on Cloudflare, you need to create an account on the Cloudflare dashboard, get your account ID, and generate a token with Workers AI permissions. You can then send a completion request:
1curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/@cf/mistral/mistral-7b-instruct-v0.1 \
2 -X POST \
3 -H "Authorization: Bearer {API_TOKEN}" \
4 -d '{ "messages": [{ "role": "user", "content": "[INST] 2 + 2 ? [/INST]" }]}'
1async function run(model, prompt) {
2 const messages = [{ role: 'user', content: prompt }];
3
4 const response = await fetch(
5 `https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/${model}`,
6 {
7 headers: { Authorization: 'Bearer {API_TOKEN}' },
8 method: 'POST',
9 body: JSON.stringify({ messages }),
10 }
11 );
12 const result = await response.json();
13 return result;
14}
15
16run('@cf/mistral/mistral-7b-instruct-v0.1', '[INST] 2 + 2 ? [/INST]').then(
17 response => {
18 console.log(JSON.stringify(response));
19 }
20);
1import requests
2
3API_BASE_URL = "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/"
4headers = {"Authorization": "Bearer {API_TOKEN}"}
5
6def run(model, prompt):
7input = {
8"messages": [
9{ "role": "user", "content": prompt }
10]
11}
12response = requests.post(f"{API_BASE_URL}{model}", headers=headers, json=input)
13return response.json()
14
15output = run("@cf/mistral/mistral-7b-instruct-v0.1", "[INST] 2 + 2 = ? [/INST]")
16print(output)
17
Here is the output you should receive
1{'result': {'response': '2 + 2 = 4.'}, 'success': True, 'errors': [], 'messages': []}