Skip to content

Commit 9e6db14

Browse files
Add Prompt IDs for Agents SDK (#110)
* your message * update type for prompt, convert protocol types * add example and docs entry * add docs translation and changeset * Update docs/src/content/docs/guides/models.mdx Co-authored-by: Kazuhiro Sera <[email protected]> * feedback * update API shape --------- Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 52eb3f9 commit 9e6db14

File tree

14 files changed

+207
-44
lines changed

14 files changed

+207
-44
lines changed

.changeset/kind-parents-swim.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openai/agents-openai': patch
3+
'@openai/agents-core': patch
4+
---
5+
6+
Adding support for prompt configuration to agents

docs/src/content/docs/guides/models.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Code } from '@astrojs/starlight/components';
77
import modelCustomProviderExample from '../../../../../examples/docs/models/customProviders.ts?raw';
88
import setDefaultOpenAIKeyExample from '../../../../../examples/docs/config/setDefaultOpenAIKey.ts?raw';
99
import modelSettingsExample from '../../../../../examples/docs/models/modelSettings.ts?raw';
10+
import promptIdExample from '../../../../../examples/basic/prompt-id.ts?raw';
1011
import agentWithModelExample from '../../../../../examples/docs/models/agentWithModel.ts?raw';
1112
import runnerWithModelExample from '../../../../../examples/docs/models/runnerWithModel.ts?raw';
1213
import setTracingExportApiKeyExample from '../../../../../examples/docs/config/setTracingExportApiKey.ts?raw';
@@ -87,6 +88,26 @@ Attach settings at either level:
8788

8889
---
8990

