微信扫码
添加专属顾问
我要投稿
环境搭建
langchain==0.2.9 torch html2text paddlepaddle paddleocr
文本提取
import cv2
from paddleocr import PPStructure
table_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 html2text
markdown = 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 HuggingFacePipeline
from langchain_huggingface import ChatHuggingFace
llm = 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-06-13
【LangChain 团队重磅实测报告】多智能体架构揭秘:谁才是性能之王?
2025-06-13
大模型_百炼:MCP让我焦虑
2025-06-11
前端学AI之LangChain.js入门教程:实现智能对话机器人
2025-06-08
Langfuse:重新定义LLM应用开发与运维的可观测性
2025-06-08
Langgraph实战--自定义embeding
2025-06-07
为 AI Agent 铺路:深度解析下一代应用的核心基建 LangGraph
2025-06-05
智能体框架怎么选?LangChain、Dify、CrewAI、AutoGen五大框架横向对比
2025-06-04
吴恩达对谈LangChain创始人:企业构建Agen系统的核心认知!
2025-03-20
2025-05-08
2025-04-18
2025-03-22
2025-05-06
2025-03-23
2025-04-13
2025-05-28
2025-03-22
2025-04-18
2025-06-13
2025-05-21
2025-05-19
2025-05-08
2025-05-06
2025-04-22
2025-04-18
2025-03-22