提示工程快速入门 (SDK)
本快速入门将介绍如何使用 SDK 创建、测试和迭代提示。在本教程中,我们将使用 OpenAI,但您可以使用所需的任何 LLM。
快速入门
本教程使用 SDK 进行提示工程,如果您有兴趣改用 UI,请阅读本指南。
1. 设置
首先,安装所需的软件包:
- 蟒
- TypeScript (类型脚本)
pip install -qU langsmith openai langchain_core
yarn add langsmith @langchain/core langchain openai
接下来,确保您已注册 LangSmith 帐户,然后创建并设置您的 API 密钥。 您还需要注册 OpenAI API 密钥以运行本教程中的代码。
LANGSMITH_API_KEY = '<your_api_key>'
OPENAI_API_KEY = '<your_api_key>'
2. 创建提示
要在 LangSmith 中创建提示,请在提示中定义所需的消息列表,然后使用ChatPromptTemplate函数 (Python) 或 TypeScript 函数。
然后您所要做的就是调用push_prompt(Python) 或pushPrompt(TypeScript) 将您的提示发送到 LangSmith!
- 蟒
- TypeScript (类型脚本)
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate
# Connect to the LangSmith client
client = Client()
# Define the prompt
prompt = ChatPromptTemplate([
("system", "You are a helpful chatbot."),
("user", "{question}"),
])
# Push the prompt
client.push_prompt("my-prompt", object=prompt)
import { Client } from "langsmith";
import { ChatPromptTemplate } from "@langchain/core/prompts";
// Connect to the LangSmith client
const client = new Client();
// Define the prompt
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful chatbot."],
["user", "{question}"]
]);
// Push the prompt
await client.pushPrompt("my-prompt", {
object: prompt
});
3. 测试提示
要测试提示,您需要提取提示,使用要测试的输入值调用它,然后使用这些输入值调用模型。 您的 LLM 或应用程序期望。
- 蟒
- TypeScript (类型脚本)
from langsmith import Client
from openai import OpenAI
from langchain_core.messages import convert_to_openai_messages
# Connect to LangSmith and OpenAI
client = Client()
oai_client = OpenAI()
# Pull the prompt to use
# You can also specify a specific commit by passing the commit hash "my-prompt:<commit-hash>"
prompt = client.pull_prompt("my-prompt")
# Since our prompt only has one variable we could also pass in the value directly
# The code below is equivalent to formatted_prompt = prompt.invoke("What is the color of the sky?")
formatted_prompt = prompt.invoke({"question": "What is the color of the sky?"})
# Test the prompt
response = oai_client.chat.completions.create(
model="gpt-4o",
messages=convert_to_openai_messages(formatted_prompt.messages),
)
import { OpenAI } from "openai";
import { pull } from "langchain/hub"
import { convertPromptToOpenAI } from "@langchain/openai";
// Connect to LangSmith and OpenAI
const oaiClient = new OpenAI();
// Pull the prompt to use
// You can also specify a specific commit by passing the commit hash "my-prompt:<commit-hash>"
const prompt = await pull("my-prompt");
// Format the prompt with the question
const formattedPrompt = await prompt.invoke({ question: "What is the color of the sky?" });
// Test the prompt
const response = await oaiClient.chat.completions.create({
model: "gpt-4o",
messages: convertPromptToOpenAI(formattedPrompt).messages,
});
4. 迭代提示
LangSmith 使整个团队可以轻松迭代提示。您的工作区成员可以选择一个提示进行迭代,一旦他们对自己的更改感到满意,他们就可以简单地将其保存为新的提交。
要改进提示:
-
我们建议参考模型提供商提供的文档,了解提示创建的最佳实践。 例如使用 OpenAI API 进行提示工程的最佳实践和 Gemini 的提示设计简介。
-
为了帮助您在 LangSmith 中迭代您的提示,我们创建了 Prompt Canvas — 一种用于构建和优化您的提示的交互式工具。了解如何使用 Prompt Canvas。
要向提示添加新提交,您可以使用相同的push_prompt(Python) 或pushPrompt(TypeScript) 方法设置为
首次创建提示的时间。
- 蟒
- TypeScript (类型脚本)
from langsmith import Client
from langchain_core.prompts import ChatPromptTemplate
# Connect to the LangSmith client
client = Client()
# Define the prompt to update
new_prompt = ChatPromptTemplate([
("system", "You are a helpful chatbot. Respond in Spanish."),
("user", "{question}"),
])
# Push the updated prompt making sure to use the correct prompt name
# Tags can help you remember specific versions in your commit history
client.push_prompt("my-prompt", object=new_prompt, tags=["Spanish"])
import { Client } from "langsmith";
import { ChatPromptTemplate } from "@langchain/core/prompts";
// Connect to the LangSmith client
const client = new Client();
// Define the prompt
const newPrompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful chatbot. Speak in Spanish."],
["user", "{question}"]
]);
// Push the updated prompt making sure to use the correct prompt name
// Tags can help you remember specific versions in your commit history
await client.pushPrompt("my-prompt", {
object: newPrompt,
tags: ["Spanish"]
});