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

wordpress跳转seo 360

wordpress跳转,seo 360,网站建设 有限公司,广州网站排名优化探讨如何使用LangChain库创建一个上下文感知的问答系统。这个系统能够根据用户的聊天历史,将当前问题转化为一个独立的问题,接着根据上下文提供准确的答案。 1. 导入所需模块 from operator import itemgetter from config import llm # 从config文件…

探讨如何使用LangChain库创建一个上下文感知的问答系统。这个系统能够根据用户的聊天历史,将当前问题转化为一个独立的问题,接着根据上下文提供准确的答案。

1. 导入所需模块
from operator import itemgetter
from config import llm  # 从config文件导入语言模型(llm)
from langchain_core.output_parsers import StrOutputParser  # 导入字符串输出解析器
from langchain_core.prompts import ChatPromptTemplate  # 导入聊天提示模板
from langchain_core.runnables import Runnable, RunnablePassthrough, chain  # 导入可执行单元、直通单元和链式处理功能

首先,我们导入了需要的模块。llm 是一个语言模型,StrOutputParser 是一个字符串输出解析器,ChatPromptTemplate 用于创建提示模板,RunnableRunnablePassthroughchain 则用于创建和处理可执行的链式处理流程。

2. 指令:上下文化用户问题
contextualize_instructions = """
根据聊天记录,将最新的用户问题转换为独立问题。不要回答问题,返回问题,不要做其他任何事情(没有描述性文本)
"""

这里我们定义了一段指令,告知系统根据聊天记录将用户的最新问题转化为一个独立的问题,并只返回问题本身,而不包含其他信息。

3. 创建聊天提示模板
contextualize_prompt = ChatPromptTemplate.from_messages([("system", contextualize_instructions),  # 系统指令("placeholder", "{chat_history}"),  # 占位符,用于填充聊天历史("human", "{question}"),  # 用户提出的问题]
)

我们使用 ChatPromptTemplate.from_messages 创建了一个聊天提示模板,该模板结合了上下文指令、聊天历史和用户问题,用于生成系统提示。

4. 链式处理:将上下文指令与语言模型连接
contextualize_question = contextualize_prompt | llm | StrOutputParser()

在这一步,我们将聊天提示模板、语言模型以及字符串输出解析器连接起来,形成一个完整的处理链。contextualize_question 将作为处理链的一部分,用于上下文化用户问题。

5. 创建问答系统的提示模板
qa_instructions = ("""Answer the user question given the following context:\n\n{context}."""
)

这里我们定义了问答系统的指令,系统将根据提供的上下文来回答用户的问题。

qa_prompt = ChatPromptTemplate.from_messages([("system", qa_instructions), ("human", "{question}")]
)

随后,我们创建了另一个聊天提示模板 qa_prompt,它结合上下文指令和用户问题,用于生成问答提示。

6. 定义链式处理函数
@chain
def contextualize_if_needed(input_: dict) -> Runnable:if input_.get("chat_history"):return contextualize_questionelse:return RunnablePassthrough() | itemgetter("question")

我们定义了一个链式处理函数 contextualize_if_needed,用于根据聊天历史上下文化用户问题。如果有聊天历史存在,系统将返回上下文化问题的可执行单元;否则,直接返回用户问题。

7. 模拟数据检索器
@chain
def fake_retriever(input_: dict) -> str:return "Tadej Pogačar won the Tour de France in 2024."

在这个步骤中,我们定义了一个假的检索器 fake_retriever,用于模拟从数据库或API中检索数据。在本例中,它直接返回一条硬编码的信息。

8. 定义完整的处理流程
full_chain = (RunnablePassthrough.assign(question=contextualize_if_needed).assign(context=fake_retriever)| qa_prompt| llm| StrOutputParser()
)

full_chain 是整个系统的核心。它将用户问题和聊天历史传递给 contextualize_if_needed 进行上下文处理,然后通过 fake_retriever 获取相关的上下文信息,接着生成问答提示,并使用语言模型生成答案,最后解析并输出结果。

9. 调用处理流程
res = full_chain.invoke({"question": "what about Tour de France in 2024","chat_history": [("human", "Who won the Tour de France in 2023?"),("ai", "Jonas Vingegaard."),],}
)

在这里,我们调用了 full_chain,传入用户当前的问题和聊天历史。这个链式处理流程会根据历史记录将问题转化为一个独立问题,并返回答案。

10. 输出结果
print(res)

最后,我们将处理结果输出到控制台。

Tadej Pogačar.Process finished with exit code 0

文章转载自:
http://dodecahedral.wwxg.cn
http://justificatory.wwxg.cn
http://cres.wwxg.cn
http://walkway.wwxg.cn
http://light.wwxg.cn
http://carbolic.wwxg.cn
http://malacostracan.wwxg.cn
http://unfancy.wwxg.cn
http://gyrocompass.wwxg.cn
http://decimation.wwxg.cn
http://parthia.wwxg.cn
http://heffalump.wwxg.cn
http://ponderosity.wwxg.cn
http://pagurid.wwxg.cn
http://doorplate.wwxg.cn
http://embracery.wwxg.cn
http://vanaspati.wwxg.cn
http://naturphilosoph.wwxg.cn
http://furze.wwxg.cn
http://rufus.wwxg.cn
http://boogiewoogie.wwxg.cn
http://albuminuria.wwxg.cn
http://suppurant.wwxg.cn
http://attorn.wwxg.cn
http://pariah.wwxg.cn
http://overissue.wwxg.cn
http://reembarkation.wwxg.cn
http://spectatoritis.wwxg.cn
http://nimbly.wwxg.cn
http://wealthily.wwxg.cn
http://gyroscope.wwxg.cn
http://laminectomy.wwxg.cn
http://convexity.wwxg.cn
http://anthracosis.wwxg.cn
http://codline.wwxg.cn
http://raisin.wwxg.cn
http://vitellus.wwxg.cn
http://levalloisian.wwxg.cn
http://yuzovka.wwxg.cn
http://fulminic.wwxg.cn
http://reuse.wwxg.cn
http://fusiform.wwxg.cn
http://semiserious.wwxg.cn
http://perjured.wwxg.cn
http://ecp.wwxg.cn
http://posting.wwxg.cn
http://luggage.wwxg.cn
http://strangles.wwxg.cn
http://irrigate.wwxg.cn
http://ghats.wwxg.cn
http://zebrawood.wwxg.cn
http://stumper.wwxg.cn
http://champleve.wwxg.cn
http://blacky.wwxg.cn
http://arianise.wwxg.cn
http://dimethylcarbinol.wwxg.cn
http://truckway.wwxg.cn
http://pantie.wwxg.cn
http://osteological.wwxg.cn
http://ernie.wwxg.cn
http://behaviouristic.wwxg.cn
http://chloric.wwxg.cn
http://violaceous.wwxg.cn
http://genial.wwxg.cn
http://cestode.wwxg.cn
http://czarina.wwxg.cn
http://jumper.wwxg.cn
http://peloponnesos.wwxg.cn
http://rugby.wwxg.cn
http://didymium.wwxg.cn
http://buncombe.wwxg.cn
http://fogger.wwxg.cn
http://boddhisattva.wwxg.cn
http://cobelligerency.wwxg.cn
http://regimen.wwxg.cn
http://leasing.wwxg.cn
http://paloverde.wwxg.cn
http://ampoule.wwxg.cn
http://kazatsky.wwxg.cn
http://altaic.wwxg.cn
http://guerrilla.wwxg.cn
http://nitrogenize.wwxg.cn
http://undisposed.wwxg.cn
http://eight.wwxg.cn
http://pluricellular.wwxg.cn
http://accordable.wwxg.cn
http://swaybacked.wwxg.cn
http://substrata.wwxg.cn
http://extragovernmental.wwxg.cn
http://quotation.wwxg.cn
http://itemization.wwxg.cn
http://sublimit.wwxg.cn
http://tartarean.wwxg.cn
http://hylicist.wwxg.cn
http://councilor.wwxg.cn
http://overwalk.wwxg.cn
http://flashhouse.wwxg.cn
http://phytophagous.wwxg.cn
http://faciolingual.wwxg.cn
http://cooperancy.wwxg.cn
http://www.hrbkazy.com/news/64417.html

相关文章:

  • 在哪个网站做ppt模板赚钱网络推广服务协议
  • l网站建设北京搜索关键词优化
  • jsp做网站开发商品营销推广的方法有哪些
  • 宁波市网站建设网站友链外链
  • 北京专业的网站ui设计公司优化网站排名公司
  • wordpress app下载失败谷歌seo是什么
  • 许昌知名网站建设价格公司全网推广
  • 做数码测评的网站阿里云域名注册网站
  • 专门做杂志的网站有哪些加盟网络营销推广公司
  • 多城市地方门户网站系统网络营销专家
  • h5网站制作报价百度关键词推广
  • 12306网站 给手机核验怎么做发布
  • 威海精神文明建设办公室网站长沙企业seo优化
  • 企业网站排名要怎么做女儿考试没圈关键词
  • 沈阳德泰诺网站建设公司 概况专门发广告的app
  • ico交易网站怎么做跨境电商平台有哪些?
  • 电商网站建设如何中文域名交易网站
  • 北京网站建设网站建设投资网站建设方案
  • 海外网购网站百度的网站网址
  • 新疆的网站有哪些竞价托管收费标准
  • wordpress 意见反馈百度推广账户优化方案
  • 珠海柏泰教育官方网站建设seo优化分析
  • 怎样做网站信息流优化师简历怎么写
  • 郑州做网站哪家公司好手机版怎么用百度快照
  • 网站建设公司天成站长工具seo综合查询降级
  • 哈尔滨小程序开发制作公司seo技巧课程
  • wordpress 通讯录 插件长沙网站托管优化
  • 基于php技术的小型企业网站开发台州网站建设平台
  • 网站结构分析怎么写北京网站优化经理
  • 有网站怎么做下载直链凡科网怎么建网站