Skip to content

Commit 3740f53

Browse files
committed
rag
1 parent 4b22f32 commit 3740f53

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

src/content/docs/agents/examples/browse-the-web.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ cd your-agent-project-folder
9191
npx wrangler@latest secret put BROWSERBASE_API_KEY
9292
```
9393
```sh output
94-
Enter a secret value: … ******
94+
Enter a secret value: ******
9595
Creating the secret for the Worker "agents-example"
9696
Success! Uploaded secret BROWSERBASE_API_KEY
9797
```

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

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,89 @@ sidebar:
88

99
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

11-
TODO
11+
Agents can use Retrieval Augmented Generation (RAG) to retrieve relevant information and use it augment [calls to AI models](/agents/examples/using-ai-models/). Store a user's chat history to use as context for future conversations, summarize documents to bootstrap an Agent's knowledge base, and/or use data from your Agent's [web browsing](/agents/examples/browse-the-web/) tasks to enhance your Agent's capabilities.
12+
13+
You can use the Agent's own [SQL database](/agents/examples/manage-and-sync-state) as the source of truth for your data and store embeddings in [Vectorize](/vectorize/) (or any other vector-enabled database) to allow your Agent to retrieve relevant information.
14+
15+
### Vector search
16+
17+
:::note
18+
19+
If you're brand-new to vector databases and Vectorize, visit the [Vectorize tutorial](/vectorize/get-started/intro/) to learn the basics, including how to create an index, insert data, and generate embeddings.
20+
21+
:::
22+
23+
You can query a vector index (or indexes) from any method on your Agent: any Vectorize index you attach is available on `this.env` within your Agent. If you've [associated metadata](/vectorize/best-practices/insert-vectors/#metadata) with your vectors that maps back to data stored in your Agent, you can then look up the data directly within your Agent using `this.sql`.
24+
25+
Here's an example of how to give an Agent retrieval capabilties:
26+
27+
<TypeScriptExample>
28+
29+
```ts
30+
import { Agent } from "agents-sdk";
31+
32+
interface Env {
33+
AI: Ai;
34+
VECTOR_DB: Vectorize;
35+
}
36+
37+
export class RAGAgent extends Agent<Env> {
38+
// Other methods on our Agent
39+
// ...
40+
//
41+
async queryKnowledge(userQuery: string) {
42+
// Turn a query into an embedding
43+
const queryVector = await this.env.AI.run('@cf/baai/bge-base-en-v1.5', {
44+
text: [userQuery],
45+
});
46+
47+
// Retrieve results from our vector index
48+
let searchResults = await this.env.VECTOR_DB.query(queryVector.data[0], {
49+
topK: 10,
50+
returnMetadata: 'all',
51+
});
52+
53+
let knowledge = [];
54+
for (const match of searchResults.matches) {
55+
console.log(match.metadata);
56+
knowledge.push(match.metadata);
57+
}
58+
59+
// Use the metadata to re-associate the vector search results
60+
// with data in our Agent's SQL database
61+
let results = this.sql`SELECT * FROM knowledge WHERE id IN (${knowledge.map((k) => k.id)})`;
62+
63+
// Return them
64+
return results;
65+
}
66+
}
67+
```
68+
69+
</TypeScriptExample>
70+
71+
You'll also need to connect your Agent to your vector indexes:
72+
73+
<WranglerConfig>
74+
75+
```jsonc
76+
{
77+
// ...
78+
"vectorize": [
79+
{
80+
"binding": "VECTOR_DB",
81+
"index_name": "your-vectorize-index-name"
82+
}
83+
]
84+
// ...
85+
}
86+
```
87+
88+
</WranglerConfig>
89+
90+
If you have multiple indexes you want to make available, you can provide an array of `vectorize` bindings.
91+
92+
#### Next steps
93+
94+
* Learn more on how to [combine Vectorize and Workers AI](/vectorize/get-started/embeddings/)
95+
* Review the [Vectorize query API](/vectorize/reference/client-api/)
96+
* Use [metadata filtering](/vectorize/reference/metadata-filtering/) to add context to your results

src/content/docs/agents/examples/run-workflows.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Agents do not have to run to completion: they can loop, branch and run forever,
2424

2525
An Agent can trigger one or more Workflows from within any method, whether from an incoming HTTP request, a WebSocket connection, on a delay or schedule, and/or from any other action the Agent takes.
2626

27-
Trigering a Workflow from an Agent is no different from [triggering a Workflow from a Worker script](/workflows/build/trigger-workflows/):
27+
Triggering a Workflow from an Agent is no different from [triggering a Workflow from a Worker script](/workflows/build/trigger-workflows/):
2828

2929
<TypeScriptExample>
3030

src/content/docs/agents/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Agents built with `agents-sdk` can be deployed directly to Cloudflare and run on
5858

5959
***
6060

61-
### Build on the Cloudflare Platform
61+
#### Build on the Cloudflare Platform
6262

6363
<RelatedProduct header="Workers" href="/workers/" product="workers">
6464

0 commit comments

Comments
 (0)