跳到主要内容
您可以使用 LangSmith Python 和 TypeScript SDK 以编程方式管理提示。
以前,此功能位于现已弃用的 `langchainhub` 包中。今后所有功能都将位于 `langsmith` 包中。

安装包

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

配置环境变量

如果您已将 `LANGSMITH_API_KEY` 设置为 LangSmith 中当前工作区的 API 密钥,则可以跳过此步骤。 否则,请导航到 LangSmith 中的 `设置 > API 密钥 > 创建 API 密钥` 获取您工作区的 API 密钥。 设置您的环境变量。
export LANGSMITH_API_KEY="lsv2_..."
我们所说的“提示”以前被称为“仓库”,因此代码中所有对“仓库”的引用都指的是提示。

推送提示

要创建新提示或更新现有提示,您可以使用 `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 游乐场支持。(请参阅此处设置:支持的提供者
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` 返回。 要拉取私有提示,您无需指定所有者句柄(尽管如果您设置了,也可以指定)。 要从 LangChain Hub 拉取公共提示,您需要指定提示作者的句柄。
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。如果存储的提示包含模型,它将作为 RunnableSequence 返回。确保您已为正在使用的模型设置了正确的环境变量。
from langsmith import Client

client = Client()
chain = client.pull_prompt("joke-generator-with-model", include_model=True)
chain.invoke({"topic": "cats"})
拉取提示时,您还可以指定特定的提交哈希或提交标签以拉取特定版本的提示。
prompt = client.pull_prompt("joke-generator:12344e88")
要从 LangChain Hub 拉取公共提示,您需要指定提示作者的句柄。
prompt = client.pull_prompt("efriis/my-first-prompt")
对于拉取提示,如果您使用 Node.js 或支持动态导入的环境,我们建议使用 `langchain/hub/node` 入口点,因为它会自动处理与您的提示配置关联的模型的反序列化。如果您在非 Node 环境中,则非 OpenAI 模型不支持“includeModel”,您应该使用基本的 `langchain/hub` 入口点。

不使用 LangChain 的提示

如果您想将提示存储在 LangSmith 中但直接与模型提供者的 API 一起使用,您可以使用我们的转换方法。这些方法将您的提示转换为 OpenAI 或 Anthropic API 所需的负载。 这些转换方法依赖于 LangChain 集成包中的逻辑,您需要除了您选择的官方 SDK 之外,还安装相应的包作为依赖项。以下是一些示例:

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 prompts`、`delete prompt`、`like prompt` 和 `unlike prompt` 方法来列出、删除和点赞/取消点赞提示。有关这些方法的详细文档,请参阅 LangSmith SDK 客户端
# 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")

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.