公開如何使用 OpenAI 配合 LiveKit 來實現會多國語言的小姐姐,可以即時回答您的問題,這個跟 Twilio 一樣的簡單和易用

內容目錄
取得 LiveKit key
利用 google 帳號登入 LiveKit Login 命名一個 project

並且到專案中的 settings -> KEYS ,取得 API KEY

程式碼
首先安裝相關依賴
pip install livekit-agents livekit-plugins-openai livekit-plugins-silero python-dotenv
設定環境變數
LIVEKIT_URL="" LIVEKIT_API_KEY="" LIVEKIT_API_SECRET="" OPENAI_API_KEY=""
主要程式碼
import asyncio
from dotenv import load_dotenv
from livekit.agents import AutoSubscribe, JobContext,WorkerOptions, cli, llm
from livekit.agents.voice_assistant import VoiceAssistant
from livekit.plugins import openai, silero
load_dotenv()
async def entry(ctx: JobContext):
chat_ctx = llm.ChatContext().append(
role="system",
text=("你是專業的助理,回答時候用專業的語氣回應。")
)
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
asssitant = VoiceAssistant(
vad=silero.VAD.load(),
stt=openai.STT(),
tts=openai.TTS(voice="nova"),
llm=openai.LLM(model="gpt-4o-mini"),
chat_ctx=chat_ctx
)
asssitant.start(ctx.room)
await asyncio.sleep(1)
await asssitant.say("你好,第一次見面,很高興認識你",allow_interruptions=True)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entry))測試與驗證
道專案中,可以看到 Get started 中有支援各種的平台的程式碼以及 server 可以使用

價格說明

參考資料
https://github.com/livekit/agents
近期留言