搜尋
首頁後端開發Python教學使用 OpenAI 和 Streamlit 建立文件檢索和問答系統

您好,開發社群! ?

今天,我很高興向您介紹我的專案:EzioDevIo RAG(檢索增強生成)。該系統允許用戶上傳 PDF 文檔,根據其內容提出問題,並接收由 OpenAI 的 GPT-3.5 Turbo 模型生成的即時答案。這對於導航大型文件或快速提取相關資訊特別有用。 ??

Building a Document Retrieval & Q&A System with OpenAI and Streamlit

您可以在我的 GitHub 上找到完整的程式碼:EzioDevIo RAG 專案。讓我們深入研究該項目並分解每個步驟!

?深入了解 EzioDevIo RAG 專案 GitHub 儲存庫中的完整程式碼庫和設定說明!

專案概覽

你將學到什麼

  1. 如何將 OpenAI 的語言模型與 PDF 文件檢索整合。
  2. 如何使用 Streamlit 建立使用者友善的介面。
  3. 如何使用 Docker 將應用程式容器化以方便部署。 專案特色
  • 上傳 PDF 並從中獲取資訊。
  • 根據上傳的PDF內容提問。
  • OpenAI 的 gpt-3.5-turbo 模型產生的即時響應。
  • 使用 Docker 輕鬆部署以實現可擴充性。

*這是我們專案目錄的最終結構:*

RAG-project/
├── .env                       # Environment variables (API key)
├── app.py                     # Streamlit app for the RAG system
├── document_loader.py         # Code for loading and processing PDF documents
├── retriever.py               # Code for indexing and retrieving documents
├── main.py                    # Main script for RAG pipeline
├── requirements.txt           # List of required libraries
├── Dockerfile                 # Dockerfile for containerizing the app
├── .gitignore                 # Ignore sensitive and unnecessary files
├── data/
│   └── uploaded_pdfs/         # Folder to store uploaded PDFs
└── images/
    └── openai_api_setup.png   # Example image for OpenAI API setup instructions

第 1 步:設定項目

先決條件

確保您擁有以下內容:

  • Python 3.8:本地運行應用程式。
  • OpenAI API 金鑰:您需要它來存取 OpenAI 的模型。在 OpenAI API 上註冊以取得您的金鑰。
  • Docker:可選,但建議將應用程式容器化以進行部署。

第 2 步:複製儲存庫並設定虛擬環境

2.1。克隆儲存庫
首先從 GitHub 克隆專案儲存庫並導航到專案目錄。

git clone https://github.com/EzioDEVio/RAG-project.git
cd RAG-project

2.2。設定虛擬環境
若要隔離專案依賴性,請建立並啟動虛擬環境。這有助於防止與其他項目的套件發生衝突。

python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`

2.3。安裝依賴項
安裝requirements.txt中列出的所需Python庫。其中包括用於語言模型的 OpenAI、用於 UI 的 Streamlit、用於 PDF 處理的 PyMuPDF 以及用於高效相似性搜尋的 FAISS。

pip install -r requirements.txt

2.4。設定您的 OpenAI API 金鑰
在專案根目錄下建立.env檔。該文件將安全地儲存您的 OpenAI API 金鑰。將以下行加入文件中,將 your_openai_api_key_here 替換為您的實際 API 金鑰:

RAG-project/
├── .env                       # Environment variables (API key)
├── app.py                     # Streamlit app for the RAG system
├── document_loader.py         # Code for loading and processing PDF documents
├── retriever.py               # Code for indexing and retrieving documents
├── main.py                    # Main script for RAG pipeline
├── requirements.txt           # List of required libraries
├── Dockerfile                 # Dockerfile for containerizing the app
├── .gitignore                 # Ignore sensitive and unnecessary files
├── data/
│   └── uploaded_pdfs/         # Folder to store uploaded PDFs
└── images/
    └── openai_api_setup.png   # Example image for OpenAI API setup instructions

?提示:確保將 .env 新增至您的 .gitignore 檔案中,以避免在將專案推送到公用儲存庫時暴露您的 API 金鑰。

第 3 步:了解專案結構
以下是目錄結構的快速概述,可協助您瀏覽程式碼:
以下是目錄結構的快速概述,可協助您瀏覽程式碼:

git clone https://github.com/EzioDEVio/RAG-project.git
cd RAG-project

每個檔案都有特定的功能:

  • app.py:管理 Streamlit 介面,允許使用者上傳檔案和提問。
  • document_loader.py:使用 PyMuPDF 處理 PDF 的載入和處理。
  • retriever.py:使用 FAISS 索引文件文字並根據使用者查詢檢索相關部分。
  • main.py:將所有內容連結在一起,包括呼叫 OpenAI 的 API 來產生回應。

第 4 步:建立核心程式碼
現在,讓我們深入了解該專案的主要組成部分。

4.1。載入文件 (document_loader.py)
document_loader.py 檔案負責從 PDF 中提取文字。在這裡,我們使用 PyMuPDF 庫來處理 PDF 中的每個頁面並儲存文字。

python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`

