LangChainHub 共享的提示词Prompt
langchainhub 是一个开源的提示词(Prompt)共享平台,主要面向使用LangChain框架的开发者。它为开发者提供了以下核心功能:
平台功能
- 提示词共享与发现:用户可以上传和下载经过测试的Prompt模板,支持按任务类型和领域分类浏览,提供Prompt评分和用户反馈系统
- Prompt版本管理:支持Prompt的版本控制,记录不同版本的迭代改进过程,允许回滚到历史版本
- 协作功能:可以fork他人分享的Prompt进行二次开发,支持团队协作编辑,提供讨论区供用户交流Prompt优化经验
- 测试与验证:内置Prompt测试环境,支持批量测试不同LLM模型的效果,提供性能指标评估
技术特点
- 与LangChain深度集成:支持直接导出为LangChain可用的Prompt模板,兼容LangChain的各种组件,提供API便于程序化访问
- 格式标准化:统一的Prompt描述格式,包含元数据,支持变量插值和条件逻辑
- 安全机制:敏感信息过滤,使用权限控制,Prompt来源验证
应用场景
- 快速开发:新项目可直接复用已验证Prompt,减少重复设计时间,跨团队知识共享
- 教育研究:学习优质Prompt设计模式,分析不同Prompt的效果差异
- 企业应用:建立企业内部的Prompt知识库,标准化Prompt开发流程
使用建议
- 搜索技巧:使用精确的关键词组合,关注高评分和活跃维护的Prompt
- 贡献指南:提供清晰的Prompt描述,包含测试用例和预期输出
- 优化流程:从基础Prompt开始逐步改进,记录每次修改的效果变化
该平台目前支持多种主流大语言模型(如GPT、Claude、LLaMA等)的Prompt管理,并持续扩展更多功能以满足开发者需求。
LangChainHub 的思路真的很好,通过Hub的方式将Prompt 共享起来,大家可以通过很方便的手段,短短的几行代码就可以使用共享的Prompt。作者非常看好这个项目。官方推荐使用LangChainHub,但是它在GitHub已经一年没有更新了,数据还在更新。
安装依赖
pip install langchainhub
Prompt模板
HUMAN
You 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 ChatOpenAI
model = 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'}