Skip to content

Commit 731d6bb

Browse files
committed
guides, ai models
1 parent 68928f6 commit 731d6bb

File tree

5 files changed

+83
-25
lines changed

5 files changed

+83
-25
lines changed

src/content/docs/agents/examples/using-ai-models.mdx

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66

77
---
88

9-
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
9+
import { AnchorHeading, MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

1111
Agents can communicate with AI models hosted on any provider, including [Workers AI](/workers-ai/), OpenAI, Anthropic, and Google's Gemini, and use the model routing features in [AI Gateway](/ai-gateway/) to route across providers, eval responses, and manage AI provider rate limits.
1212

@@ -16,9 +16,69 @@ A user can disconnect during a long-running response from a modern reasoning mod
1616

1717
## Calling AI Models
1818

19+
You can call models from any method within an Agent, including from HTTP requests using the [`onRequest`](/agents/api-reference/sdk/) handler, when a [scheduled task](/agents/examples/schedule-tasks/) runs, when handling a WebSocket message in the [`onMessage`](/agents/examples/websockets/) handler, or from any of your own methods.
20+
21+
Importantly, Agents can call AI models on their own — autonomously — and can handle long-running responses that can take minutes (or longer) to respond in full.
22+
23+
### Long-running model requests {/*long-running-model-requests*/}
24+
25+
Modern [reasoning models](https://platform.openai.com/docs/guides/reasoning) or "thinking" model can take some time to both generate a response _and_ stream the response back to the client.
26+
27+
Instead of buffering the entire response, or risking the client disconecting, you can stream the response back to the client by using the [WebSocket API](/agents/examples/websockets/).
28+
29+
<TypeScriptExample file="src/index.ts">
30+
31+
```ts
32+
import { Agent } from "@cloudflare/agents"
33+
import { OpenAI } from "openai"
34+
35+
export class MyAgent extends Agent<Env> {
36+
async onConnect(connection: Connection, ctx: ConnectionContext) {
37+
// Omitted for simplicity: authenticating the user
38+
connection.accept()
39+
}
40+
41+
async onMessage(connection: Connection, message: WSMessage) {
42+
let msg = JSON.parse(message)
43+
// This can run as long as it needs to, and return as many messages as it needs to!
44+
await queryReasoningModel(connection, msg.prompt)
45+
}
46+
47+
async queryReasoningModel(connection: Connection, userPrompt: string) {
48+
const client = new OpenAI({
49+
apiKey: this.env.OPENAI_API_KEY,
50+
});
51+
52+
try {
53+
const stream = await client.chat.completions.create({
54+
model: this.env.MODEL || 'o3-mini',
55+
messages: [{ role: 'user', content: userPrompt }],
56+
stream: true,
57+
});
58+
59+
// Stream responses back as WebSocket messages
60+
for await (const chunk of stream) {
61+
const content = chunk.choices[0]?.delta?.content || '';
62+
if (content) {
63+
connection.send(JSON.stringify({ type: 'chunk', content }));
64+
}
65+
}
66+
67+
// Send completion message
68+
connection.send(JSON.stringify({ type: 'done' }));
69+
} catch (error) {
70+
connection.send(JSON.stringify({ type: 'error', error: error }));
71+
}
72+
}
73+
```
74+
75+
</TypeScriptExample>
76+
77+
You can also persist AI model responses back to [Agent's internal state](/agents/examples/manage-and-sync-state/) by using the `this.setState` method. For example, if you run a [scheduled task](/agents/examples/scheduling-tasks/), you can store the output of the task and read it later. Or, if a user disconnects, read the message history back and send it to the user when they reconnect.
78+
1979
### Workers AI
2080
21-
### Inference endpoints
81+
### Hosted models
2282
2383
You can use [any of the models available in Workers AI](/workers-ai/models/) within your Agent by [configuring a binding](/workers-ai/configuration/bindings/).
2484
@@ -63,7 +123,6 @@ binding = "AI"
63123
64124
</WranglerConfig>
65125
66-
67126
### Model routing
68127
69128
You can also use the model routing features in [AI Gateway](/ai-gateway/) directly from an Agent by specifying a [`gateway` configuration](/ai-gateway/providers/workersai/) when calling the AI binding.
@@ -149,11 +208,11 @@ export class MyAgent extends Agent<Env> {
149208
150209
</TypeScriptExample>
151210
152-
### OpenAI SDK
211+
### OpenAI compatible endpoints
153212
154213
Agents can call models across any service, including those that support the OpenAI API. For example, you can use the OpenAI SDK to use one of [Google's Gemini models](https://ai.google.dev/gemini-api/docs/openai#node.js) directly from your Agent.
155214
156-
Agents can stream responses back over HTTP using Server Sent Events (SSE) from within an `onRequest` handler, or by using the native [WebSockets](/agents/examples/websockets/) API to responses back to a a client over a long running WebSocket.
215+
Agents can stream responses back over HTTP using Server Sent Events (SSE) from within an `onRequest` handler, or by using the native [WebSockets](/agents/examples/websockets/) API in your Agent to responses back to a client, which is especially useful for larger models that can take over 30+ seconds to reply.
157216
158217
<TypeScriptExample file="src/index.ts">
159218

src/content/docs/agents/examples/websockets.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Real-time and WebSockets
2+
title: Using WebSockets
33
pcx_content_type: concept
44
sidebar:
55
order: 2

src/content/docs/agents/getting-started/observing-agents.mdx

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Build a Human-in-the-loop Agent
4+
external_link: https://github.com/cloudflare/agents/tree/main/guides/human-in-the-loop
5+
sidebar:
6+
order: 2
7+
head: []
8+
description: Implement human-in-the-loop functionality using Cloudflare Agents, allowing AI agents to request human approval before executing certain actions
9+
---
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
pcx_content_type: navigation
3+
title: Implement Effective Agent Patterns
4+
external_link: https://github.com/cloudflare/agents/tree/main/guides/anthropic-patterns
5+
sidebar:
6+
order: 3
7+
head: []
8+
description: Implement common agent patterns using the `@cloudflare/agents` framework.
9+
---

0 commit comments

Comments
 (0)