微信扫码
添加专属顾问
我要投稿
环境搭建
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-22
LangChain Agent 年度报告:输出质量仍是 Agent 最大障碍,客服、研究是最快落地场景
2025-12-21
文档审核Agent2.0系统落地方案:LangChain1.1+MinerU
2025-12-21
LangChain、Dify、n8n、Coze框架对比
2025-12-20
涌现观点|LangChain 2025 报告发布:57%的企业在用Agent,但32%的人被"质量"卡住了
2025-12-18
2025 LangChain智能体工程年度报告发布!AI智能体从画饼到吃饼
2025-12-17
智能体LangChain v1.0生态解读与迁移建议
2025-12-08
让AI智能体拥有像人类的持久记忆:基于LangGraph的长短期记忆管理实践指南
2025-12-04
Agentic RAG这样用LangChain解决复杂问题
2025-11-03
2025-10-23
2025-10-19
2025-11-06
2025-10-31
2025-11-05
2025-10-23
2025-11-01
2025-10-15
2025-10-09
2025-11-03
2025-10-29
2025-07-14
2025-07-13
2025-07-05
2025-06-26
2025-06-13
2025-05-21