-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
68 lines (55 loc) · 2.28 KB
/
Copy pathagent.py
File metadata and controls
68 lines (55 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import logging
from dotenv import load_dotenv
from livekit import agents
from livekit.agents import AgentSession, Agent
from livekit.agents.voice.room_io import RoomOptions, AudioInputOptions
from livekit.plugins import noise_cancellation, silero
from livekit.plugins.turn_detector.multilingual import MultilingualModel
from livekit.plugins import mistralai
from prompt import AGENT_INSTRUCTIONS, SESSION_INSTRUCTIONS, OUTBOUND_AGENT_INSTRUCTIONS, OUTBOUND_SESSION_INSTRUCTIONS
from tool import transfer_to_human, end_call, initiate_call
load_dotenv(".env")
class Assistant(Agent):
def __init__(self, is_outbound: bool = False):
self.is_outbound = is_outbound
instructions = OUTBOUND_AGENT_INSTRUCTIONS if is_outbound else AGENT_INSTRUCTIONS
super().__init__(
instructions=instructions,
tools=[transfer_to_human, end_call, initiate_call]
)
async def entrypoint(ctx: agents.JobContext):
# Set up logging
logger = logging.getLogger("outbound-agent")
# Check if this is an outbound call by looking for phone number in metadata
is_outbound = bool(getattr(ctx, 'metadata', None) and ctx.metadata.startswith('+'))
if is_outbound:
logger.info(f"Starting outbound call to {ctx.metadata}")
else:
logger.info("Starting inbound call")
session = AgentSession(
stt="deepgram/nova-3:en",
llm=mistralai.LLM(model="mistral-small-2506"),
tts="elevenlabs/eleven_turbo_v2_5:Xb7hH8MSUJpSbSDYk0k2",
vad=silero.VAD.load(),
turn_detection=MultilingualModel(),
)
await session.start(
room=ctx.room,
agent=Assistant(is_outbound=is_outbound),
room_options=RoomOptions(
audio_input=AudioInputOptions(
noise_cancellation=noise_cancellation.BVC(),
),
),
)
# Use appropriate instructions based on call direction
instructions = OUTBOUND_SESSION_INSTRUCTIONS if is_outbound else SESSION_INSTRUCTIONS
await session.generate_reply(instructions=instructions)
if is_outbound:
logger.info(f"Outbound call to {ctx.metadata} is active")
if __name__ == "__main__":
agents.cli.run_app(agents.WorkerOptions(
entrypoint_fnc=entrypoint,
agent_name="in&out"
))