it网站建设,网站建设的两个方面,公司如何注册企业邮箱,WordPress搭建手机网站简介
一夕轻雷落万丝#xff0c;霁光浮瓦碧参差。
紧接之前LangChain专题文章#xff1a;
15:如何用LangChain做长文档问答#xff1f;16:如何基于LangChain打造联网版ChatGPT#xff1f;17:ChatGPT应用框架LangChain速成大法
今天这篇小作文是LangChain实践专题的第4…简介
一夕轻雷落万丝霁光浮瓦碧参差。
紧接之前LangChain专题文章
15:如何用LangChain做长文档问答16:如何基于LangChain打造联网版ChatGPT17:ChatGPT应用框架LangChain速成大法
今天这篇小作文是LangChain实践专题的第4篇主要介绍如何用LangChain进行网页问答。前文介绍用LangChain做文档问答需要先将网页另存为pdf或者其他文档格式再读取文件做问答。今天这篇小作文介绍如何直接做网页问答本质上是将前文的数据本地存储改为数据在线爬取。
网页爬取
这里使用LLMRequestsChain从 URL 获取 HTML 结果然后使用 LLM 解析结果。以下以罗大佑百科网页为例说明如何用LangChain进行web QA。
示例1: 信息抽取
import os
os.environ[OPENAI_API_KEY] sk-XXXXimport warnings
warnings.filterwarnings(ignore)
# model_name gpt-3.5-turbo
model_name gpt-3.5-turbo-16k
task_url https://baike.baidu.com/item/%E7%BD%97%E5%A4%A7%E4%BD%91/236869
llm OpenAI(model_namemodel_name, temperature0)template 在 和 之间是网页的返回的HTML内容。
请抽取表格中的信息。 {requests_result}
请使用JSON格式返回你抽取的结果。
Extracted:prompt PromptTemplate(input_variables[requests_result],templatetemplate
)chain LLMRequestsChain(llm_chainLLMChain(llmllm, promptprompt))
inputs {url: task_url
}response chain(inputs)
print(response[output])输出结果如下
{姓名: 罗大佑,性别: 男,民族: 汉族,国籍: 中国,出生地: 台湾省台北市,出生日期: 1954年7月20日,毕业院校: 台湾中山医学院,星座: 巨蟹座,血型: O型,身高: 172 cm,经纪公司: 种子音乐,擅长乐器: 吉他、钢琴,代表作品: 东方之珠、明天会更好、鹿港小镇、光阴的故事、童年、恋曲1990、之乎者也、你的样子、亚细亚的孤儿、恋曲1980、爱人同志、闪亮的日子、爱的箴言、未来的主人翁、沉默的表示、穿过你的黑发的我的手、野百合也有春天,主要成就: 台湾金曲奖特别贡献奖、hito流行音乐奖颁奖典礼乐坛成就大奖、中国金唱片奖艺术成就奖、音乐风云榜歌坛杰出贡献奖、亚洲最杰出艺人奖
}示例2: 指定字段抽取信息
template 在 和 之间是网页的返回的HTML内容。
请抽取表格中的信息。 {requests_result}
请使用如下JSON格式返回你抽取的结果。
{{中文名: a, 代表作品: b, 祖籍: c, 妻子: d}}Extracted:prompt PromptTemplate(input_variables[requests_result],templatetemplate
)chain LLMRequestsChain(llm_chainLLMChain(llmllm, promptprompt))
inputs {url: task_url
}response chain(inputs)
print(response[output])输出结果如下
{中文名: 罗大佑, 代表作品: 东方之珠、明天会更好、鹿港小镇、光阴的故事、童年、恋曲1990、之乎者也、你的样子、亚细亚的孤儿、恋曲1980、爱人同志、闪亮的日子、爱的箴言、未来的主人翁、沉默的表示、穿过你的黑发的我的手、野百合也有春天, 祖籍: 广东省梅州市梅县区, 妻子: 李烈、Elaine}查阅原文 网页问答
示例1: 数据统计
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chains import LLMRequestsChain, LLMChain
import osos.environ[OPENAI_API_KEY] sk-XXXX
os.environ[HTTP_PROXY] XXX
os.environ[HTTPS_PROXY] XXXimport warnings
warnings.filterwarnings(ignore)
# model_name gpt-3.5-turbo
model_name gpt-3.5-turbo-16k
task_url https://baike.baidu.com/item/%E7%BD%97%E5%A4%A7%E4%BD%91/236869
llm OpenAI(model_namemodel_name, temperature0)template 在 和 之间是网页的返回的HTML内容。 {requests_result} 根据网页内容回答问题:{query}。prompt PromptTemplate(input_variables[requests_result, query],templatetemplate
)chain LLMRequestsChain(llm_chainLLMChain(llmllm, promptprompt))
inputs {url: task_url,query:罗大佑有几个老婆,
}response chain(inputs)
print(response[output])输出结果如下
根据网页内容罗大佑有两个老婆。他与李烈结婚后仅维持了一年半的婚姻然后在2010年与Elaine结婚。示例2: 时间类
# model_name gpt-3.5-turbo
model_name gpt-3.5-turbo-16k
task_url https://baike.baidu.com/item/%E7%BD%97%E5%A4%A7%E4%BD%91/236869
llm OpenAI(model_namemodel_name, temperature0)template 在 和 之间是网页的返回的HTML内容。 {requests_result} 根据网页内容回答问题:{query}。prompt PromptTemplate(input_variables[requests_result, query],templatetemplate
)chain LLMRequestsChain(llm_chainLLMChain(llmllm, promptprompt))
inputs {url: task_url,query:罗大佑最近获得的奖是什么,
}response chain(inputs)
print(response[output])输出结果如下
根据网页内容罗大佑最近获得的奖是第32届台湾金曲奖特别贡献奖。 文章转载自: http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.tkrpt.cn.gov.cn.tkrpt.cn http://www.morning.gmwqd.cn.gov.cn.gmwqd.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.dwzwm.cn.gov.cn.dwzwm.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.jopebe.cn.gov.cn.jopebe.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.qglqb.cn.gov.cn.qglqb.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.mqss.cn.gov.cn.mqss.cn http://www.morning.gtxrw.cn.gov.cn.gtxrw.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.mfzyn.cn.gov.cn.mfzyn.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.trtdg.cn.gov.cn.trtdg.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.fxxmj.cn.gov.cn.fxxmj.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.bzbq.cn.gov.cn.bzbq.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.mnjyf.cn.gov.cn.mnjyf.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.iiunion.com.gov.cn.iiunion.com http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.cdrzw.cn.gov.cn.cdrzw.cn http://www.morning.iznek.com.gov.cn.iznek.com