【LangChain 數據連接封裝】 文檔加載器、文檔處理器

在這里插入圖片描述
小結:

  1. 文檔處理部分,建議在實際應用中詳細測試后使用
  2. 與向量數據庫的鏈接部分本質是接口封裝,向量數據庫需要自己選型
  3. 類似 LlamaIndex,LangChain 也提供了豐富的 Document Loaders DocumentLoaders和 Text Splitters Text Splitters

1. 文檔加載器 Document Loaders

#!pip install pymupdf
from langchain_community.document_loaders import PyMuPDFLoaderloader = PyMuPDFLoader("llama2.pdf")
pages = loader.load_and_split()print(pages[0].page_content)打印輸出:
Llama 2: Open Foundation and Fine-Tuned Chat Models
Hugo Touvron?
Louis Martin?
Kevin Stone?
Peter Albert Amjad Almahairi Yasmine Babaei Nikolay Bashlykov Soumya Batra
Prajjwal Bhargava Shruti Bhosale Dan Bikel Lukas Blecher Cristian Canton Ferrer Moya Chen
Guillem Cucurull David Esiobu Jude Fernandes Jeremy Fu Wenyin Fu Brian Fuller
Cynthia Gao Vedanuj Goswami Naman Goyal Anthony Hartshorn Saghar Hosseini Rui Hou
Hakan Inan Marcin Kardas Viktor Kerkez Madian Khabsa Isabel Kloumann Artem Korenev
Punit Singh Koura Marie-Anne Lachaux Thibaut Lavril Jenya Lee Diana Liskovich
Yinghai Lu Yuning Mao Xavier Martinet Todor Mihaylov Pushkar Mishra
Igor Molybog Yixin Nie Andrew Poulton Jeremy Reizenstein Rashi Rungta Kalyan Saladi
Alan Schelten Ruan Silva Eric Michael Smith Ranjan Subramanian Xiaoqing Ellen Tan Binh Tang
Ross Taylor Adina Williams Jian Xiang Kuan Puxin Xu Zheng Yan Iliyan Zarov Yuchen Zhang
Angela Fan Melanie Kambadur Sharan Narang Aurelien Rodriguez Robert Stojnic
Sergey Edunov
Thomas Scialom?
GenAI, Meta
Abstract
In this work, we develop and release Llama 2, a collection of pretrained and ?ne-tuned
large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.
Our ?ne-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases. Our
models outperform open-source chat models on most benchmarks we tested, and based on
our human evaluations for helpfulness and safety, may be a suitable substitute for closed-
source models. We provide a detailed description of our approach to ?ne-tuning and safety
improvements of Llama 2-Chat in order to enable the community to build on our work and
contribute to the responsible development of LLMs.
?Equal contribution, corresponding authors: {tscialom, htouvron}@meta.com
?Second author
Contributions for all the authors can be found in Section A.1.
arXiv:2307.09288v2  [cs.CL]  19 Jul 2023

2. 文檔處理器 TextSplitter

