-
Notifications
You must be signed in to change notification settings - Fork 10k
Add autorag recipes page #21709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
aninibread
merged 4 commits into
cloudflare:production
from
G4brym:gmassadas/add-autorag.recipes
Apr 16, 2025
Merged
Add autorag recipes page #21709
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| --- | ||
| pcx_content_type: concept | ||
| title: Recipes | ||
| sidebar: | ||
| order: 5 | ||
| --- | ||
|
|
||
| import { | ||
| Badge, | ||
| Description, | ||
| Render, | ||
| TabItem, | ||
| Tabs, | ||
| WranglerConfig, | ||
| MetaInfo, | ||
| Type, | ||
| } from "~/components"; | ||
|
|
||
| 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. | ||
|
|
||
| ## Bring your own model | ||
|
|
||
| This scenario allows you to leverage AutoRAG for chunk search, while asking a different model to answer the user question. | ||
|
|
||
| ```ts | ||
| import {openai} from '@ai-sdk/openai'; | ||
| import {generateText} from "ai"; | ||
|
|
||
| export interface Env { | ||
| AI: Ai; | ||
| OPENAI_API_KEY: string; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env): Promise<Response> { | ||
| const url = new URL(request.url) | ||
G4brym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const userQuery = url.searchParams.get('query') ?? 'How do I train a llama to deliver coffee?' | ||
| const searchResult = await env.AI.autorag('my-rag').search({query: userQuery}) | ||
|
|
||
| if (searchResult.data.length === 0) { | ||
| return Response.json({text: `No data found for query "${userQuery}"`}) | ||
| } | ||
|
|
||
| const chunks = searchResult.data.map((item) => { | ||
| const data = item.content.map((content) => { | ||
| return content.text | ||
| }).join('\n\n') | ||
|
|
||
| return `<file name="${item.filename}">${data}</file>` | ||
| }).join('\n\n') | ||
|
|
||
| const generateResult = await generateText({ | ||
| model: openai("gpt-4o-mini"), | ||
| messages: [ | ||
| {role: 'system', content: 'You are a helpful assistant and your task is to answer the user question using the provided files.'}, | ||
| {role: 'user', content: chunks}, | ||
| {role: 'user', content: userQuery}, | ||
| ], | ||
| }); | ||
|
|
||
| return Response.json({text: generateResult.text}); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| ## Simple search engine | ||
|
|
||
| Using the `search` method you can a simple but fast search engine. | ||
G4brym marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| To replicate this example remember to: | ||
| - Disable `rewrite_query` as you want to match the original user query | ||
| - Configure your AutoRAG to have small chunk sizes, usually 256 tokens is enough | ||
|
|
||
| ```ts | ||
| export interface Env { | ||
| AI: Ai; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env): Promise<Response> { | ||
| const url = new URL(request.url) | ||
| const userQuery = url.searchParams.get('query') ?? 'How do I train a llama to deliver coffee?' | ||
| const searchResult = await env.AI.autorag('my-rag').search({query: userQuery, rewrite_query: false}) | ||
|
|
||
| return Response.json({ | ||
| files: searchResult.data.map((obj) => obj.filename) | ||
| }) | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.