-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy pathmain.py
More file actions
79 lines (64 loc) · 3.03 KB
/
main.py
File metadata and controls
79 lines (64 loc) · 3.03 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
69
70
71
72
73
74
75
76
77
78
79
import json
import os
from src.agent.capability import MatchingCapability
from src.agent.capability_worker import CapabilityWorker
from src.main import AgentWorker
# =============================================================================
# VOICE UNIT CONVERTER
# A voice-powered unit converter. Ask any conversion in natural language
# and get the answer instantly. No API needed — the LLM handles everything.
# =============================================================================
EXIT_WORDS = {"stop", "exit", "quit", "done", "cancel", "bye", "goodbye", "leave"}
SYSTEM_PROMPT = (
"You are a unit converter. The user will ask you to convert between units. "
"Respond with ONLY the conversion result in one short sentence. "
"Do not explain the formula. Do not add disclaimers. Just give the answer. "
"Example: '200 grams is about 7 ounces.'"
)
class VoiceUnitConverterCapability(MatchingCapability):
worker: AgentWorker = None
capability_worker: CapabilityWorker = None
#{{register_capability}}
def call(self, worker: AgentWorker):
self.worker = worker
self.capability_worker = CapabilityWorker(self.worker)
self.worker.session_tasks.create(self.run())
async def run(self):
try:
self.worker.editor_logging_handler.info("[VoiceUnitConverter] Ability started")
await self.capability_worker.speak(
"Unit converter ready. What would you like to convert?"
)
while True:
user_input = await self.capability_worker.user_response()
if not user_input:
await self.capability_worker.speak(
"I didn't catch that. What would you like to convert?"
)
continue
if any(word in user_input.lower() for word in EXIT_WORDS):
await self.capability_worker.speak("Goodbye!")
break
try:
response = self.capability_worker.text_to_text_response(
user_input, system_prompt=SYSTEM_PROMPT
)
await self.capability_worker.speak(response)
await self.capability_worker.speak("Anything else?")
except Exception as e:
self.worker.editor_logging_handler.error(
f"[VoiceUnitConverter] LLM error: {e}"
)
await self.capability_worker.speak(
"Sorry, I had trouble with that. Try again."
)
except Exception as e:
self.worker.editor_logging_handler.error(
f"[VoiceUnitConverter] Unexpected error: {e}"
)
await self.capability_worker.speak(
"Something went wrong. Exiting unit converter."
)
finally:
self.worker.editor_logging_handler.info("[VoiceUnitConverter] Ability ended")
self.capability_worker.resume_normal_flow()