您可以使用 LangSmith Python 和 TypeScript SDK 以编程方式管理提示。
以前此功能位于 langchainhub 包中,该包现已弃用。所有后续功能将位于 langsmith 包中。

安装包

在 Python 中,您可以直接使用 LangSmith SDK(推荐,完整功能),或者您可以通过 LangChain 包使用(仅限于推送和拉取提示)。 在 TypeScript 中,您必须使用 LangChain npm 包来拉取提示(它也允许推送)。对于所有其他功能,请使用 LangSmith 包。
pip install -U langsmith # version >= 0.1.99

配置环境变量

如果已经在当前工作区设置了 LANGSMITH_API_KEY,可跳过此步。 否则请前往 LangSmith Settings > API Keys > Create API Key 创建 API Key。 然后设置环境变量:
export LANGSMITH_API_KEY="lsv2_..."
文档中提到的 “prompt” 过去称为 “repo”,代码里出现 repo 即指 prompt。

推送提示

使用 push_prompt 可创建或更新提示。
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate

client = Client()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
url = client.push_prompt("joke-generator", object=prompt)
# url is a link to the prompt in the UI
print(url)
也可以将提示与模型组合成 RunnableSequence 后推送,用于保存该提示对应的模型配置。所选模型必须在 LangSmith playground 中受支持(参见 Supported Providers)。
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

client = Client()
model = ChatOpenAI(model="gpt-4o-mini")
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
chain = prompt | model
client.push_prompt("joke-generator-with-model", object=chain)

拉取提示

pull_prompt 方法会返回 LangChain 的 PromptTemplate 拉取私有提示无需指定作者 handle(即使设置了也可选填)。 从 LangChain Hub 拉取公开提示时,需指定作者 handle。
from langsmith import Client
from langchain_openai import ChatOpenAI

client = Client()
prompt = client.pull_prompt("joke-generator")
model = ChatOpenAI(model="gpt-4o-mini")
chain = prompt | model
chain.invoke({"topic": "cats"})
与推送类似,也可以将提示与模型一起拉取成 RunnableSequence。拉取时将 include_model 设为 True 即可,若存储的提示包含模型就会返回组合链。请确保已为该模型配置所需环境变量。
from langsmith import Client

client = Client()
chain = client.pull_prompt("joke-generator-with-model", include_model=True)
chain.invoke({"topic": "cats"})
可以通过指定提交哈希或 commit tag 拉取特定版本。
prompt = client.pull_prompt("joke-generator:12344e88")
若要从 LangChain Hub 拉取公开提示,请提供作者 handle。
prompt = client.pull_prompt("efriis/my-first-prompt")
在 Node.js 或支持动态导入的环境中,推荐使用 langchain/hub/node 入口,可自动反序列化提示中关联的模型配置。若非 Node 环境,“includeModel” 不支持非 OpenAI 模型,需使用基础 langchain/hub 入口。

在不使用 LangChain 的情况下调用提示

若希望将提示存储在 LangSmith 但直接调用模型厂商 API,可使用转换方法将提示转成 OpenAI 或 Anthropic API 所需的 payload。 这些方法依赖 LangChain 集成包逻辑,需额外安装对应依赖。示例如下:

OpenAI

pip install -U langchain_openai
from openai import OpenAI
from langsmith.client import Client, convert_prompt_to_openai_format

# langsmith client
client = Client()
# openai client
oai_client = OpenAI()

# pull prompt and invoke to populate the variables
prompt = client.pull_prompt("joke-generator")
prompt_value = prompt.invoke({"topic": "cats"})
openai_payload = convert_prompt_to_openai_format(prompt_value)
openai_response = oai_client.chat.completions.create(**openai_payload)

Anthropic

pip install -U langchain_anthropic
from anthropic import Anthropic
from langsmith.client import Client, convert_prompt_to_anthropic_format

# langsmith client
client = Client()
# anthropic client
anthropic_client = Anthropic()

# pull prompt and invoke to populate the variables
prompt = client.pull_prompt("joke-generator")
prompt_value = prompt.invoke({"topic": "cats"})
anthropic_payload = convert_prompt_to_anthropic_format(prompt_value)
anthropic_response = anthropic_client.messages.create(**anthropic_payload)

列出、删除、点赞提示

可通过 list_promptsdelete_promptlike_promptunlike_prompt 等方法管理提示。详见 LangSmith SDK client 文档。
# List all prompts in my workspace
prompts = client.list_prompts()

# List my private prompts that include "joke"
prompts = client.list_prompts(query="joke", is_public=False)

# Delete a prompt
client.delete_prompt("joke-generator")

# Like a prompt
client.like_prompt("efriis/my-first-prompt")

# Unlike a prompt
client.unlike_prompt("efriis/my-first-prompt")

Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.