From a16faa68f97a2d807ce6d287b16b7a8830419bf9 Mon Sep 17 00:00:00 2001 From: Dominik Kundel Date: Tue, 10 Jun 2025 15:56:25 -0700 Subject: [PATCH] chore(examples): add reasoning example --- examples/basic/package.json | 3 ++- examples/basic/reasoning.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/basic/reasoning.ts diff --git a/examples/basic/package.json b/examples/basic/package.json index 13e1b41a..a6806bf8 100644 --- a/examples/basic/package.json +++ b/examples/basic/package.json @@ -20,6 +20,7 @@ "start:stream-text": "tsx stream-text.ts", "start:json-schema-output-type": "tsx json-schema-output-type.ts", "start:tool-use-behavior": "tsx tool-use-behavior.ts", - "start:tools": "tsx tools.ts" + "start:tools": "tsx tools.ts", + "start:reasoning": "tsx reasoning.ts" } } diff --git a/examples/basic/reasoning.ts b/examples/basic/reasoning.ts new file mode 100644 index 00000000..21ed49e8 --- /dev/null +++ b/examples/basic/reasoning.ts @@ -0,0 +1,36 @@ +import { styleText } from 'node:util'; +import { Agent, run } from '@openai/agents'; + +const ASSISTANT_PREFIX = styleText(['bgGreen', 'black'], 'Assistant'); +const THINKING_PREFIX = styleText(['bgGray', 'black'], 'Thought'); + +async function main() { + const agent = new Agent({ + name: 'Agent', + model: 'o3', + modelSettings: { + providerData: { + reasoning: { + effort: 'high', + summary: 'auto', + }, + }, + }, + }); + + const result = await run(agent, 'How many r are in strawberry?'); + + for (const item of result.newItems) { + if (item.type === 'reasoning_item') { + for (const entry of item.rawItem.content) { + if (entry.type === 'input_text') { + console.log(`${THINKING_PREFIX}: ${entry.text}`); + } + } + } + } + + console.log(`${ASSISTANT_PREFIX}: ${result.finalOutput}`); +} + +main().catch(console.error);