#!pip install --upgrade langchain-text-splittersfrom langchain_text_splitters import RecursiveCharacterTextSplittertext_splitter = RecursiveCharacterTextSplitter(chunk_size=200,chunk_overlap=100, length_function=len,add_start_index=True,
)paragraphs = text_splitter.create_documents([pages[0].page_content])
for para in paragraphs:print(para.page_content)print('-------')打印輸出:
Llama 2: Open Foundation and Fine-Tuned Chat Models
Hugo Touvron?
Louis Martin?
Kevin Stone?
Peter Albert Amjad Almahairi Yasmine Babaei Nikolay Bashlykov Soumya Batra
-------
Kevin Stone?
Peter Albert Amjad Almahairi Yasmine Babaei Nikolay Bashlykov Soumya Batra
Prajjwal Bhargava Shruti Bhosale Dan Bikel Lukas Blecher Cristian Canton Ferrer Moya Chen
-------
Prajjwal Bhargava Shruti Bhosale Dan Bikel Lukas Blecher Cristian Canton Ferrer Moya Chen
Guillem Cucurull David Esiobu Jude Fernandes Jeremy Fu Wenyin Fu Brian Fuller
-------
Guillem Cucurull David Esiobu Jude Fernandes Jeremy Fu Wenyin Fu Brian Fuller
Cynthia Gao Vedanuj Goswami Naman Goyal Anthony Hartshorn Saghar Hosseini Rui Hou
-------
Cynthia Gao Vedanuj Goswami Naman Goyal Anthony Hartshorn Saghar Hosseini Rui Hou
Hakan Inan Marcin Kardas Viktor Kerkez Madian Khabsa Isabel Kloumann Artem Korenev
-------
Hakan Inan Marcin Kardas Viktor Kerkez Madian Khabsa Isabel Kloumann Artem Korenev
Punit Singh Koura Marie-Anne Lachaux Thibaut Lavril Jenya Lee Diana Liskovich
-------
Punit Singh Koura Marie-Anne Lachaux Thibaut Lavril Jenya Lee Diana Liskovich
Yinghai Lu Yuning Mao Xavier Martinet Todor Mihaylov Pushkar Mishra
-------
Yinghai Lu Yuning Mao Xavier Martinet Todor Mihaylov Pushkar Mishra
Igor Molybog Yixin Nie Andrew Poulton Jeremy Reizenstein Rashi Rungta Kalyan Saladi
-------
Igor Molybog Yixin Nie Andrew Poulton Jeremy Reizenstein Rashi Rungta Kalyan Saladi
Alan Schelten Ruan Silva Eric Michael Smith Ranjan Subramanian Xiaoqing Ellen Tan Binh Tang
-------
Alan Schelten Ruan Silva Eric Michael Smith Ranjan Subramanian Xiaoqing Ellen Tan Binh Tang
Ross Taylor Adina Williams Jian Xiang Kuan Puxin Xu Zheng Yan Iliyan Zarov Yuchen Zhang
-------
Ross Taylor Adina Williams Jian Xiang Kuan Puxin Xu Zheng Yan Iliyan Zarov Yuchen Zhang
Angela Fan Melanie Kambadur Sharan Narang Aurelien Rodriguez Robert Stojnic
Sergey Edunov
Thomas Scialom?
-------
Sergey Edunov
Thomas Scialom?
GenAI, Meta
Abstract
In this work, we develop and release Llama 2, a collection of pretrained and ?ne-tuned
-------
Abstract
In this work, we develop and release Llama 2, a collection of pretrained and ?ne-tuned
large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.
-------
large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.
Our ?ne-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases. Our
-------
Our ?ne-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases. Our
models outperform open-source chat models on most benchmarks we tested, and based on
-------
models outperform open-source chat models on most benchmarks we tested, and based on
our human evaluations for helpfulness and safety, may be a suitable substitute for closed-
-------
our human evaluations for helpfulness and safety, may be a suitable substitute for closed-
source models. We provide a detailed description of our approach to ?ne-tuning and safety
-------
source models. We provide a detailed description of our approach to ?ne-tuning and safety
improvements of Llama 2-Chat in order to enable the community to build on our work and
-------
improvements of Llama 2-Chat in order to enable the community to build on our work and
contribute to the responsible development of LLMs.
-------
contribute to the responsible development of LLMs.
?Equal contribution, corresponding authors: {tscialom, htouvron}@meta.com
?Second author
-------
?Equal contribution, corresponding authors: {tscialom, htouvron}@meta.com
?Second author
Contributions for all the authors can be found in Section A.1.
arXiv:2307.09288v2  [cs.CL]  19 Jul 2023
-------

3.向量數據庫與向量檢索

from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import PyMuPDFLoader# 加載文檔
loader = PyMuPDFLoader("llama2.pdf")
pages = loader.load_and_split()# 文檔切分# 每個文本塊的最大字符數#  chunk_overlap=100,         # 塊之間的重疊字符數#  length_function=len,       # 使用Python內置len計算長度#  add_start_index=True,      # 保留原始文檔中的起始位置信息
text_splitter = RecursiveCharacterTextSplitter(chunk_size=300,chunk_overlap=100,length_function=len,add_start_index=True,
)texts = text_splitter.create_documents([page.page_content for page in pages[:4]]
)# 灌庫
# 將文本轉換為1536維的向量(ada模型維度)
# FAISS會建立索引結構加速相似度搜索
# 生成的db對象包含所有文本塊及其向量表示
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
db = FAISS.from_documents(texts, embeddings)# 檢索 top-3 結果
# 設置返回前3個最相關結果
retriever = db.as_retriever(search_kwargs={"k": 3})docs = retriever.invoke("llama2有多少參數")for doc in docs:print(doc.page_content)print("----")#  查詢文本會被自動轉換為向量# 通過余弦相似度在向量空間搜索最接近的3個文本塊# 返回包含原始文本和元數據的Document對象打印輸出:
but are not releasing.§
2. Llama 2-Chat, a ?ne-tuned version of Llama 2 that is optimized for dialogue use cases. We release
variants of this model with 7B, 13B, and 70B parameters as well.
We believe that the open release of LLMs, when done safely, will be a net bene?t to society. Like all LLMs,
----
Llama 2-Chat, at scales up to 70B parameters. On the series of helpfulness and safety benchmarks we tested,
Llama 2-Chat models generally perform better than existing open-source models. They also appear to
----
large language models (LLMs) ranging in scale from 7 billion to 70 billion parameters.
Our ?ne-tuned LLMs, called Llama 2-Chat, are optimized for dialogue use cases. Our
models outperform open-source chat models on most benchmarks we tested, and based on
----
關鍵參數說明:

參數 建議值范圍 作用

  • chunk_size 300-1000 控制文本塊大小,影響信息完整性和檢索精度
  • chunk_overlap 10%-30% 保持文本塊間的上下文關聯
  • k值 3-10 平衡結果覆蓋率和噪聲過濾
潛在優化方向:
  • 增加預處理步驟:清理PDF解析后的特殊字符

  • 嘗試不同的embedding模型(如text-embedding-3-small)

  • 結合元數據過濾(如指定搜索范圍)

  • 添加Rerank層優化結果排序

  • 實現對話歷史管理(對于多輪對話場景)

注意事項:
  • PDF解析質量取決于文檔結構復雜度

  • chunk_size過小可能導致上下文丟失

  • 實際應用需要處理分頁錯誤等異常情況

這個流程展示了典型的RAG(Retrieval-Augmented Generation)架構實現,后續可連接LLM生成最終答案,形成完整的問答系統。

更多的三方檢索組件鏈接,參考:三方檢索組件

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/71450.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/71450.shtml
英文地址,請注明出處:http://en.pswp.cn/web/71450.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Springboot集成dubbo完整過程(三)

準備工作 1,準備mysql服務環境2,準備redis服務環境3,準備zookeeper服務環境4,準備逆向生成bean的xml配置文件5,準備slf4j日志xml配置文件6,準備一個sql腳本 1,搭建創建服務工程 1,創…

【DeepSeek 】學習編程的利器:DeepSeek 使用指南

學習編程的利器:DeepSeek 使用指南 如果你正苦于如何開始學習 Python/R/Linux/HTML 語法,這個方法或許是你學習過程中的利器,又不用考慮請教真人,麻煩別人。 學習階段 第一階段:通讀語法書籍 第一步通讀一些相關語法…

【大模型篇】目前主流 AI 大模型體系全解析:架構、特點與應用

大家好,我是大 F,深耕AI算法十余年,互聯網大廠技術崗。分享AI算法干貨、技術心得。 歡迎關注《大模型理論和實戰》、《DeepSeek技術解析和實戰》,一起探索技術的無限可能! 閱讀完本文,您將知道:目前主流的大模型體系有哪些?及其架構的特點。 前言 在自然語言處理(NL…

電池管理系統(BMS)架構詳細解析:原理與器件選型指南

BMS(電池管理系統)架構詳細講解 從你提供的BMS(Battery Management System)架構圖來看,主要涉及到電池監控模塊、通信模塊、功率控制模塊等部分。下面我將詳細講解該架構的各個功能模塊及其工作原理。 1. 電池管理核…

決策樹(Decision Tree)基礎知識

目錄 一、回憶1、*機器學習的三要素:1)*函數族2)*目標函數2.1)*模型的其他復雜度參數 3)*優化算法 2、*前處理/后處理1)前處理:特征工程2)后處理:模型選擇和模型評估 3、…

洛谷 P3648 APIO2014 序列分割 題解

寫了挺多斜率優化的題目了,這道(差點)就速切了,原因還是單調隊列維護斜率的寫法出鍋。 題意 題目描述 你正在玩一個關于長度為 n n n 的非負整數序列的游戲。這個游戲中你需要把序列分成 k 1 k 1 k1 個非空的塊。為了得到 …

策略模式的C++實現示例

核心思想 策略模式是一種行為型設計模式,它定義了一系列算法,并將每個算法封裝在獨立的類中,使得它們可以互相替換。策略模式讓算法的變化獨立于使用它的客戶端,從而使得客戶端可以根據需要動態切換算法,而不需要修改…

Loki+Promtail+Grafana監控K8s日志

在現代云原生架構中,監控與日志管理對于確保系統穩定性和可靠性至關重要。Kubernetes(K8s)作為當下流行的容器編排平臺,對日志的監控管理需求尤為突出。Loki, Promtail 和 Grafana 構成了一套強大的日志監控解決方案,它…

