|
| 1 | +import { Agent, run } from '@openai/agents'; |
| 2 | +import OpenAI from 'openai'; |
| 3 | + |
| 4 | +async function main() { |
| 5 | + const client = new OpenAI(); |
| 6 | + |
| 7 | + console.log('### New conversation:\n'); |
| 8 | + const newConvo = await client.conversations.create({}); |
| 9 | + console.log(`New conversation: ${JSON.stringify(newConvo, null, 2)}`); |
| 10 | + const conversationId = newConvo.id; |
| 11 | + |
| 12 | + const agent = new Agent({ |
| 13 | + name: 'Assistant', |
| 14 | + instructions: 'You are a helpful assistant. be VERY concise.', |
| 15 | + }); |
| 16 | + |
| 17 | + // Set the conversation ID for the runs |
| 18 | + console.log('\n### Agent runs:\n'); |
| 19 | + const runOptions = { conversationId }; |
| 20 | + let result = await run( |
| 21 | + agent, |
| 22 | + 'What is the largest country in South America?', |
| 23 | + runOptions, |
| 24 | + ); |
| 25 | + console.log(`First run: ${result.finalOutput}`); // e.g., Brazil |
| 26 | + result = await run(agent, 'What is the capital of that country?', runOptions); |
| 27 | + console.log(`Second run: ${result.finalOutput}`); // e.g., Brasilia |
| 28 | + |
| 29 | + console.log('\n### Conversation items:\n'); |
| 30 | + const convo = await client.conversations.items.list(conversationId); |
| 31 | + for await (const page of convo.iterPages()) { |
| 32 | + for (const item of page.getPaginatedItems()) { |
| 33 | + // desc order |
| 34 | + console.log(JSON.stringify(item, null, 2)); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | +if (require.main === module) { |
| 39 | + main().catch(console.error); |
| 40 | +} |
0 commit comments