模板化提示
通過識別和提取變數來模板化提示
提示工具 API 正處於封閉研究預覽階段。申請加入封閉研究預覽。
開始之前
提示工具是一組用於生成和改進提示的 API。與我們的其他 API 不同,這是一個實驗性 API:您需要申請存取權限,並且它不具備與其他 API 相同的長期支援承諾。
這些 API 類似於 Anthropic Workbench 中提供的功能,旨在供其他提示工程平台和遊樂場使用。
開始使用提示改進器
要使用提示生成 API,您需要:
- 已加入提示工具 API 的封閉研究預覽
- 直接使用 API,而不是 SDK
- 添加 beta 標頭
prompt-tools-2025-04-02
此 API 在 SDK 中不可用
模板化提示
Headers
Optional header to specify the beta version(s) you want to use.
To use multiple betas, use a comma separated list like beta1,beta2
or specify the header multiple times for each beta.
Body
The prompt to templatize, structured as a list of message
objects.
Each message in the messages
array must:
- Contain only text-only content blocks
- Not include tool calls, images, or prompt caching blocks
Example of a simple text prompt:
[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Translate hello to German"
}
]
}
]
Note that only contiguous user messages with text content are allowed. Assistant prefill is permitted, but other content types will cause validation errors.
[
{
"content": [
{
"text": "Translate hello to German",
"type": "text"
}
],
"role": "user"
}
]
The existing system prompt to templatize.
{
"system": "You are a professional English to German translator",
[...]
}
Note that this differs from the Messages API; it is strictly a string.
"You are a professional English to German translator"
Response
The templatized prompt with variable placeholders.
The response includes the input messages with specific values replaced by variable placeholders. These messages maintain the original message structure but contain uppercase variable names in place of concrete values.
For example, an input message content like "Translate hello to German"
would be transformed to "Translate {{WORD_TO_TRANSLATE}} to {{TARGET_LANGUAGE}}"
.
{
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Translate {{WORD_TO_TRANSLATE}} to {{TARGET_LANGUAGE}}"
}
]
}
]
}
[
{
"content": [
{
"text": "Translate {{WORD_TO_TRANSLATE}} to {{TARGET_LANGUAGE}}",
"type": "text"
}
],
"role": "user"
}
]
The input system prompt with variables identified and replaced.
If no system prompt was provided in the original request, this field will be an empty string.
"You are a professional English to {{TARGET_LANGUAGE}} translator"
Usage information
A mapping of template variable names to their original values, as extracted from the input prompt during templatization. Each key represents a variable name identified in the templatized prompt, and each value contains the corresponding content from the original prompt that was replaced by that variable.
Example:
"variable_values": {
"WORD_TO_TRANSLATE": "hello",
"TARGET_LANGUAGE": "German"
}
In this example response, the original prompt – Translate hello to German
– was templatized to Translate WORD_TO_TRANSLATE to TARGET_LANGUAGE
, with the variable values extracted as shown.
{
"TARGET_LANGUAGE": "German",
"WORD_TO_TRANSLATE": "hello"
}