首頁 > web前端 > js教程 > 主體

使用 LlamaIndex.ts 和 Azure OpenAI 建立 RAG 應用程式:入門!

WBOY
發布: 2024-09-10 18:30:30
原創
646 人瀏覽過

隨著人工智慧繼續塑造我們的工作方式以及與技術互動的方式,許多企業正在尋找在智慧應用程式中利用自己的數據的方法。如果你使用過 ChatGPT 或 Azure OpenAI 等工具,那麼你已經熟悉生成式 AI 如何改善流程並增強使用者體驗。然而,為了真正自訂和相關的回應,您的應用程式需要合併您的專有數據。

這就是檢索增強生成 (RAG) 的用武之地,它提供了一種結構化方法來將資料檢索與人工智慧驅動的回應整合在一起。借助 LlamaIndex 等框架,您可以輕鬆地將這種功能建置到您的解決方案中,從而釋放業務資料的全部潛力。

Building a RAG app with LlamaIndex.ts and Azure OpenAI: Getting started!

想要快速運行並探索該應用程式?點這裡。

什麼是 RAG - 檢索增強生成?

檢索增強生成 (RAG) 是一種神經網路框架,它透過包含檢索組件來存取相關資訊並整合您自己的資料來增強 AI 文字生成。它由兩個主要部分組成:

  • 檢索器:密集檢索器模型(例如,基於 BERT),用於搜尋大型文件語料庫以查找與給定查詢相關的相關段落或資訊。
  • 產生器:序列到序列模型(例如,基於 BART 或 T5),它將查詢和檢索到的文字作為輸入,並產生連貫的、上下文豐富的回應。

檢索器找到相關文檔,生成器使用它們來創建更準確、更豐富的回應。這種組合使 RAG 模型能夠有效地利用外部知識,並提高生成文本的品質和相關性。

LlamaIndex 如何實現 RAG?

要使用 LlamaIndex 實作 RAG 系統,請遵循以下一般步驟:

資料攝取:

  • 使用 SimpleDirectoryReader 等文件載入器將文件載入到 LlamaIndex.ts 中,這有助於從 PDF、API 或 SQL 資料庫等各種來源匯入資料。
  • 使用 SentenceSplitter 將大文件分解為更小的、可管理的區塊。

索引建立:

  • 使用 VectorStoreIndex 建立這些文件區塊的向量索引,從而允許基於嵌入進行高效的相似性搜尋。
  • 對於複雜的資料集,可以選擇使用遞歸檢索技術來管理分層結構化資料並根據使用者查詢檢索相關部分。

查詢引擎設定:

  • 使用asQueryEngine將向量索引轉換為查詢引擎,並使用similarityTopK等參數來定義應檢索多少個頂級文件。
  • 對於更高級的設置,創建一個多代理系統,其中每個代理負責特定文檔,並且頂級代理協調整個檢索過程。

檢索與產生:

  • 透過定義一個目標函數來實現 RAG 管道,該函數根據使用者查詢檢索相關文件區塊。
  • 使用 RetrieverQueryEngine 執行實際的檢索和查詢處理,以及可選的後處理步驟,例如使用 CohereRerank 等工具對檢索到的文件重新排名。

作為一個實際範例,我們提供了一個範例應用程式來示範使用 Azure OpenAI 的完整 RAG 實作。

實用 RAG 範例應用程式

我們現在將專注於使用 LlamaIndex.ts(LlamaIndex 的 TypeScipt 實作)和 Azure OpenAI 建置 RAG 應用程序,並將其部署為 Azure 容器應用程式上的無伺服器 Web 應用程式。

運行範例的要求

  • Azure Developer CLI (azd):一個命令列工具,可輕鬆部署整個應用程序,包括後端、前端和資料庫。
  • Azure 帳戶:您需要一個 Azure 帳戶來部署應用程式。取得一個帶有一些點數的免費 Azure 帳戶即可開始使用。

您將在 GitHub 上找到入門專案。我們建議您分叉此模板,以便您可以在需要時自由編輯它:

Building a RAG app with LlamaIndex.ts and Azure OpenAI: Getting started!

高層架構

入門專案應用程式基於以下架構建構:

  • Azure OpenAI: The AI provider that processes the user's queries.
  • LlamaIndex.ts: The framework that helps ingest, transform, and vectorize content (PDFs) and create a search index.
  • Azure Container Apps: The container environment where the serverless application is hosted.
  • Azure Managed Identity: Ensures top-notch security and eliminates the need for handling credentials and API keys.

Building a RAG app with LlamaIndex.ts and Azure OpenAI: Getting started!

For more details on what resources are deployed, check the infra folder available in all our samples.

Example User Workflows

The sample application contains logic for two workflows:

  1. Data Ingestion: Data is fetched, vectorized, and search indexes are created. If you want to add more files like PDFs or Word files, this is where you should add them.

      npm run generate
    
    登入後複製
  2. Serving Prompt Requests: The app receives user prompts, sends them to Azure OpenAI, and augments these prompts using the vector index as a retriever.

Running the Sample

Before running the sample, ensure you have provisioned the necessary Azure resources.

To run the GitHub template in GitHub Codespace, simply click
Building a RAG app with LlamaIndex.ts and Azure OpenAI: Getting started!

In your Codespaces instance, sign into your Azure account, from your terminal:

azd auth login
登入後複製

Provision, package, and deploy the sample application to Azure using a single command:

azd up
登入後複製

To run and try the application locally, install the npm dependencies and run the app:

npm install
npm run dev
登入後複製

The app will run on port 3000 in your Codespaces instance or at http://localhost:3000 in your browser.

Conclusion

This guide demonstrated how to build a serverless RAG (Retrieval-Augmented Generation) application using LlamaIndex.ts and Azure OpenAI, deployed on Microsoft Azure. By following this guide, you can leverage Azure's infrastructure and LlamaIndex's capabilities to create powerful AI applications that provide contextually enriched responses based on your data.

We’re excited to see what you build with this getting started application. Feel free to fork it and like the GitHub repository to receive the latest updates and features.

以上是使用 LlamaIndex.ts 和 Azure OpenAI 建立 RAG 應用程式:入門!的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!