說明:函數讀取指定資料夾中的所有PDF文件,提取每個頁面的文本,並將文本添加到字典列表中。每個字典代表一個文檔及其文字和檔案名稱。

4.2。文件索引與檢索 (retriever.py)
FAISS(Facebook AI相似性搜尋)幫助我們執行相似性搜尋。我們用它來建立文件嵌入的索引,這使我們能夠在使用者提出問題時檢索相關部分。

pip install -r requirements.txt

說明:

create_index:使用 OpenAIEmbeddings 將文件文字轉換為嵌入,並使用 FAISS 建立索引。
retrieve_documents:根據使用者查詢搜尋相關文件部分。

4.3。產生回應 (main.py)
此模組處理使用者查詢、檢索相關文件並使用 OpenAI 的語言模型產生答案。

OPENAI_API_KEY=your_openai_api_key_here

說明:

generate_response:使用檢索到的文件和使用者查詢的上下文建立提示,然後將其傳送到 OpenAI 的 API。然後將響應作為答案返回。

第 5 步:建立 Streamlit 介面 (app.py)
Streamlit 提供了一個互動式前端,使用戶可以輕鬆上傳文件和提問。

RAG-project/
├── .env                       # Environment variables (API key)
├── app.py                     # Streamlit app for the RAG system
├── document_loader.py         # Code for loading and processing PDF documents
├── retriever.py               # Code for indexing and retrieving documents
├── main.py                    # Main script for RAG pipeline
├── requirements.txt           # List of required libraries
├── Dockerfile                 # Dockerfile for containerizing the app
├── .gitignore                 # Ignore sensitive and unnecessary files
├── data/
│   └── uploaded_pdfs/         # Folder to store uploaded PDFs
└── images/
    └── openai_api_setup.png   # Example image for OpenAI API setup instructions

說明:

  • 此程式碼使用 Streamlit 建立一個簡單的 UI,允許使用者上傳 PDF 並輸入問題。
  • 當使用者點擊「取得答案」時,應用程式會檢索相關文件並產生答案。

第 6 步:對應用程式進行 Docker 化
Docker 可讓您將應用程式打包到容器中,從而輕鬆部署。

Dockerfile

RAG-project/
├── .env                       # Environment variables (API key)
├── app.py                     # Streamlit app for the RAG system
├── document_loader.py         # Code for loading and processing PDF documents
├── retriever.py               # Code for indexing and retrieving documents
├── main.py                    # Main script for RAG pipeline
├── requirements.txt           # List of required libraries
├── Dockerfile                 # Dockerfile for containerizing the app
├── .gitignore                 # Ignore sensitive and unnecessary files
├── data/
│   └── uploaded_pdfs/         # Folder to store uploaded PDFs
└── images/
    └── openai_api_setup.png   # Example image for OpenAI API setup instructions

說明:

我們使用多階段構建來保持最終圖像的精簡。
為了安全起見,應用程式以非 root 使用者身分執行。

運行 Docker 容器

  1. 建置 Docker 映像:
git clone https://github.com/EzioDEVio/RAG-project.git
cd RAG-project

  1. 運行容器:
python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`

第 7 步:使用 GitHub Actions 設定 CI/CD
為了做好生產準備,請新增 CI/CD 管道來建置、測試和掃描 Docker 映像。您可以在儲存庫中找到此設定的 .github/workflows 檔案。

最後的想法
該專案將 OpenAI 的語言模型功能與文件檢索結合,創建一個功能性的互動式工具。如果您喜歡這個項目,請在 GitHub 儲存庫上加註星標,並在開發社群上關注我。讓我們一起打造更多精彩的項目! ?

?查看 GitHub 儲存庫? EzioDevIo RAG 專案 GitHub 儲存庫!

以上是使用 OpenAI 和 Streamlit 建立文件檢索和問答系統的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具