-
Notifications
You must be signed in to change notification settings - Fork 48
/
conversation.py
222 lines (199 loc) · 7.26 KB
/
conversation.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
from datetime import datetime
from typing import Annotated, Any, Optional, Union
from pydantic import BaseModel, ConfigDict, EmailStr, Field, create_model
from pydantic.fields import FieldInfo
from helpers.pydantic_types.phone_numbers import PhoneNumber
from models.claim import ClaimFieldModel, ClaimTypeEnum
class LanguageEntryModel(BaseModel):
"""
Language entry, containing the standard short code, an human name and the Azure Text-to-Speech voice name.
See: https://learn.microsoft.com/en-us/azure/ai-services/speech-service/language-support?tabs=tts#supported-languages
"""
custom_voice_endpoint_id: Optional[str] = None
pronunciations_en: list[str]
short_code: str
voice: str
@property
def human_name(self) -> str:
return self.pronunciations_en[0]
def __str__(self): # Pretty print for logs
return self.short_code
class LanguageModel(BaseModel):
"""
Manage language for the workflow.
"""
default_short_code: str = "fr-FR"
# Voice list from Azure TTS
# See: https://learn.microsoft.com/en-us/azure/ai-services/speech-service/language-support?tabs=tts
availables: list[LanguageEntryModel] = [
LanguageEntryModel(
pronunciations_en=["French", "FR", "France"],
short_code="fr-FR",
voice="fr-FR-DeniseNeural",
),
LanguageEntryModel(
pronunciations_en=["English", "EN", "United States"],
short_code="en-US",
voice="en-US-NancyNeural",
),
LanguageEntryModel(
pronunciations_en=["Spanish", "ES", "Spain"],
short_code="es-ES",
voice="es-ES-LiaNeural",
),
LanguageEntryModel(
pronunciations_en=["Chinese", "ZH", "China"],
short_code="zh-CN",
voice="zh-CN-XiaoqiuNeural",
),
]
@property
def default_lang(self) -> LanguageEntryModel:
return next(
(
lang
for lang in self.availables
if self.default_short_code == lang.short_code
),
self.availables[0],
)
class WorkflowInitiateModel(BaseModel):
agent_phone_number: PhoneNumber
bot_company: str
bot_name: str
claim: list[ClaimFieldModel] = [
ClaimFieldModel(
description="Date and time of the incident",
name="incident_datetime",
type=ClaimTypeEnum.DATETIME,
),
ClaimFieldModel(
description="Description of the incident",
name="incident_description",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Location of the incident",
name="incident_location",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Injuries sustained during the incident",
name="injuries",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Involved parties in the incident",
name="involved_parties",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Medical records related to the incident",
name="medical_records",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Policy number of the customer",
name="policy_number",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Pre-existing damages",
name="pre_existing_damages",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Witnesses of the incident",
name="witnesses",
type=ClaimTypeEnum.TEXT,
),
] # Configured like in v4 for compatibility
lang: LanguageModel = LanguageModel() # Object is fully defined by default
prosody_rate: float = Field(
default=1.0,
ge=0.75,
le=1.25,
)
task: str = (
"Helping the customer to file an insurance claim. The customer is probably calling because they have a problem with something covered by their policy, but it's not certain. The assistant needs information from the customer to complete the claim. The conversation is over when all the data relevant to the case has been collected. Filling in as much information as possible is important for further processing."
)
def claim_model(self) -> type[BaseModel]:
return _fields_to_pydantic(
name="ClaimEntryModel",
fields=[
*self.claim,
ClaimFieldModel(
description="Email of the customer",
name="policyholder_email",
type=ClaimTypeEnum.EMAIL,
),
ClaimFieldModel(
description="First and last name of the customer",
name="policyholder_name",
type=ClaimTypeEnum.TEXT,
),
ClaimFieldModel(
description="Phone number of the customer",
name="policyholder_phone",
type=ClaimTypeEnum.PHONE_NUMBER,
),
],
)
class ConversationModel(BaseModel):
initiate: WorkflowInitiateModel
answer_hard_timeout_sec: int = Field(
default=180, # 3 mins
serialization_alias="intelligence_hard_timeout_sec", # Compatibility with v7
)
answer_soft_timeout_sec: int = Field(
default=30, # 30 secs
serialization_alias="intelligence_soft_timeout_sec", # Compatibility with v7
)
callback_timeout_hour: int = Field(
default=72, # 3 days
serialization_alias="conversation_timeout_hour", # Compatibility with v7
)
phone_silence_timeout_sec: int = Field(
default=1, # 1 second
serialization_alias="voice_timeout_after_silence_sec", # Compatibility with v7
)
slow_llm_for_chat: bool = Field(
default=True,
serialization_alias="use_slow_llm_for_chat_as_default", # Compatibility with v7
)
voice_recognition_retry_max: int = Field(
default=2, # 2 retries
serialization_alias="max_voice_recognition_retry", # Compatibility with v7
)
def _fields_to_pydantic(name: str, fields: list[ClaimFieldModel]) -> type[BaseModel]:
field_definitions = {field.name: _field_to_pydantic(field) for field in fields}
return create_model(
name,
**field_definitions, # pyright: ignore
__config__=ConfigDict(
extra="ignore", # Avoid validation errors, just ignore data
),
)
def _field_to_pydantic(
field: ClaimFieldModel,
) -> Union[Annotated[Any, ...], tuple[type, FieldInfo]]:
field_type = _type_to_pydantic(field.type)
return (
Optional[field_type],
Field(
default=None,
description=field.description,
),
)
def _type_to_pydantic(
data: ClaimTypeEnum,
) -> Union[type, Annotated[Any, ...]]:
if data == ClaimTypeEnum.DATETIME:
return datetime
if data == ClaimTypeEnum.EMAIL:
return EmailStr
if data == ClaimTypeEnum.PHONE_NUMBER:
return PhoneNumber
if data == ClaimTypeEnum.TEXT:
return str
raise ValueError(f"Unsupported data: {data}")