Git 批量合并 Commit 并且保留之前的 Commit 快速實現的思路

文章目錄 需求Rebase / Pick / squashVim 的快速全局字符串替換 需求 我想把如下的提交 commit,變成一個 Commit,并且合并這些 Commit 的消息到一個節點 Rebase / Pick / squash 我合并到 5e59217 這個hash 上,這樣合并后會保留兩個 Commit…

基于海思soc的智能產品開發(芯片sdk和linux開發關系)

【 聲明:版權所有,歡迎轉載,請勿用于商業用途。 聯系信箱:feixiaoxing 163.com】 隨著國產化芯片的推進,在soc領域,越來越多的項目使用國產soc芯片。這些soc芯片,通常來說運行的os不是linux&…

將數據庫結構化數據整合到RAG問答中的方式

**將數據庫(結構化數據)接入 RAG(Retrieval-Augmented Generation)**的常見方式,并分別說明其實現方法、優點與缺點。 方式一:LLM 自動生成查詢語句(SQL/NoSQL)直接訪問數據庫 方法…

論壇系統測試報告

目錄 一、項目背景二、論壇系統測試用例思維導圖三、論壇系統測試3.1界面測試3.2登陸測試3.3主頁測試3.4個人中心測試 四、自動化測試腳本4.1配置驅動4.2創建瀏覽器類4.3功能測試4.3.1登陸測試4.3.2注冊測試4.3.3主頁測試4.3.4帖子編輯4.3.5運行主代碼 五、BUG分析六、測試總結…

python量化交易——金融數據管理最佳實踐——使用qteasy大批量自動拉取金融數據

文章目錄 使用數據獲取渠道自動填充數據QTEASY數據拉取功能數據拉取接口refill_data_source()數據拉取API的功能特性多渠道拉取數據實現下載流量控制實現錯誤重試日志記錄其他功能 qteasy是一個功能全面且易用的量化交易策略框架, Github地址在這里。使用它&#x…

后端架構模式之-BFF(Backend-For-Frontend)

Backend-for-Frontend(BFF) 的概念與意義 1. 什么是 Backend-for-Frontend(BFF)? Backend-for-Frontend(簡稱 BFF)是一種后端架構模式,它為特定的前端應用(Web、移動端…

upload-labs靶場 1-21通關

目錄 1.Pass-01 前端繞過 分析 解題 2.Pass-02 服務器端檢測--修改IMME 分析 解題 3.Pass-03 黑名單繞過 分析 解題 4.Pass-04 .htaccess繞過 分析 解題 5.Pass-05 . .繞過和.user.ini繞過 分析 解題 6.Pass-06 大小寫繞過 分析 解題 7.Pass-07 空格繞過 分…

信貸風控系統架構設計

設計一個信貸風控系統需要綜合考慮業務需求、技術架構、數據治理、合規安全等多個維度。以下是從頂級Java架構師視角的系統設計方案,分模塊詳細說明: 一、系統架構設計原則 高可用性:7x24小時服務,多機房容災。低延遲&#xff1a…

Ubuntu20.04 在離線機器上安裝 NVIDIA Container Toolkit

步驟 1.下載4個安裝包 Index of /nvidia-docker/libnvidia-container/stable/ nvidia-container-toolkit-base_1.13.5-1_amd64.deb libnvidia-container1_1.13.5-1_amd64.deb libnvidia-container-tools_1.13.5-1_amd64.deb nvidia-container-toolkit_1.13.5-1_amd64.deb 步…

【工具】COME對比映射學習用于scRNA-seq數據的空間重構

介紹 單細胞RNA測序(scRNA-seq)能夠在單細胞分辨率下實現高通量轉錄組分析。固有的空間位置對于理解單細胞如何協調多細胞功能和驅動疾病至關重要。然而,在組織分離過程中,空間信息常常丟失。空間轉錄組學(ST&#xf…

Idea配置注釋模板

一、配置類注釋模板 打開IDEA,打開settings(快捷鍵:Ctrl Alt s),選擇Editor,找到File and Code Templates 這里以設置class文件為例,點擊Class,在右側配置以下內容 #if (${PACKAGE_NAME} && $…

pytorch高可用的設計策略和集成放大各自功能

在使用 PyTorch 編寫模型時,為確保模型具備高可用性,可從模型設計、代碼質量、訓練過程、部署等多個方面采取相應的方法,以下為你詳細介紹: 模型設計層面 模塊化設計 實現方式:將模型拆分成多個小的、獨立的模塊,每個模塊負責特定的功能。例如,在一個圖像分類模型中,可…