Skip to content

Commit 1713f92

Browse files
committed
add examples WIP
1 parent 387c23f commit 1713f92

File tree

1 file changed

+200
-34
lines changed

1 file changed

+200
-34
lines changed

src/content/docs/agents/index.mdx

Lines changed: 200 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
Plan,
1919
RelatedProduct,
2020
Render,
21+
TabItem,
22+
Tabs,
2123
} from "~/components";
2224

2325
<Plan type="all" />
@@ -30,10 +32,11 @@ Agents are deployed to Cloudflare's [Workers](/workers/) platform using [Durable
3032
Get started
3133
</LinkButton>
3234

33-
## Why build agents on Cloudflare?
34-
- *Designed for durable execution:* [Durable Objects](/durable-objects/) and [Workflows](/workflows) are built for a programming model that enables guaranteed execution for async tasks like long-running deep thinking LLM calls, human-in-the-loop, or unreliable API calls.
35-
- *Non I/O bound pricing:* don't pay for long-running processes when your code is not executing. Cloudflare Workers is designed to scale down and [only charge you for CPU time](https://blog.cloudflare.com/workers-pricing-scale-to-zero/), as opposed to wall-clock time.
36-
- *Scalable, and reliable, without compromising on performance:* by running on Cloudflare's network, agents can execute tasks close to the user without introducing latency for real-time experiences.
35+
## Why build agents on Cloudflare?
36+
37+
- *Designed for durable execution:* [Durable Objects](/durable-objects/) and [Workflows](/workflows) are built for a programming model that enables guaranteed execution for async tasks like long-running deep thinking LLM calls, human-in-the-loop, or unreliable API calls.
38+
- *Non I/O bound pricing:* don't pay for long-running processes when your code is not executing. Cloudflare Workers is designed to scale down and [only charge you for CPU time](https://blog.cloudflare.com/workers-pricing-scale-to-zero/), as opposed to wall-clock time.
39+
- *Scalable, and reliable, without compromising on performance:* by running on Cloudflare's network, agents can execute tasks close to the user without introducing latency for real-time experiences.
3740

3841
## All the products you need in one platform
3942

@@ -48,48 +51,211 @@ Observe and control your AI applications with caching, rate limiting, request re
4851
Build full-stack AI applications with Vectorize, Cloudflare’s vector database. Adding Vectorize enables you to perform tasks such as semantic search, recommendations, anomaly detection or can be used to provide context and memory to an LLM.
4952

5053

51-
</RelatedProduct>
52-
53-
<RelatedProduct header="Workers" href="/workers/" product="workers">
54-
55-
Build serverless applications and deploy instantly across the globe for exceptional performance, reliability, and scale.
56-
57-
58-
</RelatedProduct>
59-
60-
<RelatedProduct header="Pages" href="/pages/" product="pages">
61-
62-
Create full-stack applications that are instantly deployed to the Cloudflare global network.
63-
54+
## Start Building
6455

65-
</RelatedProduct>
66-
67-
<RelatedProduct header="R2" href="/r2/" product="r2">
68-
69-
Store large amounts of unstructured data without the costly egress bandwidth fees associated with typical cloud storage services.
70-
71-
72-
</RelatedProduct>
56+
<Tabs syncKey="agents-products">
57+
<TabItem label="Workflows">
7358

74-
<RelatedProduct header="D1" href="/d1/" product="d1">
59+
</TabItem>
60+
<TabItem label="Durable Objects">
7561

76-
Create new serverless SQL databases to query from your Workers and Pages projects.
7762

63+
</TabItem>
64+
<TabItem label="Browser Rendering">
7865

79-
</RelatedProduct>
66+
</TabItem>
67+
<TabItem label="AI Gateway">
8068

81-
<RelatedProduct header="Durable Objects" href="/durable-objects/" product="durable-objects">
69+
</TabItem>
8270

83-
A globally distributed coordination API with strongly consistent storage.
71+
## Use your favorite AI framework
8472

73+
Build agents using your favorite AI frameworks, and deploy it directly to [Cloudflare Workers](/workers/).
8574

86-
</RelatedProduct>
75+
<Tabs syncKey="agents-frameworks">
76+
<TabItem label="LangChain">
8777

