项目文件夹

文件
2026-07-13 13:39:38 +08:00

65 行
1.8 KiB
Python

import logging
from dotenv import load_dotenv
from livekit.agents import (
Agent,
AgentServer,
AgentSession,
JobContext,
cli,
room_io,
)
from livekit.plugins import xai
# uncomment lines 18 and 66-68 to enable Krisp background voice/noise cancellation
# from livekit.plugins import noise_cancellation
logger = logging.getLogger("agent")
load_dotenv(".env.local")
class Assistant(Agent):
def __init__(self) -> None:
super().__init__(
instructions="""You are a helpful voice AI assistant. The user is interacting with you via voice, even if you perceive the conversation as text.
You eagerly assist users with their questions by providing information from your extensive knowledge.
Your responses are concise, to the point, and without any complex formatting or punctuation including emojis, asterisks, or other symbols.
You are curious, friendly, and have a sense of humor.""",
)
server = AgentServer()
@server.rtc_session()
async def my_agent(ctx: JobContext):
ctx.log_context_fields = {
"room": ctx.room.name,
}
session = AgentSession(
llm=xai.realtime.RealtimeModel(voice="ara"),
tools=[xai.realtime.XSearch(), xai.realtime.WebSearch()],
preemptive_generation=True,
)
await session.start(
agent=Assistant(),
room=ctx.room,
room_options=room_io.RoomOptions(
audio_input=room_io.AudioInputOptions(
# noise_cancellation=lambda params: noise_cancellation.BVCTelephony()
# if params.participant.kind == rtc.ParticipantKind.PARTICIPANT_KIND_SIP
# else noise_cancellation.BVC(),
),
),
)
await ctx.connect()
if __name__ == "__main__":
cli.run_app(server)