跳到主内容

以编程方式管理提示

您可以使用 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 中当前工作区的 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 Playground 支持。(在此处查看设置:支持的提供商

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")
JavaScript 用户重要提示

对于拉取提示,如果您使用的是 Node.js 或支持动态导入的环境,我们建议使用 langchain/hub/node 入口点,因为它会自动处理与您的提示配置关联的模型的反序列化。

如果您处于非 Node 环境中,则“includeModel”不支持非 OpenAI 模型,您应该使用基本的 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 promptsdelete promptlike promptunlike 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")

此页面是否有帮助?


您可以在 GitHub 上 留下详细反馈.