88-
<RelatedProduct header="KV" href="/kv/" product="kv">
78+
Use [LangChain](https://js.langchain.com/docs/integrations/text_embedding/cloudflare_ai/) to build Retrieval-Augmented Generation (RAG) applications using [Workers AI](/workers-ai/) and [Vectorize](/vectorize/).
8979

90-
Create a global, low-latency, key-value data storage.
80+
Give your agents more context and the ability to search across content, reply to user queries, and expand their domain knowledge.
9181

82+
```sh
83+
npm i @langchain/cloudflare hono
84+
```
9285

93-
</RelatedProduct>
86+
```ts
87+
import {
88+
CloudflareVectorizeStore,
89+
CloudflareWorkersAIEmbeddings
90+
} from "@langchain/cloudflare";
91+
import { VectorizeIndex } from "@cloudflare/workers-types";
92+
import { Ai } from "@cloudflare/ai";
93+
import { Hono } from "hono";
94+
95+
export interface Env {
96+
VECTORIZE_INDEX: VectorizeIndex;
97+
AI: Ai;
98+
}
99+
100+
const app = new Hono<{ Bindings: Env }>();
101+
102+
app.get("/", async (c) => {
103+
const embeddings = new CloudflareWorkersAIEmbeddings({
104+
binding: c.env.AI,
105+
model: "@cf/baai/bge-small-en-v1.5",
106+
});
107+
108+
const store = new CloudflareVectorizeStore(embeddings, {
109+
index: c.env.VECTORIZE_INDEX,
110+
});
111+
112+
const results = await store.similaritySearch("hello", 5);
113+
return c.json(results);
114+
});
115+
116+
app.post("/load", async (c) => {
117+
const embeddings = new CloudflareWorkersAIEmbeddings({
118+
binding: c.env.AI,
119+
model: "@cf/baai/bge-small-en-v1.5",
120+
});
121+
122+
const store = new CloudflareVectorizeStore(embeddings, {
123+
index: c.env.VECTORIZE_INDEX,
124+
});
125+
126+
const documents = [
127+
{ pageContent: "hello", metadata: {} },
128+
{ pageContent: "world", metadata: {} },
129+
{ pageContent: "hi", metadata: {} }
130+
];
131+
132+
await store.addDocuments(documents, {
133+
ids: ["id1", "id2", "id3"]
134+
});
135+
136+
return c.json({ success: true });
137+
});
138+
139+
app.delete("/clear", async (c) => {
140+
const embeddings = new CloudflareWorkersAIEmbeddings({
141+
binding: c.env.AI,
142+
model: "@cf/baai/bge-small-en-v1.5",
143+
});
144+
145+
const store = new CloudflareVectorizeStore(embeddings, {
146+
index: c.env.VECTORIZE_INDEX,
147+
});
148+
149+
await store.delete({ ids: ["id1", "id2", "id3"] });
150+
return c.json({ success: true });
151+
});
152+
153+
export default app;
154+
```
155+
156+
</TabItem>
157+
<TabItem label="AI SDK">
158+
159+
Ship faster with the [AI SDK](https://sdk.vercel.ai/docs/introduction): make it easier to generate text, tool call and/or get structured output from your AI models (and then deploy it [Workers](/workers/).
160+
161+
```sh
162+
npm i ai workers-ai-provider
163+
```
164+
165+
```ts
166+
import { createWorkersAI } from 'workers-ai-provider';
167+
import { streamText } from 'ai';
168+
169+
type Env = {
170+
AI: Ai;
171+
};
172+
173+
export default {
174+
async fetch(_: Request, env: Env) {
175+
const workersai = createWorkersAI({ binding: env.AI });
176+
const result = streamText({
177+
model: workersai('@cf/meta/llama-3.2-3b-instruct'),
178+
prompt: 'Write short essay on why you like Cloudflare Durable Objects.',
179+
});
180+
181+
return result.toTextStreamResponse({
182+
headers: {
183+
'Content-Type': 'text/x-unknown',
184+
'content-encoding': 'identity',
185+
'transfer-encoding': 'chunked',
186+
},
187+
});
188+
},
189+
};
190+
```
191+
192+
</TabItem>
193+
<TabItem label="OpenAI SDK">
194+
195+
Use any model provider with OpenAI compatible endpoints, including [ChatGPT](https://platform.openai.com/docs/quickstart), [DeepSeek](https://api-docs.deepseek.com/) and [Workers AI](/workers-ai/configuration/open-ai-compatibility/), directly from Cloudflare Workers.
196+
197+
```sh
198+
npm i openai
199+
```
200+
201+
```ts
202+
import OpenAI from "openai";
203+
204+
export interface Env {
205+
OPENAI_API_KEY: string;
206+
}
207+
208+
export default {
209+
async fetch(request: Request, env: Env) {
210+
const url = new URL(request.url);
211+
const prompt = url.searchParams.get('prompt') || "Make some robot noises";
212+
213+
const openai = new OpenAI({
214+
apiKey: env.OPENAI_API_KEY
215+
});
216+
217+
const chatCompletion = await openai.chat.completions.create({
218+
messages: [{ role: "user", content: prompt }],
219+
model: "gpt-3.5-turbo",
220+
});
221+
222+
const embeddings = await openai.embeddings.create({
223+
model: "text-embedding-ada-002",
224+
input: "Cloudflare Agents documentation",
225+
});
226+
227+
return new Response(JSON.stringify({ chatCompletion, embeddings }));
228+
}
229+
}
230+
```
231+
232+
</TabItem>
233+
<TabItem label="AI Gateway">
234+
235+
Use [AI Gateway](/ai-gateway/) to cache, log, retry and run [evals](/ai-gateway/evaluations/) (evaluations) for your agents, no matter where they're deployed.
236+
237+
```py
238+
from anthropic import Anthropic
239+
240+
anthropic = Anthropic(
241+
api_key="<your_anthropic_api_key>",
242+
# Route, cache, fallback and log prompt-response pairs between your app
243+
# and your AI model provider.
244+
base_url="https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/anthropic"
245+
)
246+
247+
message = anthropic.messages.create(
248+
model="claude-3-opus-20240229",
249+
max_tokens=1000,
250+
messages=[{
251+
"role": "user",
252+
"content": "Generate a Cloudflare Worker that returns a simple JSON payload based on a query param",
253+
}]
254+
)
255+
256+
print(message.content)
257+
```
258+
259+
</TabItem>
94260

95261
***

0 commit comments

Comments
 (0)