已新增語意核心的第一個即時 API 整合,目前只能在 Python 中使用,並視為實驗性。 這是因為基礎服務仍在開發中,而且可能會有所變更,而且我們可能需要在語意核心中對 API 進行重大變更,因為我們向客戶學習如何使用,以及當我們新增這類模型和 API 的其他提供者時。
即時客戶端抽象化
為了支援來自不同廠商的不同即時 API,使用不同的通訊協定,新的用戶端抽象概念已新增至核心。 此用戶端用來連線到即時服務,以及傳送和接收訊息。 用戶端負責處理服務連線、傳送訊息及接收訊息。 用戶端也會負責處理連線或訊息傳送/接收程式期間發生的任何錯誤。 考慮到這些模型的運作方式,它們可以被視為代理,而不僅是一般的聊天完成工具。因此,它們不僅會接收指示,而且會保留自己的內部狀態,並能夠在需要時受命來為我們執行工作。
實時應用程式介面
任何即時用戶端會實作下列方法:
| 方法 | 描述 |
|---|---|
create_session |
建立新的工作階段 |
update_session |
更新現有的會話 |
delete_session |
刪除現有的會話 |
receive |
這是異步產生器方法,會接聽來自服務的訊息,並在訊息送達時回傳訊息。 |
send |
將訊息傳送至服務 |
Python 實作
Semantic Kernel 的 Python 版本目前支援下列即時用戶端:
| 客戶 | 協定 | 方式 | 已啟用函式呼叫 | 描述 |
|---|---|---|---|---|
| OpenAI | Websocket(網路通訊協定) | 文字 & 音訊 | 是的 | OpenAI Realtime API 是以 Websocket 為基礎的 API,可讓您即時傳送和接收訊息,此連接器會使用 OpenAI Python 套件來連線和接收和傳送訊息。 |
| OpenAI | WebRTC | 文字 & 音訊 | 是的 | OpenAI Realtime API 是以 WebRTC 為基礎的 API,可讓您即時傳送和接收訊息,因此在會話建立期間需要 WebRTC 相容的音訊播放軌。 |
| 天藍色 | Websocket(網路通訊協定) | 文字 & 音訊 | 是的 | Azure Realtime API 是以 Websocket 為基礎的 API,可讓您即時傳送和接收訊息,這會使用與 OpenAI Websocket 連接器相同的套件。 |
| 天藍色 | WebRTC | 文字 & 音訊 | 是的 | Azure 即時 API 是以 WebRTC 為基礎的 API,可讓您即時傳送和接收訊息,這會使用與 OpenAI WebRTC 連接器相同的套件。 |
入門指南
若要開始使用即時 API,您需要安裝 semantic-kernel 套件。
pip install semantic-kernel
視您想要如何處理音訊而定,您可能需要其他套件來與喇叭和麥克風介面,例如 pyaudio 或 sounddevice。
Websocket 用戶端
然後,您可以建立核心並將即時用戶端新增至其中,這會顯示如何使用 AzureRealtimeWebsocket 連線來執行此動作,您可以將 AzureRealtimeWebsocket 取代為 OpenAIRealtimeWebsocket,而不需要進行任何進一步變更。
from semantic_kernel.connectors.ai.open_ai import (
AzureRealtimeWebsocket,
AzureRealtimeExecutionSettings,
ListenEvents,
)
from semantic_kernel.contents import RealtimeAudioEvent, RealtimeTextEvent
# this will use environment variables to get the api key, endpoint, api version and deployment name.
realtime_client = AzureRealtimeWebsocket()
settings = AzureRealtimeExecutionSettings(voice='alloy')
async with realtime_client(settings=settings, create_response=True):
async for event in realtime_client.receive():
match event:
# receiving a piece of audio (and send it to a undefined audio player)
case RealtimeAudioEvent():
await audio_player.add_audio(event.audio)
# receiving a piece of audio transcript
case RealtimeTextEvent():
# Semantic Kernel parses the transcript to a TextContent object captured in a RealtimeTextEvent
print(event.text.text, end="")
case _:
# OpenAI Specific events
if event.service_type == ListenEvents.SESSION_UPDATED:
print("Session updated")
if event.service_type == ListenEvents.RESPONSE_CREATED:
print("\nMosscap (transcript): ", end="")
有兩個重要注意事項,第一個是 realtime_client 是異步內容管理員,這表示您可以在異步函式中使用它,並使用 async with 來建立會話。
第二個是 receive 方法是異步產生器,這表示您可以在 for 迴圈中使用它,在訊息送達時接收訊息。
WebRTC 用戶端
WebRTC 連線的設定較為複雜,因此在建立用戶端時,我們需要額外的參數。 此參數 audio_track 必須是實作 MediaStreamTrack 套件 aiortc 通訊協議的物件,這也會示範於下列連結的範例中。
若要建立使用 WebRTC 的用戶端,您可以執行下列動作:
from semantic_kernel.connectors.ai.open_ai import (
ListenEvents,
OpenAIRealtimeExecutionSettings,
OpenAIRealtimeWebRTC,
)
from aiortc.mediastreams import MediaStreamTrack
class AudioRecorderWebRTC(MediaStreamTrack):
# implement the MediaStreamTrack methods.
realtime_client = OpenAIRealtimeWebRTC(audio_track=AudioRecorderWebRTC())
# Create the settings for the session
settings = OpenAIRealtimeExecutionSettings(
instructions="""
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
""",
voice="shimmer",
)
audio_player = AudioPlayer
async with realtime_client(settings=settings, create_response=True):
async for event in realtime_client.receive():
match event.event_type:
# receiving a piece of audio (and send it to a undefined audio player)
case "audio":
await audio_player.add_audio(event.audio)
case "text":
# the model returns both audio and transcript of the audio, which we will print
print(event.text.text, end="")
case "service":
# OpenAI Specific events
if event.service_type == ListenEvents.SESSION_UPDATED:
print("Session updated")
if event.service_type == ListenEvents.RESPONSE_CREATED:
print("\nMosscap (transcript): ", end="")
這兩個範例都會以 RealtimeAudioEvent 的形式接收音訊,然後將該音訊傳遞至未指定的audio_player物件。
注意:Azure OpenAI WebRTC 用戶端有額外的參數:
region,這是指裝載 OpenAI 服務的 Azure 區域。 目前僅支援eastus2和swedencentral。
音訊輸出回呼
在此旁邊,我們在 audio_output_callback 方法上和類別建立上都有名為 receive 的參數。 在進一步處理音訊之前,將先調用此回呼,並獲取音訊數據的 numpy 陣列。這種方式不同於先將音訊剖析成 AudioContent 並作為 RealtimeAudioEvent 傳回,後者是您接著可以處理的情況,如上所述。 這顯示可提供更順暢的音訊輸出,因為傳入的音訊數據與提供給播放程式之間的額外負荷較少。
此範例示範如何定義及使用 audio_output_callback:
from semantic_kernel.connectors.ai.open_ai import (
ListenEvents,
OpenAIRealtimeExecutionSettings,
OpenAIRealtimeWebRTC,
)
from aiortc.mediastreams import MediaStreamTrack
class AudioRecorderWebRTC(MediaStreamTrack):
# implement the MediaStreamTrack methods.
class AudioPlayer:
async def play_audio(self, content: np.ndarray):
# implement the audio player
realtime_client = OpenAIRealtimeWebRTC(audio_track=AudioRecorderWebRTC())
# Create the settings for the session
settings = OpenAIRealtimeExecutionSettings(
instructions="""
You are a chat bot. Your name is Mosscap and
you have one goal: figure out what people need.
Your full name, should you need to know it, is
Splendid Speckled Mosscap. You communicate
effectively, but you tend to answer with long
flowery prose.
""",
voice="shimmer",
)
audio_player = AudioPlayer
async with realtime_client(settings=settings, create_response=True):
async for event in realtime_client.receive(audio_output_callback=audio_player.play_audio):
match event.event_type:
# no need to handle case: "audio"
case "text":
# the model returns both audio and transcript of the audio, which we will print
print(event.text.text, end="")
case "service":
# OpenAI Specific events
if event.service_type == ListenEvents.SESSION_UPDATED:
print("Session updated")
if event.service_type == ListenEvents.RESPONSE_CREATED:
print("\nMosscap (transcript): ", end="")
樣品
我們的存放庫中有四個範例,它們涵蓋使用websocket和WebRTC的基本概念,以及更複雜的設定,包括函式呼叫。 最後,有一個更 複雜的示範 會使用 Azure 通訊服務,讓您呼叫語意核心增強的即時 API。
即將推出
更多信息即將推出。
即將推出
更多信息即將推出。