Skip to content

Commit 7366e17

Browse files
G4brymaninibread
andauthored
Add autorag recipes page (#21709)
* Add autorag recipes page * Update src/content/docs/autorag/usage/recipes.mdx Co-authored-by: Anni Wang <[email protected]> * Update src/content/docs/autorag/usage/recipes.mdx Co-authored-by: Anni Wang <[email protected]> * Add inline comments --------- Co-authored-by: Anni Wang <[email protected]>
1 parent 0dbf4b0 commit 7366e17

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

src/content/docs/autorag/configuration/indexing.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Factors that affect performance include:
3232

3333
To ensure smooth and reliable indexing:
3434

35-
- Make sure your files are within the size limit (10 MB) and in a supported format to avoid being skipped.
35+
- Make sure your files are within the [**size limit**](/autorag/platform/limits-pricing/#limits) and in a supported format to avoid being skipped.
3636
- Keep your Service API token valid to prevent indexing failures.
3737
- Regularly clean up outdated or unnecessary content in your knowledge base to avoid hitting [Vectorize index limits](/vectorize/platform/limits/).
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
pcx_content_type: concept
3+
title: Recipes
4+
sidebar:
5+
order: 5
6+
---
7+
8+
import {
9+
Badge,
10+
Description,
11+
Render,
12+
TabItem,
13+
Tabs,
14+
WranglerConfig,
15+
MetaInfo,
16+
Type,
17+
} from "~/components";
18+
19+
This section provides practical examples and recipes for common use cases. These examples are done using [Workers Binding](/autorag/usage/workers-binding/) but can be easely adapted to use the [REST API](/autorag/usage/rest-api/) instead.
20+
21+
## Bring your own model
22+
23+
You can use AutoRAG for search while leveraging a model outside of Workers AI to generate responses. Here is an example of how you can use an OpenAI model to generate your responses.
24+
25+
```ts
26+
import {openai} from '@ai-sdk/openai';
27+
import {generateText} from "ai";
28+
29+
export interface Env {
30+
AI: Ai;
31+
OPENAI_API_KEY: string;
32+
}
33+
34+
export default {
35+
async fetch(request, env): Promise<Response> {
36+
// Parse incoming url
37+
const url = new URL(request.url)
38+
39+
// Get the user query or default to a predefined one
40+
const userQuery = url.searchParams.get('query') ?? 'How do I train a llama to deliver coffee?'
41+
42+
// Search for documents in AutoRAG
43+
const searchResult = await env.AI.autorag('my-rag').search({query: userQuery})
44+
45+
if (searchResult.data.length === 0) {
46+
// No matching documents
47+
return Response.json({text: `No data found for query "${userQuery}"`})
48+
}
49+
50+
// Join all document chunks into a single string
51+
const chunks = searchResult.data.map((item) => {
52+
const data = item.content.map((content) => {
53+
return content.text
54+
}).join('\n\n')
55+
56+
return `<file name="${item.filename}">${data}</file>`
57+
}).join('\n\n')
58+
59+
// Send the user query + matched documents to openai for answer
60+
const generateResult = await generateText({
61+
model: openai("gpt-4o-mini"),
62+
messages: [
63+
{role: 'system', content: 'You are a helpful assistant and your task is to answer the user question using the provided files.'},
64+
{role: 'user', content: chunks},
65+
{role: 'user', content: userQuery},
66+
],
67+
});
68+
69+
// Return the generated answer
70+
return Response.json({text: generateResult.text});
71+
},
72+
} satisfies ExportedHandler<Env>;
73+
```
74+
75+
## Simple search engine
76+
77+
Using the `search` method you can implement a simple but fast search engine.
78+
79+
To replicate this example remember to:
80+
- Disable `rewrite_query` as you want to match the original user query
81+
- Configure your AutoRAG to have small chunk sizes, usually 256 tokens is enough
82+
83+
```ts
84+
export interface Env {
85+
AI: Ai;
86+
}
87+
88+
export default {
89+
async fetch(request, env): Promise<Response> {
90+
const url = new URL(request.url)
91+
const userQuery = url.searchParams.get('query') ?? 'How do I train a llama to deliver coffee?'
92+
const searchResult = await env.AI.autorag('my-rag').search({query: userQuery, rewrite_query: false})
93+
94+
return Response.json({
95+
files: searchResult.data.map((obj) => obj.filename)
96+
})
97+
},
98+
} satisfies ExportedHandler<Env>;
99+
```

src/content/products/autorag.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ product:
1010
meta:
1111
title: AutoRAG
1212
description: Create fully managed RAG pipelines for your AI applications.
13-
author: '@cloudflare'
13+
author: "@cloudflare"
1414

1515
resources:
16-
discord: https://discord.gg/cloudflaredev
16+
discord: https://discord.gg/cloudflaredev

0 commit comments

Comments
 (0)