198 lines
5.4 KiB
Markdown
198 lines
5.4 KiB
Markdown
---
|
|
search:
|
|
exclude: true
|
|
---
|
|
# 빠른 시작
|
|
|
|
## 사전 요구 사항
|
|
|
|
Agents SDK의 기본 [빠른 시작 지침](../quickstart.md)을 따르고 가상 환경을 설정했는지 확인합니다. 그런 다음 SDK에서 선택적 음성 종속성을 설치합니다.
|
|
|
|
```bash
|
|
pip install 'openai-agents[voice]'
|
|
```
|
|
|
|
## 개념
|
|
|
|
알아야 할 주요 개념은 3단계 프로세스인 [`VoicePipeline`][agents.voice.pipeline.VoicePipeline]입니다.
|
|
|
|
1. 음성-텍스트 변환 모델을 실행하여 오디오를 텍스트로 변환합니다.
|
|
2. 일반적으로 에이전트 워크플로인 코드를 실행하여 결과를 생성합니다.
|
|
3. 텍스트-음성 변환 모델을 실행하여 결과 텍스트를 다시 오디오로 변환합니다.
|
|
|
|
```mermaid
|
|
graph LR
|
|
%% Input
|
|
A["🎤 Audio Input"]
|
|
|
|
%% Voice Pipeline
|
|
subgraph Voice_Pipeline [Voice Pipeline]
|
|
direction TB
|
|
B["Transcribe (speech-to-text)"]
|
|
C["Your Code"]:::highlight
|
|
D["Text-to-speech"]
|
|
B --> C --> D
|
|
end
|
|
|
|
%% Output
|
|
E["🎧 Audio Output"]
|
|
|
|
%% Flow
|
|
A --> Voice_Pipeline
|
|
Voice_Pipeline --> E
|
|
|
|
%% Custom styling
|
|
classDef highlight fill:#ffcc66,stroke:#333,stroke-width:1px,font-weight:700;
|
|
|
|
```
|
|
|
|
## 에이전트
|
|
|
|
먼저 몇 가지 에이전트를 설정하겠습니다. 이 SDK로 에이전트를 만들어 본 적이 있다면 익숙할 것입니다. 몇 개의 에이전트와 하나의 핸드오프, 하나의 도구를 사용합니다.
|
|
|
|
```python
|
|
import asyncio
|
|
import random
|
|
|
|
from agents import (
|
|
Agent,
|
|
function_tool,
|
|
)
|
|
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
|
|
|
|
|
|
|
|
@function_tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather for a given city."""
|
|
print(f"[debug] get_weather called with city: {city}")
|
|
choices = ["sunny", "cloudy", "rainy", "snowy"]
|
|
return f"The weather in {city} is {random.choice(choices)}."
|
|
|
|
|
|
spanish_agent = Agent(
|
|
name="Spanish",
|
|
handoff_description="A Spanish-speaking agent.",
|
|
instructions=prompt_with_handoff_instructions(
|
|
"You're speaking to a human, so be polite and concise. Speak in Spanish.",
|
|
),
|
|
model="gpt-5.6-sol",
|
|
)
|
|
|
|
agent = Agent(
|
|
name="Assistant",
|
|
instructions=prompt_with_handoff_instructions(
|
|
"You're speaking to a human, so be polite and concise. If the user speaks in Spanish, hand off to the Spanish agent.",
|
|
),
|
|
model="gpt-5.6-sol",
|
|
handoffs=[spanish_agent],
|
|
tools=[get_weather],
|
|
)
|
|
```
|
|
|
|
## 음성 파이프라인
|
|
|
|
[`SingleAgentVoiceWorkflow`][agents.voice.workflow.SingleAgentVoiceWorkflow]를 워크플로로 사용하여 간단한 음성 파이프라인을 설정합니다.
|
|
|
|
```python
|
|
from agents.voice import SingleAgentVoiceWorkflow, VoicePipeline
|
|
pipeline = VoicePipeline(workflow=SingleAgentVoiceWorkflow(agent))
|
|
```
|
|
|
|
## 파이프라인 실행
|
|
|
|
```python
|
|
import numpy as np
|
|
import sounddevice as sd
|
|
from agents.voice import AudioInput
|
|
|
|
# For simplicity, we'll just create 3 seconds of silence
|
|
# In reality, you'd get microphone data
|
|
buffer = np.zeros(24000 * 3, dtype=np.int16)
|
|
audio_input = AudioInput(buffer=buffer)
|
|
|
|
result = await pipeline.run(audio_input)
|
|
|
|
# Create an audio player using `sounddevice`
|
|
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
|
|
player.start()
|
|
|
|
# Play the audio stream as it comes in
|
|
async for event in result.stream():
|
|
if event.type == "voice_stream_event_audio":
|
|
player.write(event.data)
|
|
|
|
```
|
|
|
|
## 전체 구성
|
|
|
|
```python
|
|
import asyncio
|
|
import random
|
|
|
|
import numpy as np
|
|
import sounddevice as sd
|
|
|
|
from agents import (
|
|
Agent,
|
|
function_tool,
|
|
set_tracing_disabled,
|
|
)
|
|
from agents.voice import (
|
|
AudioInput,
|
|
SingleAgentVoiceWorkflow,
|
|
VoicePipeline,
|
|
)
|
|
from agents.extensions.handoff_prompt import prompt_with_handoff_instructions
|
|
|
|
|
|
@function_tool
|
|
def get_weather(city: str) -> str:
|
|
"""Get the weather for a given city."""
|
|
print(f"[debug] get_weather called with city: {city}")
|
|
choices = ["sunny", "cloudy", "rainy", "snowy"]
|
|
return f"The weather in {city} is {random.choice(choices)}."
|
|
|
|
|
|
spanish_agent = Agent(
|
|
name="Spanish",
|
|
handoff_description="A Spanish-speaking agent.",
|
|
instructions=prompt_with_handoff_instructions(
|
|
"You're speaking to a human, so be polite and concise. Speak in Spanish.",
|
|
),
|
|
model="gpt-5.6-sol",
|
|
)
|
|
|
|
agent = Agent(
|
|
name="Assistant",
|
|
instructions=prompt_with_handoff_instructions(
|
|
"You're speaking to a human, so be polite and concise. If the user speaks in Spanish, hand off to the Spanish agent.",
|
|
),
|
|
model="gpt-5.6-sol",
|
|
handoffs=[spanish_agent],
|
|
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)
|
|
|
|
# Create an audio player using `sounddevice`
|
|
player = sd.OutputStream(samplerate=24000, channels=1, dtype=np.int16)
|
|
player.start()
|
|
|
|
# Play the audio stream as it comes in
|
|
async for event in result.stream():
|
|
if event.type == "voice_stream_event_audio":
|
|
player.write(event.data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|
|
```
|
|
|
|
이 예제를 실행하면 에이전트가 사용자에게 음성으로 응답합니다! 에이전트와 직접 대화할 수 있는 데모는 [examples/voice/static](https://github.com/openai/openai-agents-python/tree/main/examples/voice/static)의 코드 예제를 확인하세요. |