91+
## Prompt
92+
93+
Agents can be configured with a `prompt` parameter, indicating a server-stored
94+
prompt configuration that should be used to control the Agent's behavior. Currently,
95+
this option is only supported when you use the OpenAI
96+
[Responses API](https://platform.openai.com/docs/api-reference/responses).
97+
98+
| Field | Type | Notes |
99+
| ----------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------- |
100+
| `promptId` | `string` | Unique identifier for a prompt. |
101+
| `version` | `string` | Version of the prompt you wish to use. |
102+
| `variables` | `object` | A key/value pair of variables to substitute into the prompt. Values can be strings or content input types like text, images, or files. |
103+
104+
<Code lang="typescript" code={promptIdExample} title="Agent with prompt" />
105+
106+
Any additional agent configuration, like tools or instructions, will override the
107+
values you may have configured in your stored prompt.
108+
109+
---
110+
90111
## Custom model providers
91112

92113
Implementing your own provider is straightforward – implement `ModelProvider` and `Model` and

docs/src/content/docs/ja/guides/models.mdx

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import { Code } from '@astrojs/starlight/components';
77
import modelCustomProviderExample from '../../../../../../examples/docs/models/customProviders.ts?raw';
88
import setDefaultOpenAIKeyExample from '../../../../../../examples/docs/config/setDefaultOpenAIKey.ts?raw';
99
import modelSettingsExample from '../../../../../../examples/docs/models/modelSettings.ts?raw';
10+
import promptIdExample from '../../../../../../examples/basic/prompt-id.ts?raw';
1011
import agentWithModelExample from '../../../../../../examples/docs/models/agentWithModel.ts?raw';
1112
import runnerWithModelExample from '../../../../../../examples/docs/models/runnerWithModel.ts?raw';
1213
import setTracingExportApiKeyExample from '../../../../../../examples/docs/config/setTracingExportApiKey.ts?raw';
1314

14-
すべてのエージェントは最終的に LLM を呼び出します。SDK はモデルを次の 2 つの軽量インターフェースの背後に抽象化します:
15+
すべてのエージェントは最終的に LLM を呼び出します。SDK はモデルを 2 つの軽量インターフェースで抽象化します:
1516

16-
- [`Model`](/openai-agents-js/openai/agents/interfaces/model)特定の API に対して _1 回_ のリクエストを行う方法を知っています
17-
- [`ModelProvider`](/openai-agents-js/openai/agents/interfaces/modelprovider)人間が読めるモデル ****(例: `'gpt-4o'`)を `Model` インスタンスに解決します
17+
- [`Model`](/openai-agents-js/openai/agents/interfaces/model)特定 API _1 回_ のリクエスト方法を知っています
18+
- [`ModelProvider`](/openai-agents-js/openai/agents/interfaces/modelprovider)人が読みやすいモデル **名前**(例: `'gpt-4o'`)を `Model` インスタンスへ解決します
1819

19-
日常的な開発では、通常モデル **** と必要に応じて `ModelSettings` のみを扱います
20+
日常の開発では、通常モデルの **名前** と、たまに `ModelSettings` だけを扱います
2021

2122
<Code
2223
lang="typescript"
@@ -28,87 +29,103 @@ import setTracingExportApiKeyExample from '../../../../../../examples/docs/confi
2829

2930
## OpenAI プロバイダー
3031

31-
デフォルトの `ModelProvider` は OpenAI API を使って名前を解決します。2 種類のエンドポイントをサポートしています:
32+
デフォルトの `ModelProvider` は OpenAI API を使って名前を解決します。2 つのエンドポイントをサポートします:
3233

33-
| API | 用途 | `setOpenAIAPI()` の呼び出し |
34-
| ---------------- | ---------------------------------------------------------------- | ------------------------------------------ |
35-
| Chat Completions | 標準的なチャット & 関数呼び出し | `setOpenAIAPI('chat_completions')` |
36-
| Responses | ストリーミング中心の新しい生成 API(ツール呼び出し、柔軟な出力) | `setOpenAIAPI('responses')` _(デフォルト)_ |
34+
| API | 使用用途 | `setOpenAIAPI()` 呼び出し |
35+
| ---------------- | ---------------------------------------------------------------- | --------------------------------------- |
36+
| Chat Completions | 標準的なチャットと関数呼び出し | `setOpenAIAPI('chat_completions')` |
37+
| Responses | ストリーミング優先の新しい生成 API(ツール呼び出し、柔軟な出力) | `setOpenAIAPI('responses')` _(default)_ |
3738

3839
### 認証
3940

4041
<Code
4142
lang="typescript"
4243
code={setDefaultOpenAIKeyExample}
43-
title="デフォルトの OpenAI キーを設定"
44+
title="既定の OpenAI キーを設定"
4445
/>
4546

46-
独自のネットワーク設定が必要な場合は`setDefaultOpenAIClient(client)` で自前の `OpenAI` クライアントを差し込むこともできます。
47+
ネットワーク設定をカスタマイズしたい場合は`setDefaultOpenAIClient(client)` で独自の `OpenAI` クライアントを差し込むこともできます。
4748

48-
### デフォルトモデル
49+
### 既定のモデル
4950

50-
OpenAI プロバイダーのデフォルトは `gpt-4o` です。エージェント単位またはグローバルに上書きできます:
51+
OpenAI プロバイダーの既定モデルは `gpt-4o` です。エージェント単位またはグローバルで上書きできます:
5152

5253
<Code
5354
lang="typescript"
5455
code={runnerWithModelExample}
55-
title="デフォルトモデルを設定"
56+
title="既定モデルを設定"
5657
/>
5758

5859
---
5960

6061
## ModelSettings
6162

62-
`ModelSettings` は OpenAI のパラメーターを反映しつつ、プロバイダー非依存です。
63+
`ModelSettings` は OpenAI のパラメーターを反映していますが、プロバイダー非依存です。
6364

64-
| Field | Type | 説明 |
65+
| Field | Type | Notes |
6566
| ------------------- | ------------------------------------------ | ------------------------------------------------------------------------------ |
66-
| `temperature` | `number` | 創造性と決定論的挙動のバランス |
67-
| `topP` | `number` | ニュークリアスサンプリング |
68-
| `frequencyPenalty` | `number` | 繰り返しトークンのペナルティ |
69-
| `presencePenalty` | `number` | 新しいトークンを促進 |
70-
| `toolChoice` | `'auto' \| 'required' \| 'none' \| string` | [ツール使用の強制](/openai-agents-js/ja/guides/agents#forcing-tool-use) を参照 |
71-
| `parallelToolCalls` | `boolean` | サポートされていれば関数呼び出しを並列実行 |
67+
| `temperature` | `number` | 創造性と決定論のバランス |
68+
| `topP` | `number` | ニュークレアスサンプリング |
69+
| `frequencyPenalty` | `number` | 重複トークンをペナルティ |
70+
| `presencePenalty` | `number` | 新規トークンを促進 |
71+
| `toolChoice` | `'auto' \| 'required' \| 'none' \| string` | [ツール利用の強制](/openai-agents-js/ja/guides/agents#forcing-tool-use) を参照 |
72+
| `parallelToolCalls` | `boolean` | サポートされている場合に並列関数呼び出しを許可 |
7273
| `truncation` | `'auto' \| 'disabled'` | トークン切り捨て戦略 |
73-
| `maxTokens` | `number` | レスポンスの最大トークン数 |
74-
| `store` | `boolean` | 取得/RAG ワークフローのためにレスポンスを永続化 |
74+
| `maxTokens` | `number` | 応答内の最大トークン数 |
75+
| `store` | `boolean` | 取得 / RAG ワークフロー用に応答を永続化 |
7576

76-
設定はどちらのレベルにも紐付け可能です:
77+
設定はどちらのレベルにも付与できます:
7778

7879
<Code lang="typescript" code={modelSettingsExample} title="モデル設定" />
7980

80-
`Runner` レベルの設定は、エージェント固有の設定より優先されます。
81+
`Runner` レベルの設定は、エージェントレベルの競合する設定を上書きします。
82+
83+
---
84+
85+
## プロンプト
86+
87+
エージェントは `prompt` パラメーターで設定でき、これはサーバーに保存されたプロンプト設定を指し、エージェントの挙動を制御します。現在、このオプションは OpenAI の [Responses API](https://platform.openai.com/docs/api-reference/responses) を使用する場合のみサポートされています。
88+
89+
| Field | Type | Notes |
90+
| ----------- | -------- | ---------------------------------------------------------------------------------------------------------------------------- |
91+
| `prompt_id` | `string` | プロンプトの一意識別子 |
92+
| `version` | `string` | 使用したいプロンプトのバージョン |
93+
| `variables` | `object` | プロンプトに挿入する変数のキー/バリューのペア。値は文字列またはテキスト・画像・ファイルなどのコンテンツ入力型を指定できます |
94+
95+
<Code lang="typescript" code={promptIdExample} title="モデル設定" />
96+
97+
ツールや instructions など追加のエージェント設定は、保存済みプロンプト内で設定した値を上書きします。
8198

8299
---
83100

84101
## カスタムモデルプロバイダー
85102

86-
独自プロバイダーの実装は簡単です。`ModelProvider``Model` を実装し、`Runner` コンストラクターに渡します:
103+
独自プロバイダーの実装は簡単です。`ModelProvider``Model` を実装し、そのプロバイダーを `Runner` コンストラクターに渡します:
87104

88105
<Code
89106
lang="typescript"
90107
code={modelCustomProviderExample}
91-
title="最小構成のカスタムプロバイダー"
108+
title="最小限のカスタムプロバイダー"
92109
/>
93110

94111
---
95112

96113
## トレーシングエクスポーター
97114

98-
OpenAI プロバイダーを使用している場合、API キーを指定すると自動トレース送信を有効化できます:
115+
OpenAI プロバイダーを使用する際、API キーを渡すことで自動トレースエクスポートを有効にできます:
99116

100117
<Code
101118
lang="typescript"
102119
code={setTracingExportApiKeyExample}
103120
title="トレーシングエクスポーター"
104121
/>
105122

106-
これにより、[OpenAI ダッシュボード](https://platform.openai.com/traces) にトレースが送信され、ワークフローの完全な実行グラフを確認できます。
123+
これによりトレースが [OpenAI ダッシュボード](https://platform.openai.com/traces) に送信され、ワークフローの完全な実行グラフを確認できます。
107124

108125
---
109126

110127
## 次のステップ
111128

112-
- [エージェントの実行](/openai-agents-js/ja/guides/running-agents) を試してみましょう
113-
- [ツール](/openai-agents-js/ja/guides/tools) でモデルに超能力を付与
129+
- [エージェントの実行](/openai-agents-js/ja/guides/running-agents) を試してみる
130+
- モデルに [ツール](/openai-agents-js/ja/guides/tools) でスーパーパワーを付与
114131
- 必要に応じて [ガードレール](/openai-agents-js/ja/guides/guardrails)[トレーシング](/openai-agents-js/ja/guides/tracing) を追加

examples/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"start:lifecycle-example": "tsx lifecycle-example.ts",
1616
"start:local-image": "tsx local-image.ts",
1717
"start:previous-response-id": "tsx previous-response-id.ts",
18+
"start:prompt": "tsx prompt-id.ts",
1819
"start:remote-image": "tsx remote-image.ts",
1920
"start:stream-items": "tsx stream-items.ts",
2021
"start:stream-text": "tsx stream-text.ts",

examples/basic/prompt-id.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Agent, run } from '@openai/agents';
2+
3+
async function main() {
4+
const agent = new Agent({
5+
name: 'Assistant',
6+
prompt: {
7+
promptId: 'pmpt_684b3b772e648193b92404d7d0101d8a07f7a7903e519946',
8+
version: '1',
9+
variables: {
10+
poem_style: 'limerick',
11+
},
12+
},
13+
});
14+
15+
const result = await run(agent, 'Write about unrequited love.');
16+
console.log(result.finalOutput);
17+
}
18+
19+
if (require.main === module) {
20+
main().catch(console.error);
21+
}

packages/agents-core/src/agent.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ZodObject } from 'zod/v3';
33
import type { InputGuardrail, OutputGuardrail } from './guardrail';
44
import { AgentHooks } from './lifecycle';
55
import { getAllMcpTools, type MCPServer } from './mcp';
6-
import type { Model, ModelSettings } from './model';
6+
import type { Model, ModelSettings, Prompt } from './model';
77
import type { RunContext } from './runContext';
88
import {
99
type FunctionTool,
@@ -132,6 +132,20 @@ export interface AgentConfiguration<
132132
agent: Agent<TContext, TOutput>,
133133
) => Promise<string> | string);
134134

135+
/**
136+
* The prompt template to use for the agent (OpenAI Responses API only).
137+
*
138+
* Can either be a prompt template object, or a function that returns a prompt
139+
* template object. If a function is provided, it will be called with the run
140+
* context and the agent instance. It must return a prompt template object.
141+
*/
142+
prompt?:
143+
| Prompt
144+
| ((
145+
runContext: RunContext<TContext>,
146+
agent: Agent<TContext, TOutput>,
147+
) => Promise<Prompt> | Prompt);
148+
135149
/**
136150
* A description of the agent. This is used when the agent is used as a handoff, so that an LLM
137151
* knows what it does and when to invoke it.
@@ -303,6 +317,12 @@ export class Agent<
303317
runContext: RunContext<TContext>,
304318
agent: Agent<TContext, TOutput>,
305319
) => Promise<string> | string);
320+
prompt?:
321+
| Prompt
322+
| ((
323+
runContext: RunContext<TContext>,
324+
agent: Agent<TContext, TOutput>,
325+
) => Promise<Prompt> | Prompt);
306326
handoffDescription: string;
307327
handoffs: (Agent<any, TOutput> | Handoff<any, TOutput>)[];
308328
model: string | Model;
@@ -322,6 +342,7 @@ export class Agent<
322342
}
323343
this.name = config.name;
324344
this.instructions = config.instructions ?? '';
345+
this.prompt = config.prompt;
325346
this.handoffDescription = config.handoffDescription ?? '';
326347
this.handoffs = config.handoffs ?? [];
327348
this.model = config.model ?? '';
@@ -472,6 +493,21 @@ export class Agent<
472493
return this.instructions;
473494
}
474495

496+
/**
497+
* Returns the prompt template for the agent, if defined.
498+
*
499+
* If the agent has a function as its prompt, this function will be called with the
500+
* runContext and the agent instance.
501+
*/
502+
async getPrompt(
503+
runContext: RunContext<TContext>,
504+
): Promise<Prompt | undefined> {
505+
if (typeof this.prompt === 'function') {
506+
return await this.prompt(runContext, this);
507+
}
508+
return this.prompt;
509+
}
510+
475511
/**
476512
* Fetches the available tools from the MCP servers.
477513
* @returns the MCP powered tools

packages/agents-core/src/metadata.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
export const METADATA = {
55
"name": "@openai/agents-core",
6-
"version": "0.0.1-next.0",
6+
"version": "0.0.7",
77
"versions": {
8-
"@openai/agents-core": "0.0.1-next.0",
8+
"@openai/agents-core": "0.0.7",
99
"@openai/zod": "npm:zod@^3.25.40",
1010
"openai": "^5.0.1"
1111
}

packages/agents-core/src/model.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import {
77
AgentOutputItem,
88
JsonSchemaDefinition,
99
TextOutput,
10+
InputText,
11+
InputImage,
12+
InputFile,
1013
} from './types';
1114

1215
export type ModelSettingsToolChoice =
@@ -203,6 +206,11 @@ export type ModelRequest = {
203206
* An optional signal to abort the model request.
204207
*/
205208
signal?: AbortSignal;
209+
210+
/**
211+
* The prompt template to use for the model, if any.
212+
*/
213+
prompt?: Prompt;
206214
};
207215

208216
export type ModelResponse = {
@@ -259,3 +267,25 @@ export interface ModelProvider {
259267
*/
260268
getModel(modelName?: string): Promise<Model> | Model;
261269
}
270+
271+
/**
272+
* Reference to a prompt template and its variables.
273+
*/
274+
export type Prompt = {
275+
/**
276+
* The unique identifier of the prompt template to use.
277+
*/
278+
promptId: string;
279+
/**
280+
* Optional version of the prompt template.
281+
*/
282+
version?: string;
283+
/**
284+
* Optional variables to substitute into the prompt template.
285+
* Can be a string, or an object with string keys and values that are string,
286+
* InputText, InputImage, or InputFile.
287+
*/
288+
variables?: {
289+
[key: string]: string | InputText | InputImage | InputFile;
290+
};
291+
};

packages/agents-core/src/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
373373
systemInstructions: await state._currentAgent.getSystemPrompt(
374374
state._context,
375375
),
376+
prompt: await state._currentAgent.getPrompt(state._context),
376377
input: turnInput,
377378
previousResponseId: options.previousResponseId,
378379
modelSettings,
@@ -726,6 +727,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
726727
systemInstructions: await currentAgent.getSystemPrompt(
727728
result.state._context,
728729
),
730+
prompt: await currentAgent.getPrompt(result.state._context),
729731
input: turnInput,
730732
previousResponseId: options.previousResponseId,
731733
modelSettings,

0 commit comments

Comments
 (0)