สํารวจไลบรารีไคลเอ็นต์ AI Voice Live สําหรับ Python

เสร็จสมบูรณ์เมื่อ

Tip

ดูแท็บ ข้อความและรูปภาพ สําหรับรายละเอียดเพิ่มเติม!

ไลบรารีไคลเอ็นต์ Azure AI Voice Live สําหรับ Python มีไคลเอ็นต์การแปลงเสียงพูดแบบเรียลไทม์สําหรับ Azure AI Voice Live API เปิดเซสชัน WebSocket เพื่อสตรีมเสียงไมโครโฟนไปยังบริการและรับเหตุการณ์เซิร์ฟเวอร์สําหรับการสนทนาที่ตอบสนอง

สําคัญ

ตั้งแต่เวอร์ชัน 1.0.0 SDK นี้เป็นแบบอะซิงโครนัสเท่านั้น API แบบซิงโครนัสเลิกใช้แล้วเพื่อมุ่งเน้นไปที่รูปแบบแบบอะซิงโครนัสเท่านั้น ตัวอย่างและตัวอย่างทั้งหมดใช้ไวยากรณ์แบบอะซิงโครนัส/รอ

ในหน่วยนี้ คุณจะได้เรียนรู้วิธีใช้ SDK เพื่อใช้การรับรองความถูกต้องและจัดการเหตุการณ์ นอกจากนี้ คุณยังเห็นตัวอย่างขั้นต่ําของการสร้างเซสชัน หากต้องการข้อมูลอ้างอิงฉบับเต็มเกี่ยวกับแพ็กเกจ Voice Live โปรดไปที่ข้อมูลอ้างอิงแพ็กเกจ Voice Live

ใช้การรับรองความถูกต้อง

คุณสามารถใช้การรับรองความถูกต้องด้วยคีย์ API หรือโทเค็น Microsoft Entra ID ตัวอย่างโค้ดต่อไปนี้แสดงการใช้งานคีย์ API โดยถือว่าตัวแปรสภาพแวดล้อมถูกตั้งค่าใน .env ไฟล์หรือในสภาพแวดล้อมของคุณโดยตรง

import asyncio
from azure.core.credentials import AzureKeyCredential
from azure.ai.voicelive import connect

async def main():
    async with connect(
        endpoint="your-endpoint",
        credential=AzureKeyCredential("your-api-key"),
        model="gpt-4o"
    ) as connection:
        # Your async code here
        pass

asyncio.run(main())

สําหรับแอปพลิเคชันที่ใช้งานจริง ขอแนะนําให้ใช้การรับรองความถูกต้องของ Microsoft Entra ตัวอย่างโค้ดต่อไปนี้แสดงการใช้การรับรอง DefaultAzureCredential ความถูกต้องสําหรับ:

import asyncio
from azure.identity.aio import DefaultAzureCredential
from azure.ai.voicelive import connect

async def main():
    credential = DefaultAzureCredential()

    async with connect(
        endpoint="your-endpoint",
        credential=credential,
        model="gpt-4o"
    ) as connection:
        # Your async code here
        pass

asyncio.run(main())

การจัดการเหตุการณ์

การจัดการเหตุการณ์ที่เหมาะสมช่วยให้มั่นใจได้ถึงปฏิสัมพันธ์ที่ราบรื่นยิ่งขึ้นระหว่างลูกค้าและตัวแทน ตัวอย่างเช่น เมื่อจัดการกับผู้ใช้ที่ขัดจังหวะตัวแทนเสียง คุณจําเป็นต้องยกเลิกการเล่นเสียงของตัวแทนทันทีในไคลเอ็นต์ หากคุณไม่ทําเช่นนั้น ไคลเอ็นต์จะยังคงเล่นการตอบสนองของตัวแทนล่าสุดต่อไปจนกว่าจะมีการประมวลผลการขัดจังหวะใน API ซึ่งส่งผลให้ตัวแทน "พูดคุย" กับผู้ใช้

ตัวอย่างโค้ดต่อไปนี้แสดงการจัดการเหตุการณ์พื้นฐานบางอย่าง:

async for event in connection:
    if event.type == ServerEventType.SESSION_UPDATED:
        print(f"Session ready: {event.session.id}")
        # Start audio capture

    elif event.type == ServerEventType.INPUT_AUDIO_BUFFER_SPEECH_STARTED:
        print("User started speaking")
        # Stop playback and cancel any current response

    elif event.type == ServerEventType.RESPONSE_AUDIO_DELTA:
        # Play the audio chunk
        audio_bytes = event.delta

    elif event.type == ServerEventType.ERROR:
        print(f"Error: {event.error.message}")

ตัวอย่างขั้นต่ํา

ตัวอย่างโค้ดต่อไปนี้แสดงการรับรองความถูกต้องกับ API และการกําหนดค่าเซสชัน

import asyncio
from azure.core.credentials import AzureKeyCredential
from azure.ai.voicelive.aio import connect
from azure.ai.voicelive.models import (
    RequestSession, Modality, InputAudioFormat, OutputAudioFormat, ServerVad, ServerEventType
)

API_KEY = "your-api-key"
ENDPOINT = "your-endpoint"
MODEL = "gpt-4o"

async def main():
    async with connect(
        endpoint=ENDPOINT,
        credential=AzureKeyCredential(API_KEY),
        model=MODEL,
    ) as conn:
        session = RequestSession(
            modalities=[Modality.TEXT, Modality.AUDIO],
            instructions="You are a helpful assistant.",
            input_audio_format=InputAudioFormat.PCM16,
            output_audio_format=OutputAudioFormat.PCM16,
            turn_detection=ServerVad(
                threshold=0.5, 
                prefix_padding_ms=300, 
                silence_duration_ms=500
            ),
        )
        await conn.session.update(session=session)

        # Process events
        async for evt in conn:
            print(f"Event: {evt.type}")
            if evt.type == ServerEventType.RESPONSE_DONE:
                break

asyncio.run(main())