微信扫码
添加专属顾问
我要投稿
环境搭建
langchain==0.2.9 torch html2text paddlepaddle paddleocr
文本提取
import cv2from paddleocr import PPStructuretable_engine = PPStructure(show_log=True, use_gpu=False, lang='ch')img_path = r'./2.jpg'img = cv2.imread(img_path)result = table_engine(img)html = result[0]['res']['html']print(len(html))# 2723
提取的文本是html格式的,占据了很多无用的格式提示词,我们将它转为markdown格式,这样可以有效的减少输入长度:
import html2textmarkdown = html2text.html2text(html)print(len(markdown))# 1051
定义模型
from langchain_openai import ChatOpenAIchat_model = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0.0)
也可以加载自己的模型,可参考RAG:Langchain中使用自己的LLM大模型:
from langchain_huggingface import HuggingFacePipelinefrom langchain_huggingface import ChatHuggingFacellm = HuggingFacePipeline.from_model_id(model_id="./Qwen/Qwen2-0.5B-Instruct",task="text-generation",pipeline_kwargs=dict(max_new_tokens=512,do_sample=False,),)chat_model = ChatHuggingFace(llm=llm)
这样我们就构建好了自己的模型,后面的应用流程可以灵活配置。
要素提取
from langchain_core.prompts import ChatPromptTemplate# 创建Prompt模板chat_prompt_template = ChatPromptTemplate.from_messages([("system", "You are a helpful assistant"),("user", "根据表格内容回答下面问题:\n表格内容:\n{context}\nQuestion: {question}\nAnswer:")])# 流程搭建chain = chat_prompt_template | chat_model.bind(skip_prompt=True)# 问答运行res = chain.invoke({"context": markdown, "question": "姓名是什么?"})# res.content# 赵开心res = chain.invoke({"context": markdown, "question": "婚姻状况?"})# res.content# 婚姻状况为未婚。
由于0.5B的模型比较小,我们做要素问答效果比较好,如果想一次性提取,那么可以使用更大的模型。下篇文章将介绍怎么做一次性结构化提取,prompt该怎么写。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-12-08
让AI智能体拥有像人类的持久记忆:基于LangGraph的长短期记忆管理实践指南
2025-12-04
Agentic RAG这样用LangChain解决复杂问题
2025-12-01
Deep Agent 进化论:基于文件系统的 Context Engineering 深度解析
2025-11-27
langgraph 1.0.4 最新发布:功能优化与修复详解
2025-11-25
LangChain 最新agent框架deepagents测评:长任务友好,高可控
2025-11-25
被 LangChain 全家桶搞晕了?LangGraph、LangSmith、LangFlow 一文读懂
2025-11-21
如何用 LangGraph 构建高效的 Agentic 系统
2025-11-19
LangChain v1.0 模型选型:静态还是动态?一文看懂 Agent 的正确打开方式
2025-11-03
2025-09-21
2025-10-23
2025-10-19
2025-10-31
2025-11-06
2025-11-05
2025-09-19
2025-10-23
2025-11-01
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21