Skip to content
Cloudflare Docs

Retrieval Augmented Generation

Agents can use Retrieval Augmented Generation (RAG) to retrieve relevant information and use it augment calls to 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 tasks to enhance your Agent's capabilities.

You can use the Agent's own SQL database as the source of truth for your data and store embeddings in Vectorize (or any other vector-enabled database) to allow your Agent to retrieve relevant information.

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 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.

Here's an example of how to give an Agent retrieval capabilties:

import { Agent } from "agents-sdk";
export class RAGAgent extends Agent {
// Other methods on our Agent
// ...
//
async queryKnowledge(userQuery) {
// Turn a query into an embedding
const queryVector = await this.env.AI.run("@cf/baai/bge-base-en-v1.5", {
text: [userQuery],
});
// Retrieve results from our vector index
let searchResults = await this.env.VECTOR_DB.query(queryVector.data[0], {
topK: 10,
returnMetadata: "all",
});
let knowledge = [];
for (const match of searchResults.matches) {
console.log(match.metadata);
knowledge.push(match.metadata);
}
// Use the metadata to re-associate the vector search results
// with data in our Agent's SQL database
let results = this
.sql`SELECT * FROM knowledge WHERE id IN (${knowledge.map((k) => k.id)})`;
// Return them
return results;
}
}

You'll also need to connect your Agent to your vector indexes:

{
// ...
"vectorize": [
{
"binding": "VECTOR_DB",
"index_name": "your-vectorize-index-name"
}
]
// ...
}

If you have multiple indexes you want to make available, you can provide an array of vectorize bindings.

Next steps