-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathagent.py
432 lines (362 loc) · 15.4 KB
/
agent.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
from __future__ import annotations
from dotenv import load_dotenv
load_dotenv(override=True)
import os
import asyncio
import logging
import uuid
from dataclasses import asdict, dataclass
from typing import Annotated, Literal
from livekit import rtc
from livekit.agents import (
AutoSubscribe,
JobContext,
WorkerOptions,
WorkerPermissions,
WorkerType,
cli,
llm,
)
from livekit.agents.multimodal import MultimodalAgent
from livekit.plugins import openai
from livekit.api import LiveKitAPI, DeleteRoomRequest
from jinja2 import Template
from core.config import OPENAI_API_KEY
from rich import print
logger = logging.getLogger("")
logger.setLevel(logging.INFO)
@dataclass
class SessionConfig:
openai_api_key: str
instructions: str
voice: openai.realtime.api_proto.Voice
temperature: float
max_response_output_tokens: str | int
modalities: list[openai.realtime.api_proto.Modality]
turn_detection: openai.realtime.ServerVadOptions
def __post_init__(self):
if self.modalities is None:
self.modalities = self._modalities_from_string("text_and_audio")
def to_dict(self):
return {k: v for k, v in asdict(self).items() if k != "openai_api_key"}
@staticmethod
def _modalities_from_string(modalities: str) -> list[str]:
modalities_map = {
"text_and_audio": ["text", "audio"],
"text_only": ["text"],
}
return modalities_map.get(modalities, ["text", "audio"])
def __eq__(self, other: SessionConfig) -> bool:
return self.to_dict() == other.to_dict()
class AgentFunctions(llm.FunctionContext):
def __init__(self, ctx: JobContext):
self.ctx = ctx
self.remote_identity = next(iter(ctx.room.remote_participants.values()), None).identity
super().__init__()
def _call_remote(self, method: str, payload: dict):
try:
return self.ctx.room.local_participant.perform_rpc(
destination_identity=self.remote_identity,
method=method,
payload=payload
)
except Exception as e:
logger.error(f"Error calling remote method {method}: {e}")
return None
@llm.ai_callable()
async def terminate_session(self):
"""Called when the user ask to terminate the conversation. This function will end the conversation."""
try:
logger.info("🛑 Terminating session...")
await self._call_remote("terminate", "end_session")
lkapi = LiveKitAPI()
await lkapi.room.delete_room(DeleteRoomRequest(
room=self.ctx.room.name,
))
except Exception as e:
logger.error(f"Error: {e}")
@llm.ai_callable()
async def greet(self):
"""Called as soon as entering in a conversation. This function starts the conversation."""
try:
logger.info(f"👋 Greeting user")
return "Hello! How can I help you today?"
except Exception as e:
logger.error(f"Error: {e}")
@llm.ai_callable()
async def save_idea(
self,
idea: Annotated[str, llm.TypeInfo(description="The idea formulated by the user or slightly enhanced for clarity.")],
):
"""Called when there is an information that is relevant enough to be saved."""
logger.info(f"🛠️ Saving idea: {idea}")
return await self._call_remote('save', idea)
@llm.ai_callable()
async def show(
self,
information: Annotated[str, llm.TypeInfo(description="The information to display or show written in Markdown format.")],
):
"""Display information to the user."""
# return f"Section '{section_title}' outlined with key points: {key_points}"
logger.info(f"🛠️ Displaying information {information}")
return await self._call_remote('show', information)
class Agent:
def __init__(self):
self.role = ''
self.ctx = None
super().__init__()
def load_agent_instructions(self) -> str:
# check if there is a `data.py` file in the role folder. If so, load the variables to pass them to the template
if os.path.exists(f"roles/{self.role}/data.py"):
# import the variables from the data.py file
module = __import__(f"roles.{self.role}.data", fromlist=["*"])
data = {key: value for key, value in module.__dict__.items() if not key.startswith("__")}
else:
data = {}
print(f"data: {data}")
with open(f"roles/{self.role}/agent.instruct") as f:
instructions = Template(f.read()).render(data=data)
return instructions
def get_session_config(self) -> SessionConfig:
# default voice to coral
voice = "coral"
# check if there is a config.py in the role folder. If so, load the config from there
if os.path.exists(f"roles/{self.role}/config.py"):
# import the variables from the config.py file
module = __import__(f"roles.{self.role.replace('/','.')}.config", fromlist=["*"])
voice = module.voice if hasattr(module, "voice") else "coral"
config = SessionConfig(
openai_api_key=OPENAI_API_KEY,
instructions=self.load_agent_instructions(),
voice=voice,
temperature=0.8,
max_response_output_tokens= 2048,
modalities=["text", "audio"],
turn_detection=openai.realtime.DEFAULT_SERVER_VAD_OPTIONS,
)
return config
async def call_rpc(self, participant: rtc.LocalParticipant, remote_identity: str):
try:
logger.info(f"👋 calling RPC greet to {remote_identity}")
response = await participant.perform_rpc(
destination_identity=remote_identity,
method='greet',
payload='Hello from RPC!'
)
print(f"RPC response: {response}")
except Exception as e:
print(f"RPC call failed: {e}")
async def entrypoint(self, ctx: JobContext):
logger.info(f"connecting to room {ctx.room.name}")
self.ctx = ctx
await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()
logger.info(f"<<<--•••-->>> participant attributes: {participant.attributes}")
self.role = participant.attributes.get("role", None)
remote_identity = participant.identity
await self.call_rpc(ctx.room.local_participant, remote_identity)
self.run_multimodal_agent(ctx, participant)
logger.info("agent started")
def run_multimodal_agent(self, ctx: JobContext, participant: rtc.Participant):
logger.info(f"participant {participant.identity} joined the room")
config = self.get_session_config()
logger.info(f"starting omni assistant with config: {config.to_dict()}")
model = openai.realtime.RealtimeModel(
api_key=config.openai_api_key,
instructions=config.instructions,
voice=config.voice,
temperature=config.temperature,
max_response_output_tokens=config.max_response_output_tokens,
modalities=config.modalities,
turn_detection=config.turn_detection,
)
functions : llm.FunctionContext = AgentFunctions(ctx)
# if there is a functions.py in the role folder, override the functions
if self.role:
if os.path.exists(f"roles/{self.role}/functions.py"):
logger.info(f"🧙👋 Loading custom functions from {self.role}/functions.py")
# first check if there is only one class in the file
with open(f"roles/{self.role}/functions.py") as f:
lines = f.readlines()
class_lines = [line for line in lines if "class" in line]
if len(class_lines) != 1:
raise Exception("There should be only one class in the functions.py file")
class_name = class_lines[0].split("(")[0].split(" ")[-1]
# import the class
module = __import__(f"roles.{self.role.replace('/', '.')}.functions", fromlist=[class_name])
functions = getattr(module, class_name)(ctx)
logger.info(f"🧙✅ Custom functions loaded from {self.role}/functions.py")
assistant = MultimodalAgent(
model=model,
fnc_ctx=functions,
)
assistant.start(ctx.room)
session = model.sessions[0]
if config.modalities == ["text", "audio"]:
session.conversation.item.create(
llm.ChatMessage(
role="user",
content="Please begin the interaction with the user in a manner consistent with your instructions.",
)
)
session.response.create()
@session.on("response_done")
def on_response_done(response: openai.realtime.RealtimeResponse):
variant: Literal["warning", "destructive"]
description: str | None = None
title: str
if response.status == "incomplete":
if response.status_details and response.status_details["reason"]:
reason = response.status_details["reason"]
if reason == "max_output_tokens":
variant = "warning"
title = "Max output tokens reached"
description = "Response may be incomplete"
elif reason == "content_filter":
variant = "warning"
title = "Content filter applied"
description = "Response may be incomplete"
else:
variant = "warning"
title = "Response incomplete"
else:
variant = "warning"
title = "Response incomplete"
elif response.status == "failed":
if response.status_details and response.status_details["error"]:
error_code = response.status_details["error"]["code"]
if error_code == "server_error":
variant = "destructive"
title = "Server error"
elif error_code == "rate_limit_exceeded":
variant = "destructive"
title = "Rate limit exceeded"
else:
variant = "destructive"
title = "Response failed"
else:
variant = "destructive"
title = "Response failed"
else:
return
logger.warning(f"response failed: {title} > {description}")
async def send_transcription(
ctx: JobContext,
participant: rtc.Participant,
track_sid: str,
segment_id: str,
text: str,
is_final: bool = True,
):
transcription = rtc.Transcription(
participant_identity=participant.identity,
track_sid=track_sid,
segments=[
rtc.TranscriptionSegment(
id=segment_id,
text=text,
start_time=0,
end_time=0,
language="en",
final=is_final,
)
],
)
await ctx.room.local_participant.publish_transcription(transcription)
last_transcript_id = None
# send three dots when the user starts talking. will be cleared later when a real transcription is sent.
@session.on("input_speech_started")
def on_input_speech_started():
nonlocal last_transcript_id
remote_participant = next(iter(ctx.room.remote_participants.values()), None)
if not remote_participant:
return
track_sid = next(
(
track.sid
for track in remote_participant.track_publications.values()
if track.source == rtc.TrackSource.SOURCE_MICROPHONE
),
None,
)
if last_transcript_id:
asyncio.create_task(
send_transcription(
ctx, remote_participant, track_sid, last_transcript_id, ""
)
)
new_id = str(uuid.uuid4())
last_transcript_id = new_id
asyncio.create_task(
send_transcription(
ctx, remote_participant, track_sid, new_id, "…", is_final=False
)
)
@session.on("input_speech_transcription_completed")
def on_input_speech_transcription_completed(
event: openai.realtime.InputTranscriptionCompleted,
):
nonlocal last_transcript_id
if last_transcript_id:
remote_participant = next(iter(ctx.room.remote_participants.values()), None)
if not remote_participant:
return
track_sid = next(
(
track.sid
for track in remote_participant.track_publications.values()
if track.source == rtc.TrackSource.SOURCE_MICROPHONE
),
None,
)
asyncio.create_task(
send_transcription(
ctx, remote_participant, track_sid, last_transcript_id, ""
)
)
last_transcript_id = None
@session.on("input_speech_transcription_failed")
def on_input_speech_transcription_failed(
event: openai.realtime.InputTranscriptionFailed,
):
nonlocal last_transcript_id
if last_transcript_id:
remote_participant = next(iter(ctx.room.remote_participants.values()), None)
if not remote_participant:
return
track_sid = next(
(
track.sid
for track in remote_participant.track_publications.values()
if track.source == rtc.TrackSource.SOURCE_MICROPHONE
),
None,
)
error_message = "⚠️ Transcription failed"
asyncio.create_task(
send_transcription(
ctx,
remote_participant,
track_sid,
last_transcript_id,
error_message,
)
)
last_transcript_id = None
def run_agent(self):
try:
print("[bold green]✨🧙 Starting agent...[/bold green]")
cli.run_app(
WorkerOptions(
entrypoint_fnc=self.entrypoint,
worker_type=WorkerType.ROOM,
permissions=WorkerPermissions(
can_update_metadata=True,
)
)
)
finally:
print("[bold red]✨🧙 Agent stopped...[/bold red]")
if __name__ == "__main__":
agent = Agent()
agent.run_agent()