( 教学 )Agent 构建 Prompt(提示词)3. 提示词模板 (LangChain Hub)

84 阅读3分钟

( 教学 )Agent 构建 Prompt(提示词)2. 提示词模板 (LangChain Hub)

这是一个从 LangChain Hub 获取并执行提示词的示例。

LangChain Hub 是一个收集各项目常用提示词的仓库。它让开发者能够随时高效地搜索、获取并执行这些提示词,从而简化工作流程。

  • 提示词搜索与分类:开发者可通过关键词搜索和分类快速找到所需提示词。
  • 可复用性:提示词一旦创建,即可在多个项目中重复使用,缩短开发时间。
  • 实时执行:获取的提示词可立即通过 LangChain 执行,实时查看结果。
  • 可扩展与自定义:除默认提示词外,用户还可按需添加和修改提示词。

目录

参考资料

定义yaml 环境变量

from langchain_opentutorial import set_env
import os
set_env(
    {
        "SILICONFLOW_API_KEY": "",
        "LANGCHAIN_API_KEY": "",
        "LANGFUSE_SECRET_KEY": "",
        "LANGFUSE_PUBLIC_KEY": "",
        "LANGFUSE_HOST": "https://cloud.langfuse.com",
        "TAVILY_API_KEY": "",
        "LANGCHAIN_TRACING_V2": "true",
        "LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
        "LANGCHAIN_PROJECT": "FewShotPromptTemplate",
    }
)

从 Hub 获取提示词

  • 直接从 LangChain Hub 检索并执行提示词,加速您的工作流程。
  • 如何将现成的提示词无缝集成到您的项目中。
from langchain import hub 

# 获取最新版本的提示词
prompt = hub.pull("rlm/rag-prompt")
print(prompt)

输出结果

input_variables=['context', 'question'] input_types={} partial_variables={} metadata={'lc_hub_owner': 'rlm', 'lc_hub_repo': 'rag-prompt', 'lc_hub_commit_hash': '50442af133e61576e74536c6556cefe1fac147cad032f4377b60c436e6cdcb6e'} messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], input_types={}, partial_variables={}, template="You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\nQuestion: {question} \nContext: {context} \nAnswer:"), additional_kwargs={})]
prompt = hub.pull("rlm/rag-prompt:50442af1")
prompt

输出结果:

ChatPromptTemplate(input_variables=['context', 'question'], input_types={}, partial_variables={}, metadata={'lc_hub_owner': 'rlm', 'lc_hub_repo': 'rag-prompt', 'lc_hub_commit_hash': '50442af133e61576e74536c6556cefe1fac147cad032f4377b60c436e6cdcb6e'}, messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['context', 'question'], input_types={}, partial_variables={}, template="You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\nQuestion: {question} \nContext: {context} \nAnswer:"), additional_kwargs={})])

向 Prompt Hub 注册自己的提示词

  • 将自己的提示词注册到 Prompt Hub,开发者即可与社区共享自定义提示词,使其在多个项目中复用。
  • 该功能提升提示词标准化与高效管理,简化开发流程并促进协作。
from langchain.prompts import ChatPromptTemplate


prompt = ChatPromptTemplate.from_template(
    "Summarize the following text based on the given content. Please write the answer in Korean\n\nCONTEXT: {context}\n\nSUMMARY:"
)
from langchain import hub
# 将提示词上传到 Hub
hub.push("cjlee/simple-summary-korean-1", prompt)

成功上传到 Hub 后的输出如下:

ID/提示词名称/哈希

输出结果

from langchain import hub

# 从 Hub 获取提示词
pulled_prompt = hub.pull("teddynote/simple-summary-korean")
# 打印提示词内容
print(pulled_prompt)