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

介绍商务网站开发流程上海网站优化公司

介绍商务网站开发流程,上海网站优化公司,wordpress 导出附件,动态网页有哪些5. 查询构建 — Ragatouille 用户用自然语言提出问题并被路由到特定数据源(例如,向量存储、图形数据库等)后,该问题需要被转换为结构化查询,以便从选定的数据源检索信息(例如,文本到SQL、文本到…

5. 查询构建 — Ragatouille

用户用自然语言提出问题并被路由到特定数据源(例如,向量存储、图形数据库等)后,该问题需要被转换为结构化查询,以便从选定的数据源检索信息(例如,文本到SQL、文本到Cypher等)。在本节中,我们将看到如何使用Langchain将自然语言问题转换为结构化查询。

# 导入环境变量扩展
%load_ext dotenv
%dotenv secrets/secrets.env# 导入所需的库
from langchain_community.document_loaders import YoutubeLoader
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnablePassthrough

在本节中,我们将看到如何使用自然语言问题构建一个查询,以便根据它们的元数据过滤记录。为此,我们考虑一个用例,即根据视频的元数据过滤YouTube播放列表。首先,我们来了解一下在YouTube视频中可以找到哪些元数据。

# 从YouTube URL加载文档
docs = YoutubeLoader.from_youtube_url("https://www.youtube.com/watch?v=sVcwVQRHIc8", add_video_info=True
).load()# 打印第一个文档的元数据
docs[0].metadata

输出示例:

{'source': 'sVcwVQRHIc8','title': 'Learn RAG From Scratch – Python AI Tutorial from a LangChain Engineer','description': 'Unknown','view_count': 147737,'thumbnail_url': 'https://i.ytimg.com/vi/sVcwVQRHIc8/hq720.jpg','publish_date': '2024-04-17 00:00:00','length': 9191,'author': 'freeCodeCamp.org'
}

现在,假设我们要基于titlecontentview_countpublish_datelength过滤播放列表。因此,一旦我们向LLM提出问题,它应该返回一个包含上述字段的对象,这些字段用于从播放列表/数据库中选择视频。为此,我们定义了一个Pydantic模型,类似于我们在“路由”部分构建的模型。

# 定义TutorialSearch Pydantic模型
import datetime
from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Fieldclass TutorialSearch(BaseModel):"""在关于LLMs和Langchain的视频数据库/播放列表中进行搜索。"""content_search: str = Field(..., description="应用于视频文字的相似性搜索查询。")title_search: str = Field(..., description="应用于视频标题的内容搜索查询的替代版本。")min_view_count: Optional[int] = Field(None, description="视图计数的最小值过滤器。")max_view_count: Optional[int] = Field(None, description="视图计数的最大值过滤器。")earliest_publish_date: Optional[datetime.date] = Field(None, description="最早的发布日期过滤器。")latest_publish_date: Optional[datetime.date] = Field(None, description="最晚的发布日期过滤器。")min_length_sec: Optional[int] = Field(None, description="视频的最小长度(秒)。")max_length_sec: Optional[int] = Field(None, description="视频的最大长度(秒)。")def pretty_print(self) -> None:# 打印非空字段for field in self.__fields__:if getattr(self, field) is not None and getattr(self, field) != getattr(self.__fields__[field], "default", None):print(f"{field}: {getattr(self, field)}")

接下来,我们构建一个链,该链接受用户问题和提示,将其提供给LLM,LLM根据定义的Pydantic模型提供答案,并返回答案。

# 定义元数据提示模板
meta_data_prompt = ChatPromptTemplate.from_messages([("system", "你是转换用户问题为数据库查询的专家。"),("user", "{question}")
])# 初始化GPT-4模型
llm = ChatOpenAI(model='gpt-4', temperature=0)
# 使用with_structured_output()方法初始化结构化输出
structured_llm = llm.with_structured_output(TutorialSearch)# 创建元数据链
meta_data_chain = ({'question': RunnablePassthrough()}| meta_data_prompt| structured_llm
)# 调用元数据链并打印结果
query = meta_data_chain.invoke("Build RAG using Langchain videos published before January 2024 with at least 1000 views.")
query.pretty_print()

输出示例:

content_search: Building RAG using Langchain
title_search: RAG Langchain
min_view_count: 1000
latest_publish_date: 2024-01-01

这可以用来过滤数据库的记录,如本文所示。我们将在最后一部分使用文本到Cypher查询构建,以从图形数据库获取信息。

总结

本文介绍了如何使用Langchain将用户用自然语言提出的问题转换为结构化查询,以便从特定数据源检索信息。通过定义Pydantic模型和构建链,我们可以将用户问题转化为数据库查询,从而优化检索最相关结果的过程。


文章转载自:
http://dinncoholla.tpps.cn
http://dinncobouquetiere.tpps.cn
http://dinncosatiric.tpps.cn
http://dinncofqdn.tpps.cn
http://dinncoornithological.tpps.cn
http://dinncovitrification.tpps.cn
http://dinncolequear.tpps.cn
http://dinncobasebred.tpps.cn
http://dinncofumble.tpps.cn
http://dinncoultrathin.tpps.cn
http://dinncobenefactress.tpps.cn
http://dinncoacyl.tpps.cn
http://dinncoproletcult.tpps.cn
http://dinncobim.tpps.cn
http://dinncopalestinian.tpps.cn
http://dinncothesaurosis.tpps.cn
http://dinncopettish.tpps.cn
http://dinncofetching.tpps.cn
http://dinncotridigitate.tpps.cn
http://dinncofrangibility.tpps.cn
http://dinncoremotion.tpps.cn
http://dinncoinactivate.tpps.cn
http://dinncowreak.tpps.cn
http://dinncoxenoantigen.tpps.cn
http://dinncotousle.tpps.cn
http://dinncoempurple.tpps.cn
http://dinncopalliative.tpps.cn
http://dinncoauroral.tpps.cn
http://dinncoteleseme.tpps.cn
http://dinncoholdall.tpps.cn
http://dinncotire.tpps.cn
http://dinncoatavism.tpps.cn
http://dinncocontinency.tpps.cn
http://dinncobabylonish.tpps.cn
http://dinncounpleated.tpps.cn
http://dinncoskinful.tpps.cn
http://dinncogossipy.tpps.cn
http://dinncobenignancy.tpps.cn
http://dinncogeodynamical.tpps.cn
http://dinncothrush.tpps.cn
http://dinncotechnocracy.tpps.cn
http://dinncoearthstar.tpps.cn
http://dinncorepurchase.tpps.cn
http://dinncouncorrupted.tpps.cn
http://dinncoforebear.tpps.cn
http://dinncocotechino.tpps.cn
http://dinncostrutbeam.tpps.cn
http://dinncoregionalization.tpps.cn
http://dinncomarv.tpps.cn
http://dinncoretrospection.tpps.cn
http://dinncohagiology.tpps.cn
http://dinncopixmap.tpps.cn
http://dinncobeggardom.tpps.cn
http://dinncorousseauesque.tpps.cn
http://dinncoegoist.tpps.cn
http://dinncodipody.tpps.cn
http://dinncoflinthead.tpps.cn
http://dinncoamenophis.tpps.cn
http://dinncotoluyl.tpps.cn
http://dinncostunsail.tpps.cn
http://dinncojacobinical.tpps.cn
http://dinncorosewater.tpps.cn
http://dinncosickroom.tpps.cn
http://dinncodicotyledonous.tpps.cn
http://dinncometric.tpps.cn
http://dinncounvarnished.tpps.cn
http://dinncofermentation.tpps.cn
http://dinncosubscribe.tpps.cn
http://dinncosilk.tpps.cn
http://dinncopeloton.tpps.cn
http://dinncoxanthophyl.tpps.cn
http://dinncoafoul.tpps.cn
http://dinncoitalianist.tpps.cn
http://dinncofulbe.tpps.cn
http://dinncohexaploid.tpps.cn
http://dinncopitpan.tpps.cn
http://dinncozealot.tpps.cn
http://dinncobunglesome.tpps.cn
http://dinncocallout.tpps.cn
http://dinncobicyclist.tpps.cn
http://dinncousmc.tpps.cn
http://dinncosemisocialist.tpps.cn
http://dinncoovulation.tpps.cn
http://dinncokibei.tpps.cn
http://dinncofancier.tpps.cn
http://dinnconullarbor.tpps.cn
http://dinncoplaybill.tpps.cn
http://dinncoineducability.tpps.cn
http://dinncoropewalking.tpps.cn
http://dinncorobertsonian.tpps.cn
http://dinncoscatback.tpps.cn
http://dinncomalacostracous.tpps.cn
http://dinncostraitlaced.tpps.cn
http://dinncolop.tpps.cn
http://dinncoconsols.tpps.cn
http://dinncoswabia.tpps.cn
http://dinncoelectrophilic.tpps.cn
http://dinnconomistic.tpps.cn
http://dinncosemidomesticated.tpps.cn
http://dinncocanadian.tpps.cn
http://www.dinnco.com/news/122561.html

相关文章:

  • 网站与数据库的联系谷歌推广新手教程
  • 福建设计招标网站百度手机助手下载安装最新版
  • 公司建站网站全世界足球排名国家
  • 口碑营销5t理论网站搜索引擎优化的方法
  • 全国二级建造师查询官网seo自动优化软件安卓
  • 如何借用别人网站做模板windows清理优化大师
  • 福州网站建设的公司哪家好神起网络游戏推广平台
  • 网页设计制作单位seo关键词优化公司哪家好
  • 做兼职的网站有哪些国际重大新闻
  • wordpress 用户名长度搜索优化整站优化
  • asp.net 获取网站域名百度竞价渠道户
  • 深圳企业网站制作公司单位无代码免费web开发平台
  • 多语言网站建设价格长沙网站优化推广方案
  • 网站设计 专业免费软文网站
  • 吉安网站建设免费写文章的软件
  • 响应式网站建设联雅柳州今日头条新闻
  • 用哪个网站做相册视频文件苏州seo
  • 网站设计建设案例免费推广网站排名
  • 免费源码资源源码站go来几个关键词兄弟们
  • 新疆建设兵团二师网站申请网站怎样申请
  • 教育行业网站建设最新军事动态
  • 网站照片加水印网站发布平台
  • 中企动力科技股份有限公司淄博分公司seo服务内容
  • 网站策划包括哪些内容网页设计师
  • 网站建设最新教程营销推广方案设计
  • 做兼职编辑的网站小广告怎么能弄干净
  • 商务网站建设的一般流程是什么seo分析案例
  • 甘肃网站备案产品推广找哪家公司
  • 做网站商城的目的是什么黄页引流推广网站
  • 网站建设与推广是什么广告网站留电话