当前位置: 首页 > news >正文

LangChain实战(十七):构建与PDF/PPT文档对话的AI助手

本文是《LangChain实战课》系列的第十七篇,将专篇深入讲解如何构建能够与PDF和PPT文档进行智能对话的AI助手。通过学习本文,您将掌握复杂格式文档的解析技巧、文本与表格处理技术,以及实现精准问答的系统方法。

前言

在日常工作和学习中,PDF和PPT文档是我们最常接触的文档格式之一。这些文档包含了大量有价值的信息,但手动提取和分析这些信息往往耗时耗力。通过结合LangChain和先进的文档解析技术,我们可以构建一个智能的文档问答助手,让用户能够像与人交谈一样与文档进行交互,快速获取所需信息。

面临的挑战与技术方案

PDF/PPT文档解析的挑战

  1. 格式复杂性:PDF和PPT具有复杂的布局和格式

  2. 文本提取困难:特别是扫描版PDF的OCR识别

  3. 表格处理:保持表格结构和数据的完整性

  4. 多模态内容:处理文本、图像、图表混合的内容

  5. 布局保持:保留文档的原始布局和语义结构

技术解决方案

我们将使用以下技术栈来解决这些挑战:

  • PyMuPDF:高效的PDF文本和表格提取

  • python-pptx:PPT文档解析

  • Unstructured:处理复杂文档结构

  • LangChain:构建问答流水线

  • Chroma/FAISS:向量存储和检索

  • OpenAI Embeddings:文本向量化

环境准备与安装

首先安装必要的依赖包:

# 安装核心库
pip install langchain openai python-dotenv# 安装文档处理库
pip install pymupdf python-pptx unstructured# 安装额外的文档处理依赖
pip install pdf2image pillow# 安装表格处理库
pip install tabula-py camelot-py# 安装向量数据库
pip install chromadb# 安装其他工具
pip install tiktoken sentence-transformers

构建PDF/PPT文档处理流水线

1. 文档加载与解析

让我们首先创建专门处理PDF和PPT文档的加载器:

import fitz  # PyMuPDF
from pptx import Presentation
import pandas as pd
from langchain.schema import Document
from typing import List, Dict, Any
import io
import osclass AdvancedDocumentLoader:def __init__(self):self.text_parser = TextParser()self.table_parser = TableParser()def load_pdf_document(self, file_path: str) -> List[Document]:"""加载并解析PDF文档"""documents = []try:with fitz.open(file_path) as pdf_document:for page_num in range(len(pdf_document)):page = pdf_document.load_page(page_num)# 提取文本内容text_content = page.get_text()if text_content.strip():documents.append(Document(page_content=text_content,metadata={"source": file_path,"page": page_num + 1,"type": "text","content_type": "text"}))# 提取表格tables = self._extract_pdf_tables(page)for i, table in enumerate(tables):documents.append(Document(page_content=table,metadata={"source": file_path,"page": page_num + 1,"table_index": i + 1,"type": "table","content_type": "table"}))return documentsexcept Exception as e:print(f"PDF解析错误: {e}")return []def load_ppt_document(self, file_path: str) -> List[Document]:"""加载并解析PPT文档"""documents = []try:presentation = Presentation(file_path)for slide_num, slide in enumerate(presentation.slides, 1):slide_text = []# 提取幻灯片文本for shape in slide.shapes:if hasattr(shape, "text") and shape.text.strip():slide_text.append(shape.text)if slide_text:content = "\n".join(slide_text)documents.append(Document(page_content=content,metadata={"source": file_path,"slide": slide_num,"type": "text","content_type": "slide_text"}))return documentsexcept Exception as e:print(f"PPT解析错误: {e}")return []def _extract_pdf_tables(self, page) -> List[str]:"""提取PDF页面中的表格"""tables = []try:# 使用PyMuPDF提取表格tabs = page.find_tables()if tabs.tables:for table in tabs.tables:table_data = table.extract()if table_data:# 将表格数据转换为字符串表示df = pd.DataFrame(table_data[1:], columns=table_data[0])tables.append(df.to_markdown(index=False))except Exception as e:print(f"表格提取错误: {e}")return tablesclass TextParser:"""文本内容解析器"""def preprocess_text(self, text: str) -> str:"""预处理文本内容"""# 移除多余的空格和换行text = ' '.join(text.split())# 其他清理操作...return textdef extract_key_phrases(self, text: str) -> List[str]:"""提取关键短语"""# 实现关键短语提取逻辑return []class TableParser:"""表格内容解析器"""def parse_table_structure(self, table_content: str) -> Dict[str, Any]:"""解析表格结构"""# 实现表格结构解析return {}

