@@ -4,26 +4,26 @@ search:
4
4
---
5
5
# クイックスタート
6
6
7
- Realtime エージェントは、 OpenAI の Realtime API を使用して AI 音声会話を実現します 。このガイドでは、最初の Realtime 音声エージェントの作成手順を説明します 。
7
+ Realtime エージェントは、OpenAI の Realtime API を使って AI エージェントとの音声対話を可能にします 。このガイドでは、最初のリアルタイム音声エージェントの作成手順を説明します 。
8
8
9
9
!!! warning "ベータ機能"
10
- Realtime エージェントはベータ版です。実装の改良に伴い、互換性のない変更が入る可能性があります 。
10
+ Realtime エージェントはベータ版です。実装の改善に伴い、非互換の変更が入る可能性があります 。
11
11
12
12
## 前提条件
13
13
14
- - Python 3.9 以上
15
- - OpenAI API キー
16
- - OpenAI Agents SDK の基本的な知識
14
+ - Python 3.9 以上
15
+ - OpenAI API キー
16
+ - OpenAI Agents SDK の基本的な知識
17
17
18
18
## インストール
19
19
20
- まだの場合は、 OpenAI Agents SDK をインストールします:
20
+ まだの場合は、OpenAI Agents SDK をインストールします:
21
21
22
22
``` bash
23
23
pip install openai-agents
24
24
```
25
25
26
- ## はじめての Realtime エージェントの作成
26
+ ## 最初の Realtime エージェントの作成
27
27
28
28
### 1. 必要なコンポーネントのインポート
29
29
@@ -41,47 +41,77 @@ agent = RealtimeAgent(
41
41
)
42
42
```
43
43
44
- ### 3. Runner のセットアップ
44
+ ### 3. Runner をセットアップ
45
45
46
46
``` python
47
47
runner = RealtimeRunner(
48
48
starting_agent = agent,
49
49
config = {
50
50
" model_settings" : {
51
- " model_name" : " gpt-4o-realtime-preview" ,
52
- " voice" : " alloy" ,
53
- " modalities" : [" text" , " audio" ],
51
+ " model_name" : " gpt-realtime" ,
52
+ " voice" : " ash" ,
53
+ " modalities" : [" audio" ],
54
+ " input_audio_format" : " pcm16" ,
55
+ " output_audio_format" : " pcm16" ,
56
+ " input_audio_transcription" : {" model" : " gpt-4o-mini-transcribe" },
57
+ " turn_detection" : {" type" : " semantic_vad" , " interrupt_response" : True },
54
58
}
55
59
}
56
60
)
57
61
```
58
62
59
- ### 4. セッションの開始
63
+ ### 4. セッションを開始
60
64
61
65
``` python
62
- async def main ():
63
- # Start the realtime session
64
- session = await runner.run()
65
-
66
- async with session:
67
- # Send a text message to start the conversation
68
- await session.send_message(" Hello! How are you today?" )
69
-
70
- # The agent will stream back audio in real-time (not shown in this example)
71
- # Listen for events from the session
72
- async for event in session:
73
- if event.type == " response.audio_transcript.done" :
74
- print (f " Assistant: { event.transcript} " )
75
- elif event.type == " conversation.item.input_audio_transcription.completed" :
76
- print (f " User: { event.transcript} " )
77
-
78
- # Run the session
79
- asyncio.run(main())
66
+ # Start the session
67
+ session = await runner.run()
68
+
69
+ async with session:
70
+ print (" Session started! The agent will stream audio responses in real-time." )
71
+ # Process events
72
+ async for event in session:
73
+ try :
74
+ if event.type == " agent_start" :
75
+ print (f " Agent started: { event.agent.name} " )
76
+ elif event.type == " agent_end" :
77
+ print (f " Agent ended: { event.agent.name} " )
78
+ elif event.type == " handoff" :
79
+ print (f " Handoff from { event.from_agent.name} to { event.to_agent.name} " )
80
+ elif event.type == " tool_start" :
81
+ print (f " Tool started: { event.tool.name} " )
82
+ elif event.type == " tool_end" :
83
+ print (f " Tool ended: { event.tool.name} ; output: { event.output} " )
84
+ elif event.type == " audio_end" :
85
+ print (" Audio ended" )
86
+ elif event.type == " audio" :
87
+ # Enqueue audio for callback-based playback with metadata
88
+ # Non-blocking put; queue is unbounded, so drops won’t occur.
89
+ pass
90
+ elif event.type == " audio_interrupted" :
91
+ print (" Audio interrupted" )
92
+ # Begin graceful fade + flush in the audio callback and rebuild jitter buffer.
93
+ elif event.type == " error" :
94
+ print (f " Error: { event.error} " )
95
+ elif event.type == " history_updated" :
96
+ pass # Skip these frequent events
97
+ elif event.type == " history_added" :
98
+ pass # Skip these frequent events
99
+ elif event.type == " raw_model_event" :
100
+ print (f " Raw model event: { _truncate_str(str (event.data), 200 )} " )
101
+ else :
102
+ print (f " Unknown event type: { event.type} " )
103
+ except Exception as e:
104
+ print (f " Error processing event: { _truncate_str(str (e), 200 )} " )
105
+
106
+ def _truncate_str (s : str , max_length : int ) -> str :
107
+ if len (s) > max_length:
108
+ return s[:max_length] + " ..."
109
+ return s
80
110
```
81
111
82
- ## 完全なコード例
112
+ ## 完全な例
83
113
84
- 動作する完全なコード例は次のとおりです :
114
+ 以下は動作する完全な例です :
85
115
86
116
``` python
87
117
import asyncio
@@ -93,86 +123,109 @@ async def main():
93
123
name = " Assistant" ,
94
124
instructions = " You are a helpful voice assistant. Keep responses brief and conversational." ,
95
125
)
96
-
97
126
# Set up the runner with configuration
98
127
runner = RealtimeRunner(
99
128
starting_agent = agent,
100
129
config = {
101
130
" model_settings" : {
102
- " model_name" : " gpt-4o-realtime-preview" ,
103
- " voice" : " alloy" ,
104
- " modalities" : [" text" , " audio" ],
105
- " input_audio_transcription" : {
106
- " model" : " whisper-1"
107
- },
108
- " turn_detection" : {
109
- " type" : " server_vad" ,
110
- " threshold" : 0.5 ,
111
- " prefix_padding_ms" : 300 ,
112
- " silence_duration_ms" : 200
113
- }
131
+ " model_name" : " gpt-realtime" ,
132
+ " voice" : " ash" ,
133
+ " modalities" : [" audio" ],
134
+ " input_audio_format" : " pcm16" ,
135
+ " output_audio_format" : " pcm16" ,
136
+ " input_audio_transcription" : {" model" : " gpt-4o-mini-transcribe" },
137
+ " turn_detection" : {" type" : " semantic_vad" , " interrupt_response" : True },
114
138
}
115
- }
139
+ },
116
140
)
117
-
118
141
# Start the session
119
142
session = await runner.run()
120
143
121
144
async with session:
122
145
print (" Session started! The agent will stream audio responses in real-time." )
123
-
124
146
# Process events
125
147
async for event in session:
126
- if event.type == " response.audio_transcript.done" :
127
- print (f " Assistant: { event.transcript} " )
128
- elif event.type == " conversation.item.input_audio_transcription.completed" :
129
- print (f " User: { event.transcript} " )
130
- elif event.type == " error" :
131
- print (f " Error: { event.error} " )
132
- break
148
+ try :
149
+ if event.type == " agent_start" :
150
+ print (f " Agent started: { event.agent.name} " )
151
+ elif event.type == " agent_end" :
152
+ print (f " Agent ended: { event.agent.name} " )
153
+ elif event.type == " handoff" :
154
+ print (f " Handoff from { event.from_agent.name} to { event.to_agent.name} " )
155
+ elif event.type == " tool_start" :
156
+ print (f " Tool started: { event.tool.name} " )
157
+ elif event.type == " tool_end" :
158
+ print (f " Tool ended: { event.tool.name} ; output: { event.output} " )
159
+ elif event.type == " audio_end" :
160
+ print (" Audio ended" )
161
+ elif event.type == " audio" :
162
+ # Enqueue audio for callback-based playback with metadata
163
+ # Non-blocking put; queue is unbounded, so drops won’t occur.
164
+ pass
165
+ elif event.type == " audio_interrupted" :
166
+ print (" Audio interrupted" )
167
+ # Begin graceful fade + flush in the audio callback and rebuild jitter buffer.
168
+ elif event.type == " error" :
169
+ print (f " Error: { event.error} " )
170
+ elif event.type == " history_updated" :
171
+ pass # Skip these frequent events
172
+ elif event.type == " history_added" :
173
+ pass # Skip these frequent events
174
+ elif event.type == " raw_model_event" :
175
+ print (f " Raw model event: { _truncate_str(str (event.data), 200 )} " )
176
+ else :
177
+ print (f " Unknown event type: { event.type} " )
178
+ except Exception as e:
179
+ print (f " Error processing event: { _truncate_str(str (e), 200 )} " )
180
+
181
+ def _truncate_str (s : str , max_length : int ) -> str :
182
+ if len (s) > max_length:
183
+ return s[:max_length] + " ..."
184
+ return s
133
185
134
186
if __name__ == " __main__" :
187
+ # Run the session
135
188
asyncio.run(main())
136
189
```
137
190
138
191
## 設定オプション
139
192
140
193
### モデル設定
141
194
142
- - ` model_name ` : 利用可能な Realtime モデルから選択 (例: ` gpt-4o- realtime-preview ` )
143
- - ` voice ` : 音声の選択 (` alloy ` , ` echo ` , ` fable ` , ` onyx ` , ` nova ` , ` shimmer ` )
144
- - ` modalities ` : テキストや音声の有効化 (` ["text", "audio"] ` )
195
+ - ` model_name ` : 利用可能なリアルタイムモデルから選択 (例: ` gpt-realtime ` )
196
+ - ` voice ` : 音声を選択 (` alloy ` , ` echo ` , ` fable ` , ` onyx ` , ` nova ` , ` shimmer ` )
197
+ - ` modalities ` : テキストまたは音声を有効化 (` ["text"] ` または ` [ "audio"]` )
145
198
146
199
### オーディオ設定
147
200
148
- - ` input_audio_format ` : 入力音声の形式 (` pcm16 ` , ` g711_ulaw ` , ` g711_alaw ` )
149
- - ` output_audio_format ` : 出力音声の形式
150
- - ` input_audio_transcription ` : 書き起こしの設定
201
+ - ` input_audio_format ` : 入力音声の形式 (` pcm16 ` , ` g711_ulaw ` , ` g711_alaw ` )
202
+ - ` output_audio_format ` : 出力音声の形式
203
+ - ` input_audio_transcription ` : 文字起こしの設定
151
204
152
205
### ターン検出
153
206
154
- - ` type ` : 検出方法 (` server_vad ` , ` semantic_vad ` )
155
- - ` threshold ` : 音声活動の閾値 (0.0–1.0)
156
- - ` silence_duration_ms ` : ターン終了を検出する無音時間
157
- - ` prefix_padding_ms ` : 発話前の音声パディング
207
+ - ` type ` : 検出方法 (` server_vad ` , ` semantic_vad ` )
208
+ - ` threshold ` : 音声アクティビティのしきい値 (0.0–1.0)
209
+ - ` silence_duration_ms ` : ターン終了を検出する無音時間
210
+ - ` prefix_padding_ms ` : 発話前の音声パディング
158
211
159
212
## 次のステップ
160
213
161
- - [ Realtime エージェントについて詳しく学ぶ ] ( guide.md )
162
- - [ examples/realtime] ( https://github.com/openai/openai-agents-python/tree/main/examples/realtime ) フォルダの動作するコード例を参照
163
- - エージェントにツールを追加
164
- - エージェント間のハンドオフを実装
165
- - 安全性のためのガードレールを設定
214
+ - [ Realtime エージェントの詳細 ] ( guide.md )
215
+ - 実行可能なサンプルは [ examples/realtime] ( https://github.com/openai/openai-agents-python/tree/main/examples/realtime ) フォルダを参照してください
216
+ - エージェントにツールを追加する
217
+ - エージェント間のハンドオフを実装する
218
+ - 安全性のためのガードレールを設定する
166
219
167
220
## 認証
168
221
169
- 環境に OpenAI API キーが設定されていることを確認してください :
222
+ OpenAI API キーが環境に設定されていることを確認してください :
170
223
171
224
``` bash
172
225
export OPENAI_API_KEY=" your-api-key-here"
173
226
```
174
227
175
- また、セッション作成時に直接渡すこともできます :
228
+ または、セッション作成時に直接渡します :
176
229
177
230
``` python
178
231
session = await runner.run(model_config = {" api_key" : " your-api-key" })
0 commit comments