generated from cheshire-cat-ai/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
158 lines (113 loc) · 4.76 KB
/
actions.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
import json
import random
from pathlib import Path
from pydantic import BaseModel, Field
from cat.mad_hatter.mad_hatter import MadHatter
from cat.mad_hatter.decorators import tool
from cat.looking_glass.stray_cat import StrayCat
from cat.experimental.form import form, CatForm
from .settings import Voice, VoiceSpeed
from .utils import load_user_settings, save_user_settings
def is_action_enabled() -> bool:
plugin_path = Path(__file__).parent.parent
with open(plugin_path / "settings.json") as f:
settings = json.load(f)
return settings["actions"]
def pick_random_voice() -> Voice:
setting = MadHatter().get_plugin().load_settings()
current_voice = setting["voice"]
voices = [voice for voice in Voice if voice != current_voice]
return random.choice(voices)
def get_next_speed(current_speed: VoiceSpeed, increase: True) -> VoiceSpeed | None:
speeds_list = [v for v in VoiceSpeed]
current_speed_index = speeds_list.index(current_speed)
if increase and current_speed_index == len(speeds_list) - 1:
return None
if not increase and current_speed_index == 0:
return None
step = 1 if increase else -1
return speeds_list[current_speed_index + step]
class ChangeVoiceModel(BaseModel):
voice: Voice = Field(
description=f"The voice to set, should be be one of {[e.value for e in Voice]}.",
default_factory=pick_random_voice
)
# Only define the actions if they are enabled in the settings
if is_action_enabled():
@form
class ChangeVoice(CatForm):
model_class = ChangeVoiceModel
description = "This action allows you to change the voice are you currently using.\
Use this action only if the user has explicitly asked to change the voice."
start_examples = [
"Change your voice",
"Use the voice Echo",
]
def submit(self, form_data) -> str:
# Load current settings
settings = load_user_settings(self.cat.user_id)
# Update voice
settings.voice = form_data["voice"]
# Save new settings
save_user_settings(self.cat.user_id, settings)
return {
"output": f"Voice changed to {form_data['voice']}."
}
@tool(
examples=[
"You are speaking too slow",
"Can you speak faster?",
]
)
def speak_faster(_, cat: StrayCat):
"""Use this action to make your voice speak faster. Use only if the user has explicitly asked to change the voice speed."""
user_settings = load_user_settings(cat.user_id)
if (new_speed := get_next_speed(user_settings.speed, increase=True)) is None:
return "You are already speaking as fast as you can."
user_settings.speed = new_speed
save_user_settings(cat.user_id, user_settings)
if new_speed == VoiceSpeed.FAST:
return "You are speaking as fast as you can now."
return "You are now speaking faster."
@tool(
examples=[
"You are speaking too fast",
"Can you speak slower?"
]
)
def speak_slower(_, cat: StrayCat):
"""Use this action to make the voice speak slower. Use only if the user have explicitly asked to change the voice speed."""
settings = load_user_settings(cat.user_id)
if (new_speed := get_next_speed(settings.speed, increase=False)) is None:
return "You are already speaking as slow as you can."
settings.speed = new_speed
save_user_settings(cat.user_id, settings)
if new_speed == VoiceSpeed.SLOW:
return "You are speaking as slow as you can now."
return "You are now speaking slower now."
@tool
def current_voice_speed(_, cat: StrayCat):
"""Use this action to how fast you are currently speaking."""
# Load current settings
settings = load_user_settings(cat.user_id)
return f"The current voice speed is: {settings.speed}."
@tool(
examples=[
"What's your current voice?",
"What voice are you using?",
"Which voices can you use?"
]
)
def current_voice(_, cat: StrayCat):
"""User this action to know what voice you are currently using. """
# Load current settings
settings = load_user_settings(cat.user_id)
return f"The current voice is {settings.voice}."
@tool(
examples=[
"Which voices can you use?"
]
)
def available_voice(_, cat: StrayCat):
"""Use this action to know which voices are available to use."""
return "The available voices are : " + ", ".join([v.value for v in Voice])