你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本文介绍如何在 Azure AI Foundry 门户中将语音实时与生成式 AI 和 Azure AI 语音配合使用。
创建并运行一个应用程序,以将语音实时直接与实时语音代理的生成式 AI 模型配合使用。
使用模型直接允许为每个会话指定自定义指令(提示),为动态或实验用例提供更大的灵活性。
如果希望对会话参数进行精细控制,或者需要频繁调整提示或配置,而无需在门户中更新代理,则模型可能更可取。
基于模型的会话的代码在某些方面更简单,因为它不需要管理代理 ID 或特定于代理的设置。
直接模型使用适用于不需要代理级抽象或内置逻辑的方案。
若要改用语音实时 API 和代理,请参阅 语音实时 API 代理快速入门。
先决条件
- 一份 Azure 订阅。 免费创建一个。
- 在一个受支持的区域中创建的 Azure AI Foundry 资源 。 有关区域可用性的详细信息,请参阅 语音实时概述文档。
小窍门
若要使用语音直播,无需使用 Azure AI Foundry 资源部署音频模型。 语音实时为完全托管,模型会自动为你部署。 有关模型可用性的详细信息,请参阅 语音实时概述文档。
在语音测试环境中实时试用语音功能
若要试用语音实时演示,请执行以下步骤:
转到 Azure AI Foundry 中的项目。
从左窗格中选择“游乐场地”。
在 “语音场 ”磁贴中,选择“ 试用语音场”。
选择按场景选择语音功能>语音直播。
选择一个示例方案,例如 休闲聊天。
选择 “开始” 以开始与聊天代理聊天。
选择 “结束 ”以结束聊天会话。
通过 Configuration>GenAI>生成 AI 模型从下拉列表中选择新的生成 AI 模型。
注释
还可以选择你在 Agents 沙箱中配置的代理。 有关详细信息,请参阅使用 Foundry 代理的语音实时快速入门。
根据需要编辑其他设置,例如 响应说明、 语音和 朗读率。
选择 “开始 ”以再次开始说话,然后选择 “结束 ”以结束聊天会话。
本文介绍如何使用适用于 Python 的 VoiceLive SDK 将 Azure AI 语音的实施语音功能与 Azure AI Foundry 模型配合使用。
创建并运行一个应用程序,以将语音实时直接与实时语音代理的生成式 AI 模型配合使用。
使用模型直接允许为每个会话指定自定义指令(提示),为动态或实验用例提供更大的灵活性。
如果希望对会话参数进行精细控制,或者需要频繁调整提示或配置,而无需在门户中更新代理,则模型可能更可取。
基于模型的会话的代码在某些方面更简单,因为它不需要管理代理 ID 或特定于代理的设置。
直接模型使用适用于不需要代理级抽象或内置逻辑的方案。
若要改用语音实时 API 和代理,请参阅 语音实时 API 代理快速入门。
先决条件
- 一份 Azure 订阅。 免费创建一个。
- Python 3.10 或更高版本。 如果未安装合适的 Python 版本,则可以按照 VS Code Python 教程中的说明操作,这是在操作系统上安装 Python 的最简单方法。
- 在一个受支持的区域中创建的 Azure AI Foundry 资源 。 有关区域可用性的详细信息,请参阅 区域支持。
小窍门
若要使用语音直播,无需使用 Azure AI Foundry 资源部署音频模型。 语音实时为完全托管,模型会自动为你部署。 有关模型可用性的详细信息,请参阅 语音实时概述文档。
Microsoft Entra ID 先决条件
若要使用 Microsoft Entra ID 进行推荐的无密钥身份验证,你需要:
- 安装使用 Microsoft Entra ID 进行无密钥身份验证所需的 Azure CLI。
- 将
Cognitive Services User角色分配给用户帐户。 你可以在 Azure 门户的“访问控制(IAM)”“添加角色分配”下分配角色。>
设置
创建新文件夹
voice-live-quickstart,并使用以下命令转到快速入门文件夹:mkdir voice-live-quickstart && cd voice-live-quickstart创建虚拟环境。 如果已安装 Python 3.10 或更高版本,则可以使用以下命令创建虚拟环境:
py -3 -m venv .venv .venv\scripts\activate激活 Python 环境意味着当通过命令行运行
python或pip时,你将使用应用程序的.venv文件夹中包含的 Python 解释器。 可以使用deactivate命令退出 python 虚拟环境,并在需要时重新激活它。小窍门
建议你创建并激活一个新的 Python 环境,用于安装本教程所需的包。 请勿将包安装到你的全局 Python 安装中。 安装 Python 包时,请务必使用虚拟或 Conda 环境,否则可能会中断 Python 的全局安装。
创建名为 requirements.txt的文件。 将以下包添加到文件:
aiohttp==3.11.18 azure-core==1.35.0 azure-identity==1.22.0 certifi==2025.4.26 cffi==1.17.1 cryptography==44.0.3 numpy==2.2.5 pycparser==2.22 python-dotenv==1.1.0 pyaudio requests==2.32.3 sounddevice==0.5.1 typing_extensions==4.13.2 urllib3==2.4.0 websocket-client==1.8.0 azure-ai-voicelive安装这些软件包:
pip install -r requirements.txt
检索资源信息
在要在其中运行代码的文件夹中创建一 .env 个名为的新文件。
在 .env 文件中,添加以下用于身份验证的环境变量:
AZURE_VOICELIVE_ENDPOINT=<your_endpoint>
AZURE_VOICELIVE_MODEL=<your_model>
AZURE_VOICELIVE_API_VERSION=2025-10-01
AZURE_VOICELIVE_API_KEY=<your_api_key> # Only required if using API key authentication
将默认值替换为实际终结点、模型、API 版本和 API 密钥。
| 变量名称 | 价值 |
|---|---|
AZURE_VOICELIVE_ENDPOINT |
从 Azure 门户检查资源时,可在“密钥和终结点”部分中找到此值。 |
AZURE_VOICELIVE_MODEL |
要使用的模型。 例如,gpt-4o 或 gpt-4o-mini-realtime-preview。 有关模型可用性的详细信息,请参阅 Voice Live API 概述文档。 |
AZURE_VOICELIVE_API_VERSION |
要使用的 API 版本。 例如,2025-10-01。 |
启动会话
本快速入门中的示例代码使用 Microsoft Entra ID 或 API 密钥进行身份验证。 可以将脚本参数设置为 API 密钥或访问令牌。
使用以下代码创建
voice-live-quickstart.py文件:import os import sys import asyncio import base64 import argparse import signal import threading import queue from azure.ai.voicelive.models import ServerEventType from typing import Union, Optional, TYPE_CHECKING, cast from concurrent.futures import ThreadPoolExecutor import logging # Audio processing imports try: import pyaudio except ImportError: print("This sample requires pyaudio. Install with: pip install pyaudio") sys.exit(1) ## Change to the directory where this script is located os.chdir(os.path.dirname(os.path.abspath(__file__))) # Environment variable loading try: from dotenv import load_dotenv load_dotenv('.\.env', override=True) except ImportError: print("Note: python-dotenv not installed. Using existing environment variables.") # Azure VoiceLive SDK imports from azure.core.credentials import AzureKeyCredential, TokenCredential from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential from azure.ai.voicelive.aio import connect if TYPE_CHECKING: # Only needed for type checking; avoids runtime import issues from azure.ai.voicelive.aio import VoiceLiveConnection from azure.ai.voicelive.models import ( RequestSession, ServerVad, AzureStandardVoice, Modality, InputAudioFormat, OutputAudioFormat, ) # Set up logging ## Add folder for logging if not os.path.exists('logs'): os.makedirs('logs') ## Add timestamp for logfiles from datetime import datetime timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") ## Set up logging logging.basicConfig( filename=f'logs/{timestamp}_voicelive.log', filemode="w", format='%(asctime)s:%(name)s:%(levelname)s:%(message)s', level=logging.INFO ) logger = logging.getLogger(__name__) class AudioProcessor: """ Handles real-time audio capture and playback for the voice assistant. Threading Architecture: - Main thread: Event loop and UI - Capture thread: PyAudio input stream reading - Send thread: Async audio data transmission to VoiceLive - Playback thread: PyAudio output stream writing """ def __init__(self, connection): self.connection = connection self.audio = pyaudio.PyAudio() # Audio configuration - PCM16, 24kHz, mono as specified self.format = pyaudio.paInt16 self.channels = 1 self.rate = 24000 self.chunk_size = 1024 # Capture and playback state self.is_capturing = False self.is_playing = False self.input_stream = None self.output_stream = None # Audio queues and threading self.audio_queue: "queue.Queue[bytes]" = queue.Queue() self.audio_send_queue: "queue.Queue[str]" = queue.Queue() # base64 audio to send self.executor = ThreadPoolExecutor(max_workers=3) self.capture_thread: Optional[threading.Thread] = None self.playback_thread: Optional[threading.Thread] = None self.send_thread: Optional[threading.Thread] = None self.loop: Optional[asyncio.AbstractEventLoop] = None # Store the event loop logger.info("AudioProcessor initialized with 24kHz PCM16 mono audio") async def start_capture(self): """Start capturing audio from microphone.""" if self.is_capturing: return # Store the current event loop for use in threads self.loop = asyncio.get_event_loop() self.is_capturing = True try: self.input_stream = self.audio.open( format=self.format, channels=self.channels, rate=self.rate, input=True, frames_per_buffer=self.chunk_size, stream_callback=None, ) self.input_stream.start_stream() # Start capture thread self.capture_thread = threading.Thread(target=self._capture_audio_thread) self.capture_thread.daemon = True self.capture_thread.start() # Start audio send thread self.send_thread = threading.Thread(target=self._send_audio_thread) self.send_thread.daemon = True self.send_thread.start() logger.info("Started audio capture") except Exception as e: logger.error(f"Failed to start audio capture: {e}") self.is_capturing = False raise def _capture_audio_thread(self): """Audio capture thread - runs in background.""" while self.is_capturing and self.input_stream: try: # Read audio data audio_data = self.input_stream.read(self.chunk_size, exception_on_overflow=False) if audio_data and self.is_capturing: # Convert to base64 and queue for sending audio_base64 = base64.b64encode(audio_data).decode("utf-8") self.audio_send_queue.put(audio_base64) except Exception as e: if self.is_capturing: logger.error(f"Error in audio capture: {e}") break def _send_audio_thread(self): """Audio send thread - handles async operations from sync thread.""" while self.is_capturing: try: # Get audio data from queue (blocking with timeout) audio_base64 = self.audio_send_queue.get(timeout=0.1) if audio_base64 and self.is_capturing and self.loop: # Schedule the async send operation in the main event loop future = asyncio.run_coroutine_threadsafe( self.connection.input_audio_buffer.append(audio=audio_base64), self.loop ) # Don't wait for completion to avoid blocking except queue.Empty: continue except Exception as e: if self.is_capturing: logger.error(f"Error sending audio: {e}") break async def stop_capture(self): """Stop capturing audio.""" if not self.is_capturing: return self.is_capturing = False if self.input_stream: self.input_stream.stop_stream() self.input_stream.close() self.input_stream = None if self.capture_thread: self.capture_thread.join(timeout=1.0) if self.send_thread: self.send_thread.join(timeout=1.0) # Clear the send queue while not self.audio_send_queue.empty(): try: self.audio_send_queue.get_nowait() except queue.Empty: break logger.info("Stopped audio capture") async def start_playback(self): """Initialize audio playback system.""" if self.is_playing: return self.is_playing = True try: self.output_stream = self.audio.open( format=self.format, channels=self.channels, rate=self.rate, output=True, frames_per_buffer=self.chunk_size, ) # Start playback thread self.playback_thread = threading.Thread(target=self._playback_audio_thread) self.playback_thread.daemon = True self.playback_thread.start() logger.info("Audio playback system ready") except Exception as e: logger.error(f"Failed to initialize audio playback: {e}") self.is_playing = False raise def _playback_audio_thread(self): """Audio playback thread - runs in background.""" while self.is_playing: try: # Get audio data from queue (blocking with timeout) audio_data = self.audio_queue.get(timeout=0.1) if audio_data and self.output_stream and self.is_playing: self.output_stream.write(audio_data) except queue.Empty: continue except Exception as e: if self.is_playing: logger.error(f"Error in audio playback: {e}") break async def queue_audio(self, audio_data: bytes): """Queue audio data for playback.""" if self.is_playing: self.audio_queue.put(audio_data) async def stop_playback(self): """Stop audio playback and clear queue.""" if not self.is_playing: return self.is_playing = False # Clear the queue while not self.audio_queue.empty(): try: self.audio_queue.get_nowait() except queue.Empty: break if self.output_stream: self.output_stream.stop_stream() self.output_stream.close() self.output_stream = None if self.playback_thread: self.playback_thread.join(timeout=1.0) logger.info("Stopped audio playback") async def cleanup(self): """Clean up audio resources.""" await self.stop_capture() await self.stop_playback() if self.audio: self.audio.terminate() self.executor.shutdown(wait=True) logger.info("Audio processor cleaned up") class BasicVoiceAssistant: """Basic voice assistant implementing the VoiceLive SDK patterns.""" def __init__( self, endpoint: str, credential: Union[AzureKeyCredential, TokenCredential], model: str, voice: str, instructions: str, ): self.endpoint = endpoint self.credential = credential self.model = model self.voice = voice self.instructions = instructions self.connection: Optional["VoiceLiveConnection"] = None self.audio_processor: Optional[AudioProcessor] = None self.session_ready = False self.conversation_started = False async def start(self): """Start the voice assistant session.""" try: logger.info(f"Connecting to VoiceLive API with model {self.model}") # Connect to VoiceLive WebSocket API async with connect( endpoint=self.endpoint, credential=self.credential, model=self.model, ) as connection: conn = connection self.connection = conn # Initialize audio processor ap = AudioProcessor(conn) self.audio_processor = ap # Configure session for voice conversation await self._setup_session() # Start audio systems await ap.start_playback() logger.info("Voice assistant ready! Start speaking...") print("\n" + "=" * 60) print("🎤 VOICE ASSISTANT READY") print("Start speaking to begin conversation") print("Press Ctrl+C to exit") print("=" * 60 + "\n") # Process events await self._process_events() except KeyboardInterrupt: logger.info("Received interrupt signal, shutting down...") except Exception as e: logger.error(f"Connection error: {e}") raise # Cleanup if self.audio_processor: await self.audio_processor.cleanup() async def _setup_session(self): """Configure the VoiceLive session for audio conversation.""" logger.info("Setting up voice conversation session...") # Create strongly typed voice configuration voice_config: Union[AzureStandardVoice, str] if self.voice.startswith("en-US-") or self.voice.startswith("en-CA-") or "-" in self.voice: # Azure voice voice_config = AzureStandardVoice(name=self.voice, type="azure-standard") else: # OpenAI voice (alloy, echo, fable, onyx, nova, shimmer) voice_config = self.voice # Create strongly typed turn detection configuration turn_detection_config = ServerVad(threshold=0.5, prefix_padding_ms=300, silence_duration_ms=500) # Create strongly typed session configuration session_config = RequestSession( modalities=[Modality.TEXT, Modality.AUDIO], instructions=self.instructions, voice=voice_config, input_audio_format=InputAudioFormat.PCM16, output_audio_format=OutputAudioFormat.PCM16, turn_detection=turn_detection_config, ) conn = self.connection assert conn is not None, "Connection must be established before setting up session" await conn.session.update(session=session_config) logger.info("Session configuration sent") async def _process_events(self): """Process events from the VoiceLive connection.""" try: conn = self.connection assert conn is not None, "Connection must be established before processing events" async for event in conn: await self._handle_event(event) except KeyboardInterrupt: logger.info("Event processing interrupted") except Exception as e: logger.error(f"Error processing events: {e}") raise async def _handle_event(self, event): """Handle different types of events from VoiceLive.""" logger.debug(f"Received event: {event.type}") ap = self.audio_processor conn = self.connection assert ap is not None, "AudioProcessor must be initialized" assert conn is not None, "Connection must be established" if event.type == ServerEventType.SESSION_UPDATED: logger.info(f"Session ready: {event.session.id}") self.session_ready = True # Start audio capture once session is ready await ap.start_capture() elif event.type == ServerEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED: logger.info("🎤 User started speaking - stopping playback") print("🎤 Listening...") # Stop current assistant audio playback (interruption handling) await ap.stop_playback() # Cancel any ongoing response try: await conn.response.cancel() except Exception as e: logger.debug(f"No response to cancel: {e}") elif event.type == ServerEventType.INPUT_AUDIO_BUFFER_SPEECH_STOPPED: logger.info("🎤 User stopped speaking") print("🤔 Processing...") # Restart playback system for response await ap.start_playback() elif event.type == ServerEventType.RESPONSE_CREATED: logger.info("🤖 Assistant response created") elif event.type == ServerEventType.RESPONSE_AUDIO_DELTA: # Stream audio response to speakers logger.debug("Received audio delta") await ap.queue_audio(event.delta) elif event.type == ServerEventType.RESPONSE_AUDIO_DONE: logger.info("🤖 Assistant finished speaking") print("🎤 Ready for next input...") elif event.type == ServerEventType.RESPONSE_DONE: logger.info("✅ Response complete") elif event.type == ServerEventType.ERROR: logger.error(f"❌ VoiceLive error: {event.error.message}") print(f"Error: {event.error.message}") elif event.type == ServerEventType.CONVERSATION_ITEM_CREATED: logger.debug(f"Conversation item created: {event.item.id}") else: logger.debug(f"Unhandled event type: {event.type}") def parse_arguments(): """Parse command line arguments.""" parser = argparse.ArgumentParser( description="Basic Voice Assistant using Azure VoiceLive SDK", formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) parser.add_argument( "--api-key", help="Azure VoiceLive API key. If not provided, will use AZURE_VOICELIVE_API_KEY environment variable.", type=str, default=os.environ.get("AZURE_VOICELIVE_API_KEY"), ) parser.add_argument( "--endpoint", help="Azure VoiceLive endpoint", type=str, default=os.environ.get("AZURE_VOICELIVE_ENDPOINT", "wss://api.voicelive.com/v1"), ) parser.add_argument( "--model", help="VoiceLive model to use", type=str, default=os.environ.get("AZURE_VOICELIVE_MODEL", "gpt-realtime"), ) parser.add_argument( "--voice", help="Voice to use for the assistant. E.g. alloy, echo, fable, en-US-AvaNeural, en-US-GuyNeural", type=str, default=os.environ.get("AZURE_VOICELIVE_VOICE", "en-US-Ava:DragonHDLatestNeural"), ) parser.add_argument( "--instructions", help="System instructions for the AI assistant", type=str, default=os.environ.get( "AZURE_VOICELIVE_INSTRUCTIONS", "You are a helpful AI assistant. Respond naturally and conversationally. " "Keep your responses concise but engaging.", ), ) parser.add_argument( "--use-token-credential", help="Use Azure token credential instead of API key", action="store_true", default=True ) parser.add_argument("--verbose", help="Enable verbose logging", action="store_true") return parser.parse_args() async def main(): """Main function.""" args = parse_arguments() # Set logging level if args.verbose: logging.getLogger().setLevel(logging.DEBUG) # Validate credentials if not args.api_key and not args.use_token_credential: print("❌ Error: No authentication provided") print("Please provide an API key using --api-key or set AZURE_VOICELIVE_API_KEY environment variable,") print("or use --use-token-credential for Azure authentication.") sys.exit(1) try: # Create client with appropriate credential credential: Union[AzureKeyCredential, TokenCredential] if args.use_token_credential: credential = InteractiveBrowserCredential() # or DefaultAzureCredential() if needed logger.info("Using Azure token credential") else: credential = AzureKeyCredential(args.api_key) logger.info("Using API key credential") # Create and start voice assistant assistant = BasicVoiceAssistant( endpoint=args.endpoint, credential=credential, model=args.model, voice=args.voice, instructions=args.instructions, ) # Setup signal handlers for graceful shutdown def signal_handler(sig, frame): logger.info("Received shutdown signal") raise KeyboardInterrupt() signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) # Start the assistant await assistant.start() except KeyboardInterrupt: print("\n👋 Voice assistant shut down. Goodbye!") except Exception as e: logger.error(f"Fatal error: {e}") print(f"❌ Error: {e}") sys.exit(1) if __name__ == "__main__": # Check for required dependencies dependencies = { "pyaudio": "Audio processing", "azure.ai.voicelive": "Azure VoiceLive SDK", "azure.core": "Azure Core libraries", } missing_deps = [] for dep, description in dependencies.items(): try: __import__(dep.replace("-", "_")) except ImportError: missing_deps.append(f"{dep} ({description})") if missing_deps: print("❌ Missing required dependencies:") for dep in missing_deps: print(f" - {dep}") print("\nInstall with: pip install azure-ai-voicelive pyaudio python-dotenv") sys.exit(1) # Check audio system try: p = pyaudio.PyAudio() # Check for input devices input_devices = [ i for i in range(p.get_device_count()) if cast(Union[int, float], p.get_device_info_by_index(i).get("maxInputChannels", 0) or 0) > 0 ] # Check for output devices output_devices = [ i for i in range(p.get_device_count()) if cast(Union[int, float], p.get_device_info_by_index(i).get("maxOutputChannels", 0) or 0) > 0 ] p.terminate() if not input_devices: print("❌ No audio input devices found. Please check your microphone.") sys.exit(1) if not output_devices: print("❌ No audio output devices found. Please check your speakers.") sys.exit(1) except Exception as e: print(f"❌ Audio system check failed: {e}") sys.exit(1) print("🎙️ Basic Voice Assistant with Azure VoiceLive SDK") print("=" * 50) # Run the assistant asyncio.run(main())使用以下命令登录到 Azure:
az login运行该 Python 文件。
python voice-live-quickstart.pyVoice Live API 开始根据模型的初始响应返回音频。 可以通过说话来中断模型。 输入“q”退出对话。
输出
脚本的输出将打印到控制台。 你会看到指示系统状态的消息。 音频通过扬声器或耳机播放。
============================================================
🎤 VOICE ASSISTANT READY
Start speaking to begin conversation
Press Ctrl+C to exit
============================================================
🎤 Listening...
🤔 Processing...
🎤 Ready for next input...
🎤 Listening...
🤔 Processing...
🎤 Ready for next input...
🎤 Listening...
🤔 Processing...
🎤 Ready for next input...
🎤 Listening...
🤔 Processing...
🎤 Listening...
🎤 Ready for next input...
🤔 Processing...
🎤 Ready for next input...
运行的脚本将创建一个在文件夹中命名 <timestamp>_voicelive.log 的 logs 日志文件。
默认日志级别设置为INFO,但您可以通过命令行参数--verbose在启动时修改,或者通过如下所示的代码更新日志配置来更改日志级别:
logging.basicConfig(
filename=f'logs/{timestamp}_voicelive.log',
filemode="w",
level=logging.DEBUG,
format='%(asctime)s:%(name)s:%(levelname)s:%(message)s'
日志文件包含有关与语音实时 API 的连接的信息,包括请求和响应数据。 可以查看日志文件以查看聊天的详细信息。
2025-10-02 14:47:37,901:__main__:INFO:Using Azure token credential
2025-10-02 14:47:37,901:__main__:INFO:Connecting to VoiceLive API with model gpt-realtime
2025-10-02 14:47:37,901:azure.core.pipeline.policies.http_logging_policy:INFO:Request URL: 'https://login.microsoftonline.com/organizations/v2.0/.well-known/openid-configuration'
Request method: 'GET'
Request headers:
'User-Agent': 'azsdk-python-identity/1.22.0 Python/3.11.9 (Windows-10-10.0.26200-SP0)'
No body was attached to the request
2025-10-02 14:47:38,057:azure.core.pipeline.policies.http_logging_policy:INFO:Response status: 200
Response headers:
'Date': 'Thu, 02 Oct 2025 21:47:37 GMT'
'Content-Type': 'application/json; charset=utf-8'
'Content-Length': '1641'
'Connection': 'keep-alive'
'Cache-Control': 'max-age=86400, private'
'Strict-Transport-Security': 'REDACTED'
'X-Content-Type-Options': 'REDACTED'
'Access-Control-Allow-Origin': 'REDACTED'
'Access-Control-Allow-Methods': 'REDACTED'
'P3P': 'REDACTED'
'x-ms-request-id': 'f81adfa1-8aa3-4ab6-a7b8-908f411e0d00'
'x-ms-ests-server': 'REDACTED'
'x-ms-srs': 'REDACTED'
'Content-Security-Policy-Report-Only': 'REDACTED'
'Cross-Origin-Opener-Policy-Report-Only': 'REDACTED'
'Reporting-Endpoints': 'REDACTED'
'X-XSS-Protection': 'REDACTED'
'Set-Cookie': 'REDACTED'
'X-Cache': 'REDACTED'
2025-10-02 14:47:42,105:azure.core.pipeline.policies.http_logging_policy:INFO:Request URL: 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token'
Request method: 'POST'
Request headers:
'Accept': 'application/json'
'x-client-sku': 'REDACTED'
'x-client-ver': 'REDACTED'
'x-client-os': 'REDACTED'
'x-ms-lib-capability': 'REDACTED'
'client-request-id': 'REDACTED'
'x-client-current-telemetry': 'REDACTED'
'x-client-last-telemetry': 'REDACTED'
'X-AnchorMailbox': 'REDACTED'
'User-Agent': 'azsdk-python-identity/1.22.0 Python/3.11.9 (Windows-10-10.0.26200-SP0)'
A body is sent with the request
2025-10-02 14:47:42,466:azure.core.pipeline.policies.http_logging_policy:INFO:Response status: 200
Response headers:
'Date': 'Thu, 02 Oct 2025 21:47:42 GMT'
'Content-Type': 'application/json; charset=utf-8'
'Content-Length': '6587'
'Connection': 'keep-alive'
'Cache-Control': 'no-store, no-cache'
'Pragma': 'no-cache'
'Expires': '-1'
'Strict-Transport-Security': 'REDACTED'
'X-Content-Type-Options': 'REDACTED'
'P3P': 'REDACTED'
'client-request-id': 'REDACTED'
'x-ms-request-id': '2e82e728-22c0-4568-b3ed-f00ec79a2500'
'x-ms-ests-server': 'REDACTED'
'x-ms-clitelem': 'REDACTED'
'x-ms-srs': 'REDACTED'
'Content-Security-Policy-Report-Only': 'REDACTED'
'Cross-Origin-Opener-Policy-Report-Only': 'REDACTED'
'Reporting-Endpoints': 'REDACTED'
'X-XSS-Protection': 'REDACTED'
'Set-Cookie': 'REDACTED'
'X-Cache': 'REDACTED'
2025-10-02 14:47:42,467:azure.identity._internal.interactive:INFO:InteractiveBrowserCredential.get_token succeeded
2025-10-02 14:47:42,884:__main__:INFO:AudioProcessor initialized with 24kHz PCM16 mono audio
2025-10-02 14:47:42,884:__main__:INFO:Setting up voice conversation session...
2025-10-02 14:47:42,887:__main__:INFO:Session configuration sent
2025-10-02 14:47:42,943:__main__:INFO:Audio playback system ready
2025-10-02 14:47:42,943:__main__:INFO:Voice assistant ready! Start speaking...
2025-10-02 14:47:42,975:__main__:INFO:Session ready: sess_CMLRGjWnakODcHn583fXf
2025-10-02 14:47:42,994:__main__:INFO:Started audio capture
2025-10-02 14:47:47,513:__main__:INFO:\U0001f3a4 User started speaking - stopping playback
2025-10-02 14:47:47,593:__main__:INFO:Stopped audio playback
2025-10-02 14:47:51,757:__main__:INFO:\U0001f3a4 User stopped speaking
2025-10-02 14:47:51,813:__main__:INFO:Audio playback system ready
2025-10-02 14:47:51,816:__main__:INFO:\U0001f916 Assistant response created
2025-10-02 14:47:58,009:__main__:INFO:\U0001f916 Assistant finished speaking
2025-10-02 14:47:58,009:__main__:INFO:\u2705 Response complete
2025-10-02 14:48:07,309:__main__:INFO:Received shutdown signal
本文介绍如何使用适用于 C# 的 VoiceLive SDK 通过 Azure AI Foundry 模型实时使用 Azure AI 语音语音。
创建并运行一个应用程序,以将语音实时直接与实时语音代理的生成式 AI 模型配合使用。
使用模型直接允许为每个会话指定自定义指令(提示),为动态或实验用例提供更大的灵活性。
如果希望对会话参数进行精细控制,或者需要频繁调整提示或配置,而无需在门户中更新代理,则模型可能更可取。
基于模型的会话的代码在某些方面更简单,因为它不需要管理代理 ID 或特定于代理的设置。
直接模型使用适用于不需要代理级抽象或内置逻辑的方案。
若要改用语音实时 API 和代理,请参阅 语音实时 API 代理快速入门。
先决条件
- 一份 Azure 订阅。 免费创建一个。
- 在一个受支持的区域中创建的 Azure AI Foundry 资源 。 有关区域可用性的详细信息,请参阅 语音实时概述文档。
- 已安装 .NET SDK 版本 6.0 或更高版本。
启动语音对话
按照以下步骤创建控制台应用程序并安装语音 SDK。
在需要新项目的文件夹中打开命令提示符窗口。 运行以下命令,使用 .NET CLI 创建控制台应用程序。
dotnet new console该命令会在你的项目目录中创建 Program.cs 文件。
使用 .NET CLI 在新项目中安装 Voice Live SDK。
dotnet add package Azure.AI.VoiceLive --prerelease在要在其中运行代码的文件夹中创建一
appsettings.json个名为的新文件。 在该文件中,添加以下 JSON 内容:{ "VoiceLive": { "ApiKey": "YOUR_VOICELIVE_API_KEY", "Endpoint": "wss://api.voicelive.com/v1", "Model": "gpt-realtime", "Voice": "en-US-Ava:DragonHDLatestNeural", "Instructions": "You are a professional customer service representative for TechCorp. You have access to customer databases and order systems. Always be polite, helpful, and efficient. When customers ask about orders, accounts, or need to schedule service, use the available tools to provide accurate, real-time information. Keep your responses concise but thorough." }, "Logging": { "LogLevel": { "Default": "Information", "Azure.AI.VoiceLive": "Debug" } } }将
ApiKey值替换为 AI Foundry API 密钥,并将Endpoint值替换为资源终结点。 还可以根据需要更改模型、语音和说明值。将
Program.cs的内容替换为以下代码。 此代码创建一个示例客户服务机器人。 有关更多详细信息,请参阅 GitHub 上的示例。// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System.CommandLine; using Azure.Core; using Azure.Core.Pipeline; using Azure.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using System.Text.Json; using NAudio.Wave; using Azure.AI.VoiceLive; using static SampleData; public static class FunctionModels { /// <summary> /// Parameters for checking order status. /// </summary> public class CheckOrderStatusArgs { /// <summary> /// The order number to check status for. /// </summary> public string order_number { get; set; } = string.Empty; /// <summary> /// Customer email. /// </summary> public string? email { get; set; } } /// <summary> /// Parameters for getting customer information. /// </summary> public class GetCustomerInfoArgs { /// <summary> /// Customer ID to retrieve information for. /// </summary> public string customer_id { get; set; } = string.Empty; /// <summary> /// Whether to include order history in the response. /// </summary> public bool include_history { get; set; } = false; } /// <summary> /// Parameters for scheduling support calls. /// </summary> public class ScheduleSupportCallArgs { /// <summary> /// The customer ID to schedule the call for. /// </summary> public string customer_id { get; set; } = string.Empty; /// <summary> /// The preferred time for the support call. /// </summary> public string? preferred_time { get; set; } /// <summary> /// The category of the support issue. /// </summary> public string issue_category { get; set; } = string.Empty; /// <summary> /// The urgency level of the support issue. /// </summary> public string urgency { get; set; } = "medium"; /// <summary> /// A brief description of the issue. /// </summary> public string description { get; set; } = string.Empty; } /// <summary> /// Parameters for initiating return process. /// </summary> public class InitiateReturnProcessArgs { /// <summary> /// The order number for which the return is requested. /// </summary> public string order_number { get; set; } = string.Empty; /// <summary> /// The product ID to be returned. /// </summary> public string product_id { get; set; } = string.Empty; /// <summary> /// The reason for the return. /// </summary> public string reason { get; set; } = string.Empty; /// <summary> /// The return method preferred by the customer. /// </summary> public string return_type { get; set; } = string.Empty; } /// <summary> /// Address information for shipping updates. /// </summary> public class Address { /// <summary> /// Street address for shipping. /// </summary> public string street { get; set; } = string.Empty; /// <summary> /// City for shipping address. /// </summary> public string city { get; set; } = string.Empty; /// <summary> /// state for shipping address. /// </summary> public string state { get; set; } = string.Empty; /// <summary> /// zip code for shipping address. /// </summary> public string zip_code { get; set; } = string.Empty; /// <summary> /// Country/region for shipping address. /// </summary> public string country_region { get; set; } = "US"; } /// <summary> /// Parameters for updating shipping address. /// </summary> public class UpdateShippingAddressArgs { /// <summary> /// Order number for which the address needs to be updated. /// </summary> public string order_number { get; set; } = string.Empty; /// <summary> /// New shipping address. /// </summary> public Address new_address { get; set; } = new(); } } /// <summary> /// Sample data models for demonstration purposes. /// </summary> public static class SampleData { /// <summary> /// Sample order information. /// </summary> public class Order { /// <summary> /// The unique identifier for the order. /// </summary> public string OrderNumber { get; set; } = string.Empty; /// <summary> /// Status of the order. /// </summary> public string Status { get; set; } = string.Empty; /// <summary> /// Total amount for the order. /// </summary> public decimal TotalAmount { get; set; } /// <summary> /// The items included in the order. /// </summary> public List<OrderItem> Items { get; set; } = new(); /// <summary> /// Estimated delivery date for the order. /// </summary> public DateTime? EstimatedDelivery { get; set; } /// <summary> /// Shipping tracking number, if available. /// </summary> public string? TrackingNumber { get; set; } /// <summary> /// Theustomer ID associated with the order. /// </summary> public string CustomerId { get; set; } = string.Empty; /// <summary> /// When the order was created. /// </summary> public DateTime CreatedAt { get; set; } } /// <summary> /// Sample order item information. /// </summary> public class OrderItem { /// <summary> /// The unique identifier for the product. /// </summary> public string ProductId { get; set; } = string.Empty; /// <summary> /// Name of the product. /// </summary> public string ProductName { get; set; } = string.Empty; /// <summary> /// quantity of the product ordered. /// </summary> public int Quantity { get; set; } /// <summary> /// status of the order item. /// </summary> public string Status { get; set; } = string.Empty; /// <summary> /// price of the product. /// </summary> public decimal Price { get; set; } } /// <summary> /// Sample customer information. /// </summary> public class Customer { /// <summary> /// customer ID. /// </summary> public string CustomerId { get; set; } = string.Empty; /// <summary> /// preferred name of the customer. /// </summary> public string Name { get; set; } = string.Empty; /// <summary> /// customer's email address. /// </summary> public string Email { get; set; } = string.Empty; /// <summary> /// phone number of the customer. /// </summary> public string? Phone { get; set; } /// <summary> /// customer's pricing tier. /// </summary> public string Tier { get; set; } = "Standard"; /// <summary> /// When the customer was created. /// </summary> public DateTime CreatedAt { get; set; } } /// <summary> /// Sample support ticket information. /// </summary> public class SupportTicket { /// <summary> /// ID of the support ticket. /// </summary> public string TicketId { get; set; } = string.Empty; /// <summary> /// customer ID associated with the ticket. /// </summary> public string CustomerId { get; set; } = string.Empty; /// <summary> /// category of the support issue. /// </summary> public string Category { get; set; } = string.Empty; /// <summary> /// urgency level of the support issue. /// </summary> public string Urgency { get; set; } = string.Empty; /// <summary> /// brief description of the issue. /// </summary> public string Description { get; set; } = string.Empty; /// <summary> /// time when the ticket was created. /// </summary> public DateTime ScheduledTime { get; set; } /// <summary> /// current status of the support ticket. /// </summary> public string Status { get; set; } = "Scheduled"; } } public interface ICustomerServiceFunctions { /// <summary> /// Executes a function by name with JSON arguments. /// </summary> /// <param name="functionName"></param> /// <param name="argumentsJson"></param> /// <param name="cancellationToken"></param> /// <returns></returns> Task<object> ExecuteFunctionAsync(string functionName, string argumentsJson, CancellationToken cancellationToken = default); } /// <summary> /// Implementation of customer service functions with mock data for demonstration. /// In a real implementation, these would connect to actual databases and services. /// </summary> public class CustomerServiceFunctions : ICustomerServiceFunctions { private readonly ILogger<CustomerServiceFunctions> _logger; private readonly Dictionary<string, Order> _orders; private readonly Dictionary<string, Customer> _customers; private readonly List<SupportTicket> _supportTickets; /// <summary> /// Constructor for CustomerServiceFunctions. /// </summary> /// <param name="logger"></param> /// <exception cref="ArgumentNullException"></exception> public CustomerServiceFunctions(ILogger<CustomerServiceFunctions> logger) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); // Initialize sample data _orders = InitializeSampleOrders(); _customers = InitializeSampleCustomers(); _supportTickets = new List<SupportTicket>(); } /// <summary> /// Execute a function by name with JSON arguments. /// </summary> public async Task<object> ExecuteFunctionAsync(string functionName, string argumentsJson, CancellationToken cancellationToken = default) { try { _logger.LogInformation("Executing function: {FunctionName} with arguments: {Arguments}", functionName, argumentsJson); return functionName switch { "check_order_status" => await CheckOrderStatusAsync(argumentsJson, cancellationToken).ConfigureAwait(false), "get_customer_info" => await GetCustomerInfoAsync(argumentsJson, cancellationToken).ConfigureAwait(false), "schedule_support_call" => await ScheduleSupportCallAsync(argumentsJson, cancellationToken).ConfigureAwait(false), "initiate_return_process" => await InitiateReturnProcessAsync(argumentsJson, cancellationToken).ConfigureAwait(false), "update_shipping_address" => await UpdateShippingAddressAsync(argumentsJson, cancellationToken).ConfigureAwait(false), _ => new { success = false, error = $"Unknown function: {functionName}" } }; } catch (Exception ex) { _logger.LogError(ex, "Error executing function {FunctionName}", functionName); return new { success = false, error = "Internal error occurred while processing your request." }; } } private async Task<object> CheckOrderStatusAsync(string argumentsJson, CancellationToken cancellationToken) { var args = JsonSerializer.Deserialize<FunctionModels.CheckOrderStatusArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (args == null) { return new { success = false, message = "Invalid arguments provided." }; } // Simulate async database lookup await Task.Delay(100, cancellationToken).ConfigureAwait(false); if (!_orders.TryGetValue(args.order_number, out var order)) { return new { success = false, message = "Order not found. Please verify the order number and try again." }; } return new { success = true, order = new { number = order.OrderNumber, status = order.Status, total = order.TotalAmount, items = order.Items.Select(item => new { name = item.ProductName, quantity = item.Quantity, status = item.Status, price = item.Price }), estimated_delivery = order.EstimatedDelivery?.ToString("yyyy-MM-dd"), tracking = order.TrackingNumber, order_date = order.CreatedAt.ToString("yyyy-MM-dd") } }; } private async Task<object> GetCustomerInfoAsync(string argumentsJson, CancellationToken cancellationToken) { var args = JsonSerializer.Deserialize<FunctionModels.GetCustomerInfoArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (args == null) { return new { success = false, message = "Invalid arguments provided." }; } // Simulate async database lookup await Task.Delay(150, cancellationToken).ConfigureAwait(false); var customer = _customers.Values.FirstOrDefault(c => c.CustomerId == args.customer_id || c.Email.Equals(args.customer_id, StringComparison.OrdinalIgnoreCase)); if (customer == null) { return new { success = false, message = "Customer account not found. Please verify the customer ID or email address." }; } var result = new { success = true, customer = new { id = customer.CustomerId, name = customer.Name, email = customer.Email, phone = customer.Phone, tier = customer.Tier, joined_date = customer.CreatedAt.ToString("yyyy-MM-dd") } }; if (args.include_history) { var customerOrders = _orders.Values .Where(o => o.CustomerId == customer.CustomerId) .OrderByDescending(o => o.CreatedAt) .Take(5) .Select(order => new { number = order.OrderNumber, date = order.CreatedAt.ToString("yyyy-MM-dd"), total = order.TotalAmount, status = order.Status }); return new { result.success, customer = new { result.customer.id, result.customer.name, result.customer.email, result.customer.phone, result.customer.tier, result.customer.joined_date, recent_orders = customerOrders } }; } return result; } private async Task<object> ScheduleSupportCallAsync(string argumentsJson, CancellationToken cancellationToken) { var args = JsonSerializer.Deserialize<FunctionModels.ScheduleSupportCallArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (args == null) { return new { success = false, message = "Invalid arguments provided." }; } // Validate customer exists var customer = _customers.Values.FirstOrDefault(c => c.CustomerId == args.customer_id || c.Email.Equals(args.customer_id, StringComparison.OrdinalIgnoreCase)); if (customer == null) { return new { success = false, message = "Customer not found. Please verify the customer ID." }; } // Simulate async scheduling system await Task.Delay(200, cancellationToken).ConfigureAwait(false); // Parse preferred time or use default DateTime scheduledTime; if (!string.IsNullOrEmpty(args.preferred_time) && DateTime.TryParse(args.preferred_time, out scheduledTime)) { // Use provided time } else { // Default to next business day at 10 AM scheduledTime = DateTime.Today.AddDays(1); if (scheduledTime.DayOfWeek == DayOfWeek.Saturday) scheduledTime = scheduledTime.AddDays(2); if (scheduledTime.DayOfWeek == DayOfWeek.Sunday) scheduledTime = scheduledTime.AddDays(1); scheduledTime = scheduledTime.AddHours(10); } var ticket = new SupportTicket { TicketId = $"TICKET-{DateTime.Now:yyyyMMdd}-{Random.Shared.Next(1000, 9999)}", CustomerId = customer.CustomerId, Category = args.issue_category, Urgency = args.urgency, Description = args.description, ScheduledTime = scheduledTime, Status = "Scheduled" }; _supportTickets.Add(ticket); return new { success = true, ticket = new { ticket_id = ticket.TicketId, customer_name = customer.Name, scheduled_time = ticket.ScheduledTime.ToString("yyyy-MM-dd HH:mm"), category = ticket.Category, urgency = ticket.Urgency, description = ticket.Description, status = ticket.Status }, message = $"Support call scheduled for {ticket.ScheduledTime:yyyy-MM-dd HH:mm}. You will receive a confirmation email shortly." }; } private async Task<object> InitiateReturnProcessAsync(string argumentsJson, CancellationToken cancellationToken) { var args = JsonSerializer.Deserialize<FunctionModels.InitiateReturnProcessArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (args == null) { return new { success = false, message = "Invalid arguments provided." }; } // Simulate async database lookup await Task.Delay(150, cancellationToken).ConfigureAwait(false); if (!_orders.TryGetValue(args.order_number, out var order)) { return new { success = false, message = "Order not found. Please verify the order number." }; } var item = order.Items.FirstOrDefault(i => i.ProductId == args.product_id); if (item == null) { return new { success = false, message = "Product not found in this order." }; } // Check if return is eligible (within 30 days and not already returned) if (order.CreatedAt < DateTime.Now.AddDays(-30)) { return new { success = false, message = "This order is outside the 30-day return window." }; } if (item.Status == "Returned") { return new { success = false, message = "This item has already been returned." }; } var returnId = $"RTN-{DateTime.Now:yyyyMMdd}-{Random.Shared.Next(1000, 9999)}"; return new { success = true, return_info = new { return_id = returnId, order_number = order.OrderNumber, product_name = item.ProductName, return_type = args.return_type, reason = args.reason, refund_amount = args.return_type == "refund" ? item.Price : 0, estimated_processing = "3-5 business days", return_label_url = $"https://returns.techcorp.com/label/{returnId}" }, message = "Return request initiated successfully. You will receive a return shipping label via email within 24 hours." }; } private async Task<object> UpdateShippingAddressAsync(string argumentsJson, CancellationToken cancellationToken) { var args = JsonSerializer.Deserialize<FunctionModels.UpdateShippingAddressArgs>(argumentsJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); if (args == null) { return new { success = false, message = "Invalid arguments provided." }; } // Simulate async database lookup await Task.Delay(100, cancellationToken).ConfigureAwait(false); if (!_orders.TryGetValue(args.order_number, out var order)) { return new { success = false, message = "Order not found. Please verify the order number." }; } // Check if order can be modified if (order.Status == "Delivered" || order.Status == "Shipped") { return new { success = false, message = $"Cannot update address for {order.Status.ToLower()} orders." }; } return new { success = true, updated_order = new { order_number = order.OrderNumber, status = order.Status, new_shipping_address = new { args.new_address.street, args.new_address.city, args.new_address.state, args.new_address.zip_code, args.new_address.country_region }, estimated_delivery = order.EstimatedDelivery?.AddDays(1).ToString("yyyy-MM-dd") // Adjust delivery date }, message = "Shipping address updated successfully. Your estimated delivery date may have changed." }; } private Dictionary<string, Order> InitializeSampleOrders() { return new Dictionary<string, Order> { ["ORD-2024-001"] = new Order { OrderNumber = "ORD-2024-001", Status = "Processing", TotalAmount = 299.99m, CustomerId = "CUST-001", CreatedAt = DateTime.Now.AddDays(-2), EstimatedDelivery = DateTime.Now.AddDays(3), TrackingNumber = "1Z999AA1234567890", Items = new List<OrderItem> { new() { ProductId = "LAPTOP-001", ProductName = "TechCorp Laptop Pro", Quantity = 1, Status = "Processing", Price = 299.99m } } }, ["ORD-2024-002"] = new Order { OrderNumber = "ORD-2024-002", Status = "Shipped", TotalAmount = 159.98m, CustomerId = "CUST-002", CreatedAt = DateTime.Now.AddDays(-5), EstimatedDelivery = DateTime.Now.AddDays(1), TrackingNumber = "1Z999AA1234567891", Items = new List<OrderItem> { new() { ProductId = "MOUSE-001", ProductName = "Wireless Gaming Mouse", Quantity = 1, Status = "Shipped", Price = 79.99m }, new() { ProductId = "PAD-001", ProductName = "Gaming Mouse Pad", Quantity = 1, Status = "Shipped", Price = 79.99m } } }, ["ORD-2024-003"] = new Order { OrderNumber = "ORD-2024-003", Status = "Delivered", TotalAmount = 499.99m, CustomerId = "CUST-001", CreatedAt = DateTime.Now.AddDays(-10), EstimatedDelivery = DateTime.Now.AddDays(-3), TrackingNumber = "1Z999AA1234567892", Items = new List<OrderItem> { new() { ProductId = "MONITOR-001", ProductName = "4K Gaming Monitor", Quantity = 1, Status = "Delivered", Price = 499.99m } } } }; } private Dictionary<string, Customer> InitializeSampleCustomers() { return new Dictionary<string, Customer> { ["CUST-001"] = new Customer { CustomerId = "CUST-001", Name = "John Smith", Email = "john.smith@email.com", Phone = "+1-555-0123", Tier = "Gold", CreatedAt = DateTime.Now.AddMonths(-8) }, ["CUST-002"] = new Customer { CustomerId = "CUST-002", Name = "Sarah Johnson", Email = "sarah.johnson@email.com", Phone = "+1-555-0124", Tier = "Silver", CreatedAt = DateTime.Now.AddMonths(-3) }, ["CUST-003"] = new Customer { CustomerId = "CUST-003", Name = "Mike Davis", Email = "mike.davis@email.com", Phone = "+1-555-0125", Tier = "Standard", CreatedAt = DateTime.Now.AddMonths(-1) } }; } } /// <summary> /// Customer service voice bot implementing function calling with the VoiceLive SDK. /// </summary> /// <remarks> /// This sample demonstrates how to build a sophisticated customer service bot that can: /// - Check order status and track shipments /// - Retrieve customer account information and history /// - Schedule technical support calls /// - Process returns and exchanges /// - Update shipping addresses for pending orders /// /// The bot uses strongly-typed function definitions and provides real-time voice interaction /// with proper interruption handling and error recovery. /// </remarks> public class CustomerServiceBot : IDisposable { private readonly VoiceLiveClient _client; private readonly string _model; private readonly string _voice; private readonly string _instructions; private readonly ICustomerServiceFunctions _functions; private readonly ILogger<CustomerServiceBot> _logger; private readonly ILoggerFactory _loggerFactory; private readonly HashSet<string> _assistantMessageItems = new HashSet<string>(); private readonly HashSet<string> _assistantMessageResponses = new HashSet<string>(); private VoiceLiveSession? _session; private AudioProcessor? _audioProcessor; private bool _disposed; /// <summary> /// Initializes a new instance of the CustomerServiceBot class. /// </summary> /// <param name="client">The VoiceLive client.</param> /// <param name="model">The model to use.</param> /// <param name="voice">The voice to use.</param> /// <param name="instructions">The system instructions.</param> /// <param name="functions">The customer service functions implementation.</param> /// <param name="loggerFactory">Logger factory for creating loggers.</param> public CustomerServiceBot( VoiceLiveClient client, string model, string voice, string instructions, ICustomerServiceFunctions functions, ILoggerFactory loggerFactory) { _client = client ?? throw new ArgumentNullException(nameof(client)); _model = model ?? throw new ArgumentNullException(nameof(model)); _voice = voice ?? throw new ArgumentNullException(nameof(voice)); _instructions = instructions ?? throw new ArgumentNullException(nameof(instructions)); _functions = functions ?? throw new ArgumentNullException(nameof(functions)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); _logger = loggerFactory.CreateLogger<CustomerServiceBot>(); } /// <summary> /// Start the customer service bot session. /// </summary> /// <param name="cancellationToken">Cancellation token for stopping the session.</param> public async Task StartAsync(CancellationToken cancellationToken = default) { try { _logger.LogInformation("Connecting to VoiceLive API with model {Model}", _model); // Start VoiceLive session _session = await _client.StartSessionAsync(_model, cancellationToken).ConfigureAwait(false); // Initialize audio processor _audioProcessor = new AudioProcessor(_session, _loggerFactory.CreateLogger<AudioProcessor>()); // Configure session for voice conversation with function calling await SetupSessionAsync(cancellationToken).ConfigureAwait(false); // Start audio systems await _audioProcessor.StartPlaybackAsync().ConfigureAwait(false); await _audioProcessor.StartCaptureAsync().ConfigureAwait(false); _logger.LogInformation("Customer service bot ready! Start speaking..."); Console.WriteLine(); Console.WriteLine("=" + new string('=', 69)); Console.WriteLine("🏢 CUSTOMER SERVICE BOT READY"); Console.WriteLine("I can help you with orders, returns, account info, and scheduling support calls"); Console.WriteLine("Start speaking to begin your customer service session"); Console.WriteLine("Press Ctrl+C to exit"); Console.WriteLine("=" + new string('=', 69)); Console.WriteLine(); // Process events await ProcessEventsAsync(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { _logger.LogInformation("Received cancellation signal, shutting down..."); } catch (Exception ex) { _logger.LogError(ex, "Connection error"); throw; } finally { // Cleanup if (_audioProcessor != null) { await _audioProcessor.CleanupAsync().ConfigureAwait(false); } } } /// <summary> /// Configure the VoiceLive session for customer service with function calling. /// </summary> private async Task SetupSessionAsync(CancellationToken cancellationToken) { _logger.LogInformation("Setting up customer service session with function calling..."); // Azure voice configuration var azureVoice = new AzureStandardVoice(_voice); // Create strongly typed turn detection configuration var turnDetectionConfig = new ServerVadTurnDetection { Threshold = 0.5f, PrefixPadding = TimeSpan.FromMilliseconds(300), SilenceDuration = TimeSpan.FromMilliseconds(500) }; // Create conversation session options with function tools var sessionOptions = new VoiceLiveSessionOptions { Model = _model, Instructions = _instructions, Voice = azureVoice, InputAudioFormat = InputAudioFormat.Pcm16, OutputAudioFormat = OutputAudioFormat.Pcm16, TurnDetection = turnDetectionConfig }; // Ensure modalities include audio sessionOptions.Modalities.Clear(); sessionOptions.Modalities.Add(InputModality.Text); sessionOptions.Modalities.Add(InputModality.Audio); // Add function tools for customer service operations sessionOptions.Tools.Add(CreateCheckOrderStatusTool()); sessionOptions.Tools.Add(CreateGetCustomerInfoTool()); sessionOptions.Tools.Add(CreateScheduleSupportCallTool()); sessionOptions.Tools.Add(CreateInitiateReturnProcessTool()); sessionOptions.Tools.Add(CreateUpdateShippingAddressTool()); await _session!.ConfigureSessionAsync(sessionOptions, cancellationToken).ConfigureAwait(false); _logger.LogInformation("Session configuration sent with {ToolCount} customer service tools", sessionOptions.Tools.Count); } /// <summary> /// Create the check order status function tool. /// </summary> private VoiceLiveFunctionDefinition CreateCheckOrderStatusTool() { var parameters = new { type = "object", properties = new { order_number = new { type = "string", description = "The customer's order number (required)" }, email = new { type = "string", description = "Customer's email address if order number is not available" } }, required = new[] { "order_number" } }; return new VoiceLiveFunctionDefinition("check_order_status") { Description = "Check the status of a customer order by order number or email. Use this when customers ask about their order status, shipping, or delivery information.", Parameters = BinaryData.FromObjectAsJson(parameters) }; } /// <summary> /// Create the get customer info function tool. /// </summary> private VoiceLiveFunctionDefinition CreateGetCustomerInfoTool() { var parameters = new { type = "object", properties = new { customer_id = new { type = "string", description = "Customer ID or email address to look up" }, include_history = new { type = "boolean", description = "Whether to include recent purchase history in the response", @default = false } }, required = new[] { "customer_id" } }; return new VoiceLiveFunctionDefinition("get_customer_info") { Description = "Retrieve customer account information and optionally their purchase history. Use this when customers ask about their account details or past orders.", Parameters = BinaryData.FromObjectAsJson(parameters) }; } /// <summary> /// Create the schedule support call function tool. /// </summary> private VoiceLiveFunctionDefinition CreateScheduleSupportCallTool() { var parameters = new { type = "object", properties = new { customer_id = new { type = "string", description = "Customer identifier (ID or email)" }, preferred_time = new { type = "string", description = "Preferred call time in ISO format (optional)" }, issue_category = new { type = "string", @enum = new[] { "technical", "billing", "warranty", "returns" }, description = "Category of the support issue" }, urgency = new { type = "string", @enum = new[] { "low", "medium", "high", "critical" }, description = "Urgency level of the issue", @default = "medium" }, description = new { type = "string", description = "Brief description of the issue the customer needs help with" } }, required = new[] { "customer_id", "issue_category", "description" } }; return new VoiceLiveFunctionDefinition("schedule_support_call") { Description = "Schedule a technical support call with a specialist. Use this when customers need to speak with a technical expert about complex issues.", Parameters = BinaryData.FromObjectAsJson(parameters) }; } /// <summary> /// Create the initiate return process function tool. /// </summary> private VoiceLiveFunctionDefinition CreateInitiateReturnProcessTool() { var parameters = new { type = "object", properties = new { order_number = new { type = "string", description = "Original order number for the return" }, product_id = new { type = "string", description = "Specific product ID to return from the order" }, reason = new { type = "string", @enum = new[] { "defective", "wrong_item", "not_satisfied", "damaged_shipping" }, description = "Reason for the return" }, return_type = new { type = "string", @enum = new[] { "refund", "exchange", "store_credit" }, description = "Type of return requested by the customer" } }, required = new[] { "order_number", "product_id", "reason", "return_type" } }; return new VoiceLiveFunctionDefinition("initiate_return_process") { Description = "Start the return/exchange process for a product. Use this when customers want to return or exchange items they've purchased.", Parameters = BinaryData.FromObjectAsJson(parameters) }; } /// <summary> /// Create the update shipping address function tool. /// </summary> private VoiceLiveFunctionDefinition CreateUpdateShippingAddressTool() { var parameters = new { type = "object", properties = new { order_number = new { type = "string", description = "Order number to update the shipping address for" }, new_address = new { type = "object", properties = new { street = new { type = "string", description = "Street address" }, city = new { type = "string", description = "City name" }, state = new { type = "string", description = "State or province" }, zip_code = new { type = "string", description = "ZIP or postal code" }, country_region = new { type = "string", description = "Country/region code", @default = "US" } }, required = new[] { "street", "city", "state", "zip_code" }, description = "New shipping address information" } }, required = new[] { "order_number", "new_address" } }; return new VoiceLiveFunctionDefinition("update_shipping_address") { Description = "Update shipping address for pending orders. Use this when customers need to change where their order will be delivered.", Parameters = BinaryData.FromObjectAsJson(parameters) }; } /// <summary> /// Process events from the VoiceLive session. /// </summary> private async Task ProcessEventsAsync(CancellationToken cancellationToken) { try { await foreach (SessionUpdate serverEvent in _session!.GetUpdatesAsync(cancellationToken).ConfigureAwait(false)) { await HandleSessionUpdateAsync(serverEvent, cancellationToken).ConfigureAwait(false); } } catch (OperationCanceledException) { _logger.LogInformation("Event processing cancelled"); } catch (Exception ex) { _logger.LogError(ex, "Error processing events"); throw; } } /// <summary> /// Handle different types of server events from VoiceLive. /// </summary> private async Task HandleSessionUpdateAsync(SessionUpdate serverEvent, CancellationToken cancellationToken) { _logger.LogDebug("Received event: {EventType}", serverEvent.GetType().Name); switch (serverEvent) { case SessionUpdateSessionCreated sessionCreated: await HandleSessionCreatedAsync(sessionCreated, cancellationToken).ConfigureAwait(false); break; case SessionUpdateSessionUpdated sessionUpdated: _logger.LogInformation("Session updated successfully with function tools"); // Start audio capture once session is ready if (_audioProcessor != null) { await _audioProcessor.StartCaptureAsync().ConfigureAwait(false); } break; case SessionUpdateInputAudioBufferSpeechStarted speechStarted: _logger.LogInformation("🎤 Customer started speaking - stopping playback"); Console.WriteLine("🎤 Listening..."); // Stop current assistant audio playback (interruption handling) if (_audioProcessor != null) { await _audioProcessor.StopPlaybackAsync().ConfigureAwait(false); } // Cancel any ongoing response try { await _session!.CancelResponseAsync(cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogDebug(ex, "No response to cancel"); } break; case SessionUpdateInputAudioBufferSpeechStopped speechStopped: _logger.LogInformation("🎤 Customer stopped speaking"); Console.WriteLine("🤔 Processing..."); // Restart playback system for response if (_audioProcessor != null) { await _audioProcessor.StartPlaybackAsync().ConfigureAwait(false); } break; case SessionUpdateResponseCreated responseCreated: _logger.LogInformation("🤖 Assistant response created"); break; case SessionUpdateResponseOutputItemAdded outputItemAdded: await HandleResponseOutputItemAddedAsync(outputItemAdded, cancellationToken).ConfigureAwait(false); break; case SessionUpdateResponseAudioDelta audioDelta: // Stream audio response to speakers _logger.LogDebug("Received audio delta"); if (audioDelta.Delta != null && _audioProcessor != null) { byte[] audioData = audioDelta.Delta.ToArray(); await _audioProcessor.QueueAudioAsync(audioData).ConfigureAwait(false); } break; case SessionUpdateResponseAudioDone audioDone: _logger.LogInformation("🤖 Assistant finished speaking"); Console.WriteLine("🎤 Ready for next customer inquiry..."); break; case SessionUpdateResponseContentPartAdded partAdded: if (_assistantMessageItems.Contains(partAdded.ItemId)) { _assistantMessageResponses.Add(partAdded.ResponseId); } break; case SessionUpdateResponseDone responseDone: _logger.LogInformation("✅ Response complete"); break; case SessionUpdateResponseFunctionCallArgumentsDone functionCallArgumentsDone: _logger.LogInformation("🔧 Function call arguments done for call ID: {CallId}", functionCallArgumentsDone.CallId); await HandleFunctionCallAsync(functionCallArgumentsDone.Name, functionCallArgumentsDone.CallId, functionCallArgumentsDone.Arguments, cancellationToken).ConfigureAwait(false); break; case SessionUpdateResponseAudioTranscriptDelta transcriptDelta: // For now, only deal with the assistant responses. if (_assistantMessageResponses.Contains(transcriptDelta.ResponseId)) { Console.Write($"{transcriptDelta.Delta}"); } break; case SessionUpdateResponseAudioTranscriptDone transcriptDone: // For now, only deal with the assistant responses. if (_assistantMessageResponses.Contains(transcriptDone.ResponseId)) { Console.WriteLine(); } break; case SessionUpdateError errorEvent: _logger.LogError("❌ VoiceLive error: {ErrorMessage}", errorEvent.Error?.Message); Console.WriteLine($"Error: {errorEvent.Error?.Message}"); break; default: _logger.LogDebug("Unhandled event type: {EventType}", serverEvent.GetType().Name); break; } } /// <summary> /// Handle response output item added events, including function calls. /// </summary> private async Task HandleResponseOutputItemAddedAsync(SessionUpdateResponseOutputItemAdded outputItemAdded, CancellationToken cancellationToken) { if (outputItemAdded.Item is ResponseFunctionCallItem item) { // This is a function call item, extract the details var functionName = item.Name; var callId = item.CallId; var arguments = item.Arguments; if (!string.IsNullOrEmpty(functionName) && !string.IsNullOrEmpty(callId) && !string.IsNullOrEmpty(arguments)) { await HandleFunctionCallAsync(functionName, callId, arguments, cancellationToken).ConfigureAwait(false); } else { _logger.LogWarning("Function call item missing required properties: Name={Name}, CallId={CallId}, Arguments={Arguments}", functionName, callId, arguments); } } else if (outputItemAdded.Item is ResponseMessageItem messageItem && messageItem.Role == ResponseMessageRole.Assistant) { // Keep track of the items that are from the assistant, so we know how to display the conversation. _assistantMessageItems.Add(messageItem.Id); } } /// <summary> /// Handle function call execution and send results back to the session. /// </summary> private async Task HandleFunctionCallAsync(string functionName, string callId, string arguments, CancellationToken cancellationToken) { _logger.LogInformation("🔧 Executing function: {FunctionName}", functionName); Console.WriteLine($"🔧 Looking up {functionName.Replace("_", " ")}..."); try { // Execute the function through our business logic layer var result = await _functions.ExecuteFunctionAsync(functionName, arguments, cancellationToken).ConfigureAwait(false); // Create function call output item using the model factory var outputItem = new FunctionCallOutputItem(callId, JsonSerializer.Serialize(result)); // Add the result to the conversation await _session!.AddItemAsync(outputItem, cancellationToken).ConfigureAwait(false); await _session!.StartResponseAsync(cancellationToken).ConfigureAwait(false); _logger.LogInformation("✅ Function {FunctionName} completed successfully", functionName); } catch (Exception ex) { _logger.LogError(ex, "❌ Function {FunctionName} execution failed", functionName); // Send error response var errorResult = new { success = false, error = "I'm sorry, I'm having trouble accessing that information right now. Please try again in a moment." }; var outputItem = new FunctionCallOutputItem(callId, JsonSerializer.Serialize(errorResult)); await _session!.AddItemAsync(outputItem, cancellationToken).ConfigureAwait(false); } } /// <summary> /// Handle session created event. /// </summary> private async Task HandleSessionCreatedAsync(SessionUpdateSessionCreated sessionCreated, CancellationToken cancellationToken) { _logger.LogInformation("Session ready: {SessionId}", sessionCreated.Session?.Id); // Start audio capture once session is ready if (_audioProcessor != null) { await _audioProcessor.StartCaptureAsync().ConfigureAwait(false); } } /// <summary> /// Dispose of resources. /// </summary> public void Dispose() { if (_disposed) return; _audioProcessor?.Dispose(); _session?.Dispose(); _disposed = true; } } /// <summary> /// Handles real-time audio capture and playback for the voice assistant. /// </summary> /// <remarks> /// Threading Architecture: /// - Main thread: Event loop and UI /// - Capture thread: NAudio input stream reading /// - Send thread: Async audio data transmission to VoiceLive /// - Playback thread: NAudio output stream writing /// </remarks> public class AudioProcessor : IDisposable { private readonly VoiceLiveSession _session; private readonly ILogger<AudioProcessor> _logger; // Audio configuration - PCM16, 24kHz, mono as specified private const int SampleRate = 24000; private const int Channels = 1; private const int BitsPerSample = 16; // NAudio components private WaveInEvent? _waveIn; private WaveOutEvent? _waveOut; private BufferedWaveProvider? _playbackBuffer; // Audio capture and playback state private bool _isCapturing; private bool _isPlaying; // Audio streaming channels private readonly Channel<byte[]> _audioSendChannel; private readonly Channel<byte[]> _audioPlaybackChannel; private readonly ChannelWriter<byte[]> _audioSendWriter; private readonly ChannelReader<byte[]> _audioSendReader; private readonly ChannelWriter<byte[]> _audioPlaybackWriter; private readonly ChannelReader<byte[]> _audioPlaybackReader; // Background tasks private Task? _audioSendTask; private Task? _audioPlaybackTask; private readonly CancellationTokenSource _cancellationTokenSource; private CancellationTokenSource _playbackCancellationTokenSource; /// <summary> /// Initializes a new instance of the AudioProcessor class. /// </summary> /// <param name="session">The VoiceLive session for audio communication.</param> /// <param name="logger">Logger for diagnostic information.</param> public AudioProcessor(VoiceLiveSession session, ILogger<AudioProcessor> logger) { _session = session ?? throw new ArgumentNullException(nameof(session)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); // Create unbounded channels for audio data _audioSendChannel = Channel.CreateUnbounded<byte[]>(); _audioSendWriter = _audioSendChannel.Writer; _audioSendReader = _audioSendChannel.Reader; _audioPlaybackChannel = Channel.CreateUnbounded<byte[]>(); _audioPlaybackWriter = _audioPlaybackChannel.Writer; _audioPlaybackReader = _audioPlaybackChannel.Reader; _cancellationTokenSource = new CancellationTokenSource(); _playbackCancellationTokenSource = new CancellationTokenSource(); _logger.LogInformation("AudioProcessor initialized with {SampleRate}Hz PCM16 mono audio", SampleRate); } /// <summary> /// Start capturing audio from microphone. /// </summary> public Task StartCaptureAsync() { if (_isCapturing) return Task.CompletedTask; _isCapturing = true; try { _waveIn = new WaveInEvent { WaveFormat = new WaveFormat(SampleRate, BitsPerSample, Channels), BufferMilliseconds = 50 // 50ms buffer for low latency }; _waveIn.DataAvailable += OnAudioDataAvailable; _waveIn.RecordingStopped += OnRecordingStopped; /* _logger.LogInformation($"There are {WaveIn.DeviceCount} devices available."); for (int i = 0; i < WaveIn.DeviceCount; i++) { var deviceInfo = WaveIn.GetCapabilities(i); _logger.LogInformation($"{i}: {deviceInfo.ProductName}"); } */ _waveIn.DeviceNumber = 0; // Default to first device _waveIn.StartRecording(); // Start audio send task _audioSendTask = ProcessAudioSendAsync(_cancellationTokenSource.Token); _logger.LogInformation("Started audio capture"); return Task.CompletedTask; } catch (Exception ex) { _logger.LogError(ex, "Failed to start audio capture"); _isCapturing = false; throw; } } /// <summary> /// Stop capturing audio. /// </summary> public async Task StopCaptureAsync() { if (!_isCapturing) return; _isCapturing = false; if (_waveIn != null) { _waveIn.StopRecording(); _waveIn.DataAvailable -= OnAudioDataAvailable; _waveIn.RecordingStopped -= OnRecordingStopped; _waveIn.Dispose(); _waveIn = null; } // Complete the send channel and wait for the send task _audioSendWriter.TryComplete(); if (_audioSendTask != null) { await _audioSendTask.ConfigureAwait(false); _audioSendTask = null; } _logger.LogInformation("Stopped audio capture"); } /// <summary> /// Initialize audio playback system. /// </summary> public Task StartPlaybackAsync() { if (_isPlaying) return Task.CompletedTask; _isPlaying = true; try { _waveOut = new WaveOutEvent { DesiredLatency = 100 // 100ms latency }; _playbackBuffer = new BufferedWaveProvider(new WaveFormat(SampleRate, BitsPerSample, Channels)) { BufferDuration = TimeSpan.FromMinutes(5), // 5 second buffer DiscardOnBufferOverflow = true }; _waveOut.Init(_playbackBuffer); _waveOut.Play(); _playbackCancellationTokenSource = new CancellationTokenSource(); // Start audio playback task _audioPlaybackTask = ProcessAudioPlaybackAsync(); _logger.LogInformation("Audio playback system ready"); return Task.CompletedTask; } catch (Exception ex) { _logger.LogError(ex, "Failed to initialize audio playback"); _isPlaying = false; throw; } } /// <summary> /// Stop audio playback and clear buffer. /// </summary> public async Task StopPlaybackAsync() { if (!_isPlaying) return; _isPlaying = false; // Clear the playback channel while (_audioPlaybackReader.TryRead(out _)) { } if (_playbackBuffer != null) { _playbackBuffer.ClearBuffer(); } if (_waveOut != null) { _waveOut.Stop(); _waveOut.Dispose(); _waveOut = null; } _playbackBuffer = null; // Complete the playback channel and wait for the playback task _playbackCancellationTokenSource.Cancel(); if (_audioPlaybackTask != null) { await _audioPlaybackTask.ConfigureAwait(false); _audioPlaybackTask = null; } _logger.LogInformation("Stopped audio playback"); } /// <summary> /// Queue audio data for playback. /// </summary> /// <param name="audioData">The audio data to queue.</param> public async Task QueueAudioAsync(byte[] audioData) { if (_isPlaying && audioData.Length > 0) { await _audioPlaybackWriter.WriteAsync(audioData).ConfigureAwait(false); } } /// <summary> /// Event handler for audio data available from microphone. /// </summary> private void OnAudioDataAvailable(object? sender, WaveInEventArgs e) { if (_isCapturing && e.BytesRecorded > 0) { byte[] audioData = new byte[e.BytesRecorded]; Array.Copy(e.Buffer, 0, audioData, 0, e.BytesRecorded); // Queue audio data for sending (non-blocking) if (!_audioSendWriter.TryWrite(audioData)) { _logger.LogWarning("Failed to queue audio data for sending - channel may be full"); } } } /// <summary> /// Event handler for recording stopped. /// </summary> private void OnRecordingStopped(object? sender, StoppedEventArgs e) { if (e.Exception != null) { _logger.LogError(e.Exception, "Audio recording stopped due to error"); } } /// <summary> /// Background task to process audio data and send to VoiceLive service. /// </summary> private async Task ProcessAudioSendAsync(CancellationToken cancellationToken) { try { await foreach (byte[] audioData in _audioSendReader.ReadAllAsync(cancellationToken).ConfigureAwait(false)) { if (cancellationToken.IsCancellationRequested) break; try { // Send audio data directly to the session await _session.SendInputAudioAsync(audioData, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _logger.LogError(ex, "Error sending audio data to VoiceLive"); // Continue processing other audio data } } } catch (OperationCanceledException) { // Expected when cancellation is requested } catch (Exception ex) { _logger.LogError(ex, "Error in audio send processing"); } } /// <summary> /// Background task to process audio playback. /// </summary> private async Task ProcessAudioPlaybackAsync() { try { CancellationTokenSource combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_playbackCancellationTokenSource.Token, _cancellationTokenSource.Token); var cancellationToken = combinedTokenSource.Token; await foreach (byte[] audioData in _audioPlaybackReader.ReadAllAsync(cancellationToken).ConfigureAwait(false)) { if (cancellationToken.IsCancellationRequested) break; try { if (_playbackBuffer != null && _isPlaying) { _playbackBuffer.AddSamples(audioData, 0, audioData.Length); } } catch (Exception ex) { _logger.LogError(ex, "Error in audio playback"); // Continue processing other audio data } } } catch (OperationCanceledException) { // Expected when cancellation is requested } catch (Exception ex) { _logger.LogError(ex, "Error in audio playback processing"); } } /// <summary> /// Clean up audio resources. /// </summary> public async Task CleanupAsync() { await StopCaptureAsync().ConfigureAwait(false); await StopPlaybackAsync().ConfigureAwait(false); _cancellationTokenSource.Cancel(); // Wait for background tasks to complete var tasks = new List<Task>(); if (_audioSendTask != null) tasks.Add(_audioSendTask); if (_audioPlaybackTask != null) tasks.Add(_audioPlaybackTask); if (tasks.Count > 0) { await Task.WhenAll(tasks).ConfigureAwait(false); } _logger.LogInformation("Audio processor cleaned up"); } /// <summary> /// Dispose of resources. /// </summary> public void Dispose() { CleanupAsync().Wait(); _cancellationTokenSource.Dispose(); } } public class SampleProgram { /// <summary> /// Main entry point for the customer service bot sample. /// </summary> /// <param name="args"></param> /// <returns></returns> public static async Task<int> Main(string[] args) { // Create command line interface var rootCommand = CreateRootCommand(); return await rootCommand.InvokeAsync(args).ConfigureAwait(false); } private static RootCommand CreateRootCommand() { var rootCommand = new RootCommand("Customer Service Bot using Azure VoiceLive SDK with Function Calling"); var apiKeyOption = new Option<string?>( "--api-key", "Azure VoiceLive API key. If not provided, will use AZURE_VOICELIVE_API_KEY environment variable."); var endpointOption = new Option<string>( "--endpoint", () => "wss://api.voicelive.com/v1", "Azure VoiceLive endpoint"); var modelOption = new Option<string>( "--model", () => "gpt-realtime", "VoiceLive model to use"); var voiceOption = new Option<string>( "--voice", () => "en-US-Ava:DragonHDLatestNeural", "Voice to use for the customer service bot"); var instructionsOption = new Option<string>( "--instructions", () => "You are a professional customer service representative for TechCorp. You have access to customer databases and order systems. Always be polite, helpful, and efficient. When customers ask about orders, accounts, or need to schedule service, use the available tools to provide accurate, real-time information. Keep your responses concise but thorough.", "System instructions for the customer service bot"); var useTokenCredentialOption = new Option<bool>( "--use-token-credential", "Use Azure token credential instead of API key"); var verboseOption = new Option<bool>( "--verbose", "Enable verbose logging"); rootCommand.AddOption(apiKeyOption); rootCommand.AddOption(endpointOption); rootCommand.AddOption(modelOption); rootCommand.AddOption(voiceOption); rootCommand.AddOption(instructionsOption); rootCommand.AddOption(useTokenCredentialOption); rootCommand.AddOption(verboseOption); rootCommand.SetHandler(async ( string? apiKey, string endpoint, string model, string voice, string instructions, bool useTokenCredential, bool verbose) => { await RunCustomerServiceBotAsync(apiKey, endpoint, model, voice, instructions, useTokenCredential, verbose).ConfigureAwait(false); }, apiKeyOption, endpointOption, modelOption, voiceOption, instructionsOption, useTokenCredentialOption, verboseOption); return rootCommand; } private static async Task RunCustomerServiceBotAsync( string? apiKey, string endpoint, string model, string voice, string instructions, bool useTokenCredential, bool verbose) { // Setup configuration var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true) .AddEnvironmentVariables() .Build(); // Override with command line values if provided apiKey ??= configuration["VoiceLive:ApiKey"] ?? Environment.GetEnvironmentVariable("AZURE_VOICELIVE_API_KEY"); endpoint = configuration["VoiceLive:Endpoint"] ?? endpoint; model = configuration["VoiceLive:Model"] ?? model; voice = configuration["VoiceLive:Voice"] ?? voice; instructions = configuration["VoiceLive:Instructions"] ?? instructions; // Setup logging using var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); if (verbose) { builder.SetMinimumLevel(LogLevel.Debug); } else { builder.SetMinimumLevel(LogLevel.Information); } }); var logger = loggerFactory.CreateLogger<SampleProgram>(); // Validate credentials if (string.IsNullOrEmpty(apiKey) && !useTokenCredential) { Console.WriteLine("❌ Error: No authentication provided"); Console.WriteLine("Please provide an API key using --api-key or set AZURE_VOICELIVE_API_KEY environment variable,"); Console.WriteLine("or use --use-token-credential for Azure authentication."); return; } // Check audio system before starting if (!CheckAudioSystem(logger)) { return; } try { // Create client with appropriate credential VoiceLiveClient client; if (useTokenCredential) { var tokenCredential = new DefaultAzureCredential(); client = new VoiceLiveClient(new Uri(endpoint), tokenCredential, new VoiceLiveClientOptions()); logger.LogInformation("Using Azure token credential"); } else { var keyCredential = new Azure.AzureKeyCredential(apiKey!); client = new VoiceLiveClient(new Uri(endpoint), keyCredential, new VoiceLiveClientOptions()); logger.LogInformation("Using API key credential"); } // Create customer service functions implementation var functions = new CustomerServiceFunctions(loggerFactory.CreateLogger<CustomerServiceFunctions>()); // Create and start customer service bot using var bot = new CustomerServiceBot( client, model, voice, instructions, functions, loggerFactory); // Setup cancellation token for graceful shutdown using var cancellationTokenSource = new CancellationTokenSource(); Console.CancelKeyPress += (sender, e) => { e.Cancel = true; logger.LogInformation("Received shutdown signal"); cancellationTokenSource.Cancel(); }; // Display welcome message DisplayWelcomeMessage(); // Start the customer service bot await bot.StartAsync(cancellationTokenSource.Token).ConfigureAwait(false); } catch (OperationCanceledException) { Console.WriteLine("\n👋 Customer service bot shut down. Thank you for using TechCorp support!"); } catch (Exception ex) { logger.LogError(ex, "Fatal error"); Console.WriteLine($"❌ Error: {ex.Message}"); } } private static void DisplayWelcomeMessage() { Console.WriteLine(); Console.WriteLine("🏢 Welcome to TechCorp Customer Service"); Console.WriteLine("======================================"); Console.WriteLine(); Console.WriteLine("I can help you with:"); Console.WriteLine("• 📦 Order status and tracking"); Console.WriteLine("• 👤 Account information and history"); Console.WriteLine("• 🔄 Returns and exchanges"); Console.WriteLine("• 📞 Scheduling technical support calls"); Console.WriteLine("• 🚚 Updating shipping addresses"); Console.WriteLine(); Console.WriteLine("Sample data available:"); Console.WriteLine("• Orders: ORD-2024-001, ORD-2024-002, ORD-2024-003"); Console.WriteLine("• Customers: john.smith@email.com, sarah.johnson@email.com"); Console.WriteLine("• Products: LAPTOP-001, MOUSE-001, MONITOR-001"); Console.WriteLine(); Console.WriteLine("Try saying things like:"); Console.WriteLine("• \"What's the status of order ORD-2024-001?\""); Console.WriteLine("• \"I need to return a defective laptop\""); Console.WriteLine("• \"Can you look up my account info for john.smith@email.com?\""); Console.WriteLine("• \"I need to schedule a technical support call\""); Console.WriteLine(); } private static bool CheckAudioSystem(ILogger logger) { try { // Try input (default device) using (var waveIn = new WaveInEvent { WaveFormat = new WaveFormat(24000, 16, 1), BufferMilliseconds = 50 }) { // Start/Stop to force initialization and surface any device errors waveIn.DataAvailable += (_, __) => { }; waveIn.StartRecording(); waveIn.StopRecording(); } // Try output (default device) var buffer = new BufferedWaveProvider(new WaveFormat(24000, 16, 1)) { BufferDuration = TimeSpan.FromMilliseconds(200) }; using (var waveOut = new WaveOutEvent { DesiredLatency = 100 }) { waveOut.Init(buffer); // Playing isn’t strictly required to validate a device, but it’s safe waveOut.Play(); waveOut.Stop(); } logger.LogInformation("Audio system check passed (default input/output initialized)."); return true; } catch (Exception ex) { Console.WriteLine($"❌ Audio system check failed: {ex.Message}"); return false; } } }运行控制台应用程序以启动实时对话:
dotnet run
输出
脚本的输出将打印到控制台。 你会看到指示连接状态、音频流和播放的消息。 音频通过扬声器或耳机播放。
info: SampleProgram[0]
Audio system check passed (default input/output initialized).
info: SampleProgram[0]
Using API key credential
� Welcome to TechCorp Customer Service
======================================
I can help you with:
• � Order status and tracking
• � Account information and history
• � Returns and exchanges
• � Scheduling technical support calls
• � Updating shipping addresses
Sample data available:
• Orders: ORD-2024-001, ORD-2024-002, ORD-2024-003
• Customers: john.smith@email.com, sarah.johnson@email.com
• Products: LAPTOP-001, MOUSE-001, MONITOR-001
Try saying things like:
• "What's the status of order ORD-2024-001?"
• "I need to return a defective laptop"
• "Can you look up my account info for john.smith@email.com?"
• "I need to schedule a technical support call"
info: CustomerServiceBot[0]
Connecting to VoiceLive API with model gpt-4o
info: AudioProcessor[0]
AudioProcessor initialized with 24000Hz PCM16 mono audio
info: CustomerServiceBot[0]
Setting up customer service session with function calling...
info: CustomerServiceBot[0]
Session configuration sent with 5 customer service tools
info: AudioProcessor[0]
Audio playback system ready
======================================================================
� CUSTOMER SERVICE BOT READY
I can help you with orders, returns, account info, and scheduling support calls
Start speaking to begin your customer service session
Press Ctrl+C to exit
======================================================================
info: AudioProcessor[0]
Started audio capture
info: CustomerServiceBot[0]
Customer service bot ready! Start speaking...
info: CustomerServiceBot[0]
Session ready: sess_2Ti2YGckoXIN71yMJMW265
info: CustomerServiceBot[0]
Session updated successfully with function tools
� Listening...
info: CustomerServiceBot[0]
� Customer started speaking - stopping playback
info: AudioProcessor[0]
Stopped audio playback
相关内容
- 试用语音实时代理快速入门
- 详细了解 如何使用语音实时 API
- 请参阅 语音实时 API 参考