2. 高级表格处理与结构化

对于复杂的表格数据,我们需要更精细的处理:

import camelot
import tabula
from langchain.schema import Documentclass AdvancedTableProcessor:def __init__(self):passdef extract_tables_with_camelot(self, pdf_path: str) -> List[Document]:"""使用Camelot提取PDF表格"""documents = []try:tables = camelot.read_pdf(pdf_path, pages='all', flavor='stream')for i, table in enumerate(tables):if table.parsing_report['accuracy'] > 80:  # 准确率阈值df = table.dftable_content = self._format_table_content(df)documents.append(Document(page_content=table_content,metadata={"source": pdf_path,"table_index": i 

文章转载自:

http://y1uC5jRC.xhrws.cn
http://c0y6wKYH.xhrws.cn
http://AudxIYdN.xhrws.cn
http://hcELSvQC.xhrws.cn
http://f68nQMHh.xhrws.cn
http://5u8jNiqO.xhrws.cn
http://4AWxfTxV.xhrws.cn
http://0m720mz6.xhrws.cn
http://mTBVEg8l.xhrws.cn
http://ubHpmspR.xhrws.cn
http://S7cmTV1T.xhrws.cn
http://aADWBiM4.xhrws.cn
http://FCYyJqzZ.xhrws.cn
http://P3VJ5KKt.xhrws.cn
http://37HVuVeu.xhrws.cn
http://1GZG0SC5.xhrws.cn
http://djW0UhS3.xhrws.cn
http://busExOll.xhrws.cn
http://ZZt7iQ1T.xhrws.cn
http://cNJ7iKjP.xhrws.cn
http://iE28MPRK.xhrws.cn
http://pgmFuEWO.xhrws.cn
http://4NrP2htN.xhrws.cn
http://eudhkfte.xhrws.cn
http://KpXTnBRH.xhrws.cn
http://uUSlPqAZ.xhrws.cn
http://J49O4aAd.xhrws.cn
http://Tj76KO4d.xhrws.cn
http://drnENFpU.xhrws.cn
http://sC50C9R4.xhrws.cn
http://www.dtcms.com/a/368021.html

相关文章:

  • Android14 init启动Zygote详解
  • vue3+ts导出PDF
  • 最新PDF版本!Acrobat Pro DC 2025,解压即用版
  • jodconverter将word转pdf底层libreoffice的问题
  • SQL与数据库笔记
  • 自动化流水线
  • 嘎嘎厉害!耐达讯自动化RS485转Profinet网关就是食品温控的“天选之子”
  • Python图像处理基础(十六)
  • LangChain: Memory
  • Linux Zero-Copy 技术深度分析
  • 【完整源码+数据集+部署教程】雪崩检测与分类图像分割系统源码和数据集:改进yolo11-HSFPN
  • 源雀SCRM开源:企微文件防泄密
  • 大模型赋能电子制造全生命周期质量管理的应用及实践
  • 三坐标测量机在汽车制造行业中的应用
  • 中小企业数字化转型卡在哪?选对AI工具+用好企业微信,人力成本直降70%
  • 通用虚拟示教器:让机器人教学像玩游戏一样简单
  • 记录下chatgpt的openai 开发过程
  • 从0开始学习Java+AI知识点总结-30.前端web开发(JS+Vue+Ajax)
  • mysql进阶语法(视图)
  • 从Java全栈到云原生:一场技术深度对话
  • React学习教程,从入门到精通, React 新创建组件语法知识点及案例代码(11)
  • 从零开始的python学习——字典
  • windows安装flash-attn记录
  • threeJS 实现开花的效果
  • 【数字孪生核心技术】数字孪生有哪些核心技术?
  • Leetcode—2749. 得到整数零需要执行的最少操作数【中等】(__builtin_popcountl)
  • Python基础知识总结
  • 关于rust的所有权以及借用borrowing
  • 抓虫:sw架构防火墙服务启动失败 Unable to initialize Netlink socket: 不支持的协议
  • 智慧养老综合实训室建设方案:依托教育革新提升养老人才科技应用能力