LangChain-10 Agents langchainhub 共享的提示词Prompt

2024-04-06 13:36

本文主要是介绍LangChain-10 Agents langchainhub 共享的提示词Prompt,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LangChainHub 的思路真的很好,通过Hub的方式将Prompt 共享起来,大家可以通过很方便的手段,短短的几行代码就可以使用共享的Prompt
我个人非常看好这个项目。
官方推荐使用LangChainHub,但是它在GitHub已经一年没有更新了, 倒是数据还在更新。

请添加图片描述

安装依赖

pip install langchainhub

Prompt

为了防止大家不能访问,我这里先把用到的模板复制一份出来。

HUMANYou are a helpful assistant. Help the user answer any questions.You have access to the following tools:{tools}In order to use a tool, you can use <tool></tool> and <tool_input></tool_input> tags. You will then get back a response in the form <observation></observation>For example, if you have a tool called 'search' that could run a google search, in order to search for the weather in SF you would respond:<tool>search</tool><tool_input>weather in SF</tool_input><observation>64 degrees</observation>When you are done, respond with a final answer between <final_answer></final_answer>. For example:<final_answer>The weather in SF is 64 degrees</final_answer>Begin!Previous Conversation:{chat_history}Question: {input}{agent_scratchpad}

编写代码

代码主要部分是,定义了一个工具tool,让Agent执行,模拟了一个搜索引擎,让GPT利用工具对自身的内容进行扩展,从而完成复杂的任务。

from langchain import hub
from langchain.agents import AgentExecutor, tool
from langchain.agents.output_parsers import XMLAgentOutputParser
from langchain_openai import ChatOpenAImodel = ChatOpenAI(model="gpt-3.5-turbo",
)@tool
def search(query: str) -> str:"""Search things about current events."""return "32 degrees"tool_list = [search]
# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/xml-agent-convo")# Logic for going from intermediate steps to a string to pass into model
# This is pretty tied to the prompt
def convert_intermediate_steps(intermediate_steps):log = ""for action, observation in intermediate_steps:log += (f"<tool>{action.tool}</tool><tool_input>{action.tool_input}"f"</tool_input><observation>{observation}</observation>")return log# Logic for converting tools to string to go in prompt
def convert_tools(tools):return "\n".join([f"{tool.name}: {tool.description}" for tool in tools])agent = ({"input": lambda x: x["input"],"agent_scratchpad": lambda x: convert_intermediate_steps(x["intermediate_steps"]),}| prompt.partial(tools=convert_tools(tool_list))| model.bind(stop=["</tool_input>", "</final_answer>"])| XMLAgentOutputParser()
)agent_executor = AgentExecutor(agent=agent, tools=tool_list)
message = agent_executor.invoke({"input": "whats the weather in New york?"})
print(f"message: {message}")

运行结果

➜ python3 test10.py
message: {'input': 'whats the weather in New york?', 'output': 'The weather in New York is 32 degrees'}

在这里插入图片描述

这篇关于LangChain-10 Agents langchainhub 共享的提示词Prompt的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/880006

相关文章

Linux samba共享慢的原因及解决方案

《Linuxsamba共享慢的原因及解决方案》:本文主要介绍Linuxsamba共享慢的原因及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux samba共享慢原因及解决问题表现原因解决办法总结Linandroidux samba共享慢原因及解决

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

mss32.dll文件丢失怎么办? 电脑提示mss32.dll丢失的多种修复方法

《mss32.dll文件丢失怎么办?电脑提示mss32.dll丢失的多种修复方法》最近,很多电脑用户可能遇到了mss32.dll文件丢失的问题,导致一些应用程序无法正常启动,那么,如何修复这个问题呢... 在电脑常年累月的使用过程中,偶尔会遇到一些问题令人头疼。像是某个程序尝试运行时,系统突然弹出一个错误提

电脑提示找不到openal32.dll文件怎么办? openal32.dll丢失完美修复方法

《电脑提示找不到openal32.dll文件怎么办?openal32.dll丢失完美修复方法》openal32.dll是一种重要的系统文件,当它丢失时,会给我们的电脑带来很大的困扰,很多人都曾经遇到... 在使用电脑过程中,我们常常会遇到一些.dll文件丢失的问题,而openal32.dll的丢失是其中比较

电脑提示msvcp90.dll缺少怎么办? MSVCP90.dll文件丢失的修复方法

《电脑提示msvcp90.dll缺少怎么办?MSVCP90.dll文件丢失的修复方法》今天我想和大家分享的主题是关于在使用软件时遇到的一个问题——msvcp90.dll丢失,相信很多老师在使用电脑时... 在计算机使用过程中,可能会遇到 MSVCP90.dll 丢失的问题。MSVCP90.dll 是 Mic

电脑开机提示krpt.dll丢失怎么解决? krpt.dll文件缺失的多种解决办法

《电脑开机提示krpt.dll丢失怎么解决?krpt.dll文件缺失的多种解决办法》krpt.dll是Windows操作系统中的一个动态链接库文件,它对于系统的正常运行起着重要的作用,本文将详细介绍... 在使用 Windows 操作系统的过程中,用户有时会遇到各种错误提示,其中“找不到 krpt.dll”

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

java父子线程之间实现共享传递数据

《java父子线程之间实现共享传递数据》本文介绍了Java中父子线程间共享传递数据的几种方法,包括ThreadLocal变量、并发集合和内存队列或消息队列,并提醒注意并发安全问题... 目录通过 ThreadLocal 变量共享数据通过并发集合共享数据通过内存队列或消息队列共享数据注意并发安全问题总结在 J

NFS实现多服务器文件的共享的方法步骤

《NFS实现多服务器文件的共享的方法步骤》NFS允许网络中的计算机之间共享资源,客户端可以透明地读写远端NFS服务器上的文件,本文就来介绍一下NFS实现多服务器文件的共享的方法步骤,感兴趣的可以了解一... 目录一、简介二、部署1、准备1、服务端和客户端:安装nfs-utils2、服务端:创建共享目录3、服

Mybatis提示Tag name expected的问题及解决

《Mybatis提示Tagnameexpected的问题及解决》MyBatis是一个开源的Java持久层框架,用于将Java对象与数据库表进行映射,它提供了一种简单、灵活的方式来访问数据库,同时也... 目录概念说明MyBATis特点发现问题解决问题第一种方式第二种方式问题总结概念说明MyBatis(原名