大家好,我是 Ai 學習的老章
中簡單介紹了純互聯(lián)網(wǎng)的全托管 RAG 流水線,本文我們實操看看部署流程和效果。
AutoRAG
AutoRAG 是一個由 Cloudflare 提供的全托管檢索增強生成(RAG)流水線,旨在簡化開發(fā)者將上下文感知 AI 集成到其應(yīng)用程序中的方式。
RAG 是一種通過從您自己的數(shù)據(jù)中檢索信息并將其提供給大型語言模型(LLM)以生成更具體回答的方法。RAG 在查詢時從數(shù)據(jù)源檢索相關(guān)的信息,將其與用戶的查詢輸入結(jié)合起來,然后將兩者一起輸入到 LLM 中生成基于您數(shù)據(jù)的響應(yīng)。這使得 RAG 成為 AI 驅(qū)動的支持機器人、內(nèi)部知識助手、文檔中的語義搜索以及其他源為實真相不斷演化的應(yīng)用場景的理想選擇。
構(gòu)建 RAG 流水線需要拼湊多個組件——數(shù)據(jù)存儲、向量數(shù)據(jù)庫、嵌入模型、LLMs 以及自定義索引、檢索和生成邏輯等等工具和服務(wù)拼接在一起,才能開始。維護它更是難上加難,隨著數(shù)據(jù)的變化,須手動重新索引和重新生成嵌入,以使系統(tǒng)保持相關(guān)性和性能。
AutoRAG 要實現(xiàn)的就是將脆弱的膠水代碼管道、脆弱的集成和不斷的維護,變成一個簡單的“提問,獲得智能回答”的體驗。從導(dǎo)入數(shù)據(jù)、自動分塊和嵌入,到在 Cloudflare 的 Vectorize 數(shù)據(jù)庫中存儲向量,進行語義檢索,并使用 Workers AI 生成高質(zhì)量的響應(yīng)。AutoRAG 會持續(xù)監(jiān)控數(shù)據(jù)源和索引,使 AI 保持最新,無需手動操作。
AutoRAG Indexing process 索引過程
AutoRAG Querying process 查詢過程
Cloudflare 非常慷慨,目前 AutoRAG 完全免費,索引、檢索和增強的計算操作不會產(chǎn)生額外費用。每個賬戶的 Max AutoRAG 實例 10,最大文件數(shù) 10000 個。
R2、Vectorize Stores、Workers AI、AI Gateway 都可能產(chǎn)生費用,但是免費額度足夠個人測試使用了,比如我長期把 R2 當圖床,從沒產(chǎn)生過費用。Vectorize 也有每月 3000 萬的向量維度查詢。
具體可以看看:https://developers.cloudflare.com/autorag/platform/limits-pricing/
下面我們開始:
第一步,創(chuàng)建 R2 對象存儲
訪問https://dash.cloudflare.com/
點擊 R2 對象存儲,創(chuàng)建存儲桶
名稱:html-bucker
,點擊創(chuàng)建
第二步,創(chuàng)建 Worker 抓取網(wǎng)頁到 R2
本地創(chuàng)建一個空白文件夾
cd 過去后,創(chuàng)建一個名為browser-r2-worker
的 Worker 項目:
npm create cloudflare@latest -- browser-r2-worker
設(shè)置時,請選擇以下選項:
What would you like to start with? Choose Hello World Starter.
從哪里開始?選擇 Hello World 入門。Which template would you like to use? Choose Worker only.
使用哪個模板?選擇僅 Worker。Which language do you want to use? Choose TypeScript.
使用哪種語言?選擇 TypeScript。

安裝@cloudflare/puppeteer
,這允許你控制瀏覽器渲染實例:
npm i @cloudflare/puppeteer
將以下配置添加到您的 Wrangler 配置文件中,以便您的 Worker 可以使用瀏覽器渲染并使用新的 R2 存儲桶:
{ "compatibility_flags": ["nodejs_compat"], "browser": { "binding": "MY_BROWSER" }, "r2_buckets": [ { "binding": "HTML_BUCKET", "bucket_name": "html-bucket", } ], }
將 src/index.ts 的內(nèi)容替換為以下框架腳本:
import puppeteer from "@cloudflare/puppeteer"; // Define our environment bindings interface Env { MY_BROWSER: any; HTML_BUCKET: R2Bucket; } // Define request body structure interface RequestBody { url: string; } export default { async fetch(request: Request, env: Env): Promise { // Only accept POST requests if (request.method !== 'POST') { return new Response('Please send a POST request with a target URL', { status: 405 }); } // Get URL from request body const body = await request.json() as RequestBody; // Note: Only use this parser for websites you own const targetUrl = new URL(body.url); // Launch browser and create new page const browser = await puppeteer.launch(env.MY_BROWSER); const page = await browser.newPage(); // Navigate to the page and fetch its html await page.goto(targetUrl.href); const htmlPage = await page.content(); // Create filename and store in R2 const key = targetUrl.hostname + '_' + Date.now() + '.html'; await env.HTML_BUCKET.put(key, htmlPage); // Close browser await browser.close(); // Return success response return new Response(JSON.stringify({ success: true, message: 'Page rendered and stored successfully', key: key }), { headers: { 'Content-Type': 'application/json' } }); } } satisfies ExportedHandler ;

可以通過運行以下命令將其部署到 Cloudflare 賬戶中:
cd browser-r2-worker npx wrangler deploy

回到 Cloudflare 控制臺,可以看到 Worker 已經(jīng)創(chuàng)建好了
這個worker的核心功能是使用puppeteer訪問、閱讀網(wǎng)頁并將其保存為完整HTML到剛剛創(chuàng)建的 R2 存儲桶。但是我按照官方教程遇到 Couldn't connect to server 錯誤,暫未解決,所以后續(xù)用手動上傳文件來測試。
這一步本身也不夠傻瓜,后續(xù)開發(fā)成插件自動執(zhí)行會優(yōu)雅一點。
curl -X POST https://browser-r2-worker. .workers.dev \ -H "Content-Type: application/json" \ -d '{"url": "https://blog.cloudflare.com/introducing-autorag-on-cloudflare"}'
第二步,創(chuàng)建 AutoRAG 并監(jiān)控索引過程
在 Cloudflare 管理界面中,導(dǎo)航至 AI > AutoRAG
選擇創(chuàng)建 AutoRAG 并完成設(shè)置過程:
選擇包含知識庫的 R2 存儲桶,在這種情況下,選擇
html-bucket
。選擇用于將您的數(shù)據(jù)轉(zhuǎn)換為向量表示的嵌入模型。建議使用默認模型。
選擇一個 LLM 來生成你的響應(yīng)。建議使用默認項。
選擇或創(chuàng)建一個 AI 網(wǎng)關(guān)以監(jiān)控和控制您的模型使用情況。
將你的 AutoRAG 命名為
my-rag
。選擇或創(chuàng)建一個服務(wù) API 令牌以授予 AutoRAG 訪問權(quán)限以在你的賬戶中創(chuàng)建和訪問資源。
選擇“use”以啟動 AutoRAG。

測試一下:
未上傳任何文檔情況
上傳網(wǎng)頁或其他文件
再次測試
unsetunset第四步,測試并添加到應(yīng)用程序unsetunset
AutoRAG 提供了 REST API,可以基于 API 開發(fā)應(yīng)用:
curl https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/autorag/rags/{AUTORAG_NAME}/ai-search \-H 'Content-Type: application/json' \-H "Authorization: Bearer {API_TOKEN}" \-d '{ "query": "How do I train a llama to deliver coffee?", "model": @cf/meta/llama-3.3-70b-instruct-sd, "rewrite_query": true, "max_num_results": 10, "ranking_options": { "score_threshold": 0.6 }, "stream": true,}'
具體參考:https://developers.cloudflare.com/autorag/usage/rest-api/
unsetunset總結(jié)unsetunset
Cloudflare 提供的僅是 RAG 解決方案,而非完整 RAG 應(yīng)用。我覺得這一套流水線還是蠻方便流暢的,未來可以在網(wǎng)頁采集環(huán)節(jié)開發(fā)一個瀏覽器插件,一鍵采集網(wǎng)頁素材到自己的 R2 存儲桶,類似 Obsidian 的 clippings 插件。
制作不易,如果這篇文章覺得對你有用,可否點個關(guān)注。給我個三連擊:點贊、轉(zhuǎn)發(fā)和在看。若可以再給我加個,謝謝你看我的文章,我們下篇再見!
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務(wù)。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.