Create starter files:
Based on the agent type, create appropriate starter code:
Basic Agent (TypeScript)
import { Agent, run } from '@openai/agents';
const agent = new Agent({
name: 'Assistant',
instructions: 'You are a helpful assistant.',
});
async function main() {
const result = await run(agent, 'Hello! How can you help me?');
console.log(result.finalOutput);
}
main().catch(console.error);
Basic Agent (Python)
from agents import Agent, Runner
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant."
)
result = Runner.run_sync(agent, "Hello! How can you help me?")
print(result.final_output)
Voice Agent (Python)
import asyncio
import numpy as np
import sounddevice as sd
from agents import Agent, function_tool
from agents.voice import AudioInput, SingleAgentVoiceWorkflow, VoicePipeline
@function_tool
def get_weather(city: str) -> str:
"""Get the weather for a given city."""
return f"The weather in {city} is sunny."
agent = Agent(
name="VoiceAssistant",
instructions="You are a helpful voice assistant. Be concise.",
tools=[get_weather],
)
async def main():
pipeline = VoicePipeline(workflow=SingleAgentVoiceWorkflow(agent))
buffer = np.zeros(24000 * 3, dtype=np.int16)
audio_input = AudioInput(buffer=buffer)
result = await pipeline.run(audio_input)
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
player.start()
async for event in result.stream():
if event.type == "voice_stream_event_audio":
player.write(event.data)
if __name__ == "__main__":
asyncio.run(main())
Voice Agent (TypeScript)
import { Agent, tool } from '@openai/agents';
import { RealtimeAgent, RealtimeSession, OpenAIRealtimeWebSocket } from '@openai/agents/realtime';
import { z } from 'zod';
const getWeather = tool({
name: 'get_weather',
description: 'Get the weather for a given city',
parameters: z.object({
city: z.string().describe('The city to get weather for'),
}),
execute: async ({ city }) => {
return `The weather in ${city} is sunny.`;
},
});
const agent = new RealtimeAgent({
name: 'VoiceAssistant',
instructions: 'You are a helpful voice assistant. Be concise.',
tools: [getWeather],
});
async function main() {
const transport = new OpenAIRealtimeWebSocket();
const session = new RealtimeSession(agent, { transport });
await session.connect();
console.log('Voice session connected! Ready for audio input.');
session.on('audio', (event) => {
console.log('Received audio chunk');
});
session.on('error', (event) => {
console.error('Error:', event.error);
});
}
main().catch(console.error);
Realtime Agent (Python)
import asyncio
from agents.realtime import RealtimeAgent, RealtimeRunner
agent = RealtimeAgent(
name="RealtimeAssistant",
instructions="You are a helpful voice assistant. Keep responses brief and conversational.",
)
async def main():
runner = RealtimeRunner(
starting_agent=agent,
config={
"model_settings": {
"model_name": "gpt-realtime",
"voice": "ash",
"modalities": ["audio"],
"input_audio_format": "pcm16",
"output_audio_format": "pcm16",
"input_audio_transcription": {"model": "gpt-4o-mini-transcribe"},
"turn_detection": {"type": "semantic_vad", "interrupt_response": True},
}
},
)
session = await runner.run()
async with session:
print("Realtime session started! Streaming audio responses...")
async for event in session:
if event.type == "agent_start":
print(f"Agent started: {event.agent.name}")
elif event.type == "audio":
pass
elif event.type == "error":
print(f"Error: {event.error}")
if __name__ == "__main__":
asyncio.run(main())
Realtime Agent (TypeScript)
import { RealtimeAgent, RealtimeSession, OpenAIRealtimeWebSocket } from '@openai/agents/realtime';
const agent = new RealtimeAgent({
name: 'RealtimeAssistant',
instructions: 'You are a helpful voice assistant. Keep responses brief and conversational.',
});
async function main() {
const transport = new OpenAIRealtimeWebSocket({
model: 'gpt-4o-realtime-preview',
});
const session = new RealtimeSession(agent, {
transport,
config: {
voice: 'ash',
modalities: ['audio'],
inputAudioFormat: 'pcm16',
outputAudioFormat: 'pcm16',
turnDetection: { type: 'semantic_vad', interruptResponse: true },
},
});
await session.connect();
console.log('Realtime session connected!');
session.on('agent_start', (event) => {
console.log(`Agent started: ${event.agent.name}`);
});
session.on('audio', (event) => {
console.log('Received audio chunk');
});
session.on('error', (event) => {
console.error('Error:', event.error);
});
}
main().catch(console.error);