Spaces:
Sleeping
Sleeping
Refactor empty_field usage in prompt replacements
Browse filesRemoved unnecessary `empty_field` parameters across multiple functions and replaced them with clearer instructions. Enhanced prompt templates and utils to improve consistency and readability for missing field messages.
trauma/api/message/ai/engine.py
CHANGED
|
@@ -53,7 +53,7 @@ async def search_entities(
|
|
| 53 |
empty_field_instructions = pick_empty_field_instructions(empty_field)
|
| 54 |
|
| 55 |
if empty_field == 'age':
|
| 56 |
-
response = await generate_next_question(
|
| 57 |
else:
|
| 58 |
user_messages_str = prepare_user_messages_str(decoded_message, messages)
|
| 59 |
possible_entity_indexes, search_request = await asyncio.gather(
|
|
@@ -63,7 +63,7 @@ async def search_entities(
|
|
| 63 |
final_entities = await search_semantic_entities(search_request, entity_data, possible_entity_indexes)
|
| 64 |
final_entities_str = prepare_final_entities_str(final_entities)
|
| 65 |
response = await generate_final_response(
|
| 66 |
-
final_entities_str, decoded_message, message_history_str, empty_field_instructions
|
| 67 |
)
|
| 68 |
|
| 69 |
user_message = MessageModel(chatId=chat.id, author=Author.User, text=decoded_message)
|
|
|
|
| 53 |
empty_field_instructions = pick_empty_field_instructions(empty_field)
|
| 54 |
|
| 55 |
if empty_field == 'age':
|
| 56 |
+
response = await generate_next_question(empty_field_instructions, message_history_str)
|
| 57 |
else:
|
| 58 |
user_messages_str = prepare_user_messages_str(decoded_message, messages)
|
| 59 |
possible_entity_indexes, search_request = await asyncio.gather(
|
|
|
|
| 63 |
final_entities = await search_semantic_entities(search_request, entity_data, possible_entity_indexes)
|
| 64 |
final_entities_str = prepare_final_entities_str(final_entities)
|
| 65 |
response = await generate_final_response(
|
| 66 |
+
final_entities_str, decoded_message, message_history_str, empty_field_instructions
|
| 67 |
)
|
| 68 |
|
| 69 |
user_message = MessageModel(chatId=chat.id, author=Author.User, text=decoded_message)
|
trauma/api/message/ai/openai_request.py
CHANGED
|
@@ -23,12 +23,11 @@ async def update_entity_data_with_ai(entity_data: EntityData, user_message: str,
|
|
| 23 |
|
| 24 |
|
| 25 |
@openai_wrapper(temperature=0.8)
|
| 26 |
-
async def generate_next_question(
|
| 27 |
messages = [
|
| 28 |
{
|
| 29 |
"role": "system",
|
| 30 |
"content": TraumaPrompts.generate_next_question
|
| 31 |
-
.replace("{empty_field}", empty_field)
|
| 32 |
.replace("{instructions}", instructions)
|
| 33 |
.replace("{message_history}", message_history_str)
|
| 34 |
}
|
|
@@ -51,12 +50,11 @@ async def generate_search_request(user_messages_str: str, entity_data: dict):
|
|
| 51 |
|
| 52 |
@openai_wrapper(temperature=0.8)
|
| 53 |
async def generate_final_response(
|
| 54 |
-
final_entities: str, user_message: str, message_history_str: str, empty_field_instructions: str
|
| 55 |
):
|
| 56 |
if empty_field_instructions:
|
| 57 |
prompt = (TraumaPrompts.generate_not_fully_recommendations
|
| 58 |
-
.replace("{instructions}", empty_field_instructions)
|
| 59 |
-
.replace("{empty_field}", empty_field))
|
| 60 |
elif json.loads(final_entities)['klinieken']:
|
| 61 |
prompt = (TraumaPrompts.generate_recommendation_decision
|
| 62 |
.replace("{final_entities}", final_entities))
|
|
@@ -131,7 +129,7 @@ async def generate_invalid_response(user_message: str, message_history_str: str,
|
|
| 131 |
|
| 132 |
if empty_field:
|
| 133 |
empty_field_instructions = pick_empty_field_instructions(empty_field)
|
| 134 |
-
prompt = TraumaPrompts.generate_invalid_response.replace("{
|
| 135 |
else:
|
| 136 |
prompt = TraumaPrompts.generate_invalid_response_with_recs
|
| 137 |
messages = [
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
@openai_wrapper(temperature=0.8)
|
| 26 |
+
async def generate_next_question(instructions: str, message_history_str: str):
|
| 27 |
messages = [
|
| 28 |
{
|
| 29 |
"role": "system",
|
| 30 |
"content": TraumaPrompts.generate_next_question
|
|
|
|
| 31 |
.replace("{instructions}", instructions)
|
| 32 |
.replace("{message_history}", message_history_str)
|
| 33 |
}
|
|
|
|
| 50 |
|
| 51 |
@openai_wrapper(temperature=0.8)
|
| 52 |
async def generate_final_response(
|
| 53 |
+
final_entities: str, user_message: str, message_history_str: str, empty_field_instructions: str
|
| 54 |
):
|
| 55 |
if empty_field_instructions:
|
| 56 |
prompt = (TraumaPrompts.generate_not_fully_recommendations
|
| 57 |
+
.replace("{instructions}", empty_field_instructions))
|
|
|
|
| 58 |
elif json.loads(final_entities)['klinieken']:
|
| 59 |
prompt = (TraumaPrompts.generate_recommendation_decision
|
| 60 |
.replace("{final_entities}", final_entities))
|
|
|
|
| 129 |
|
| 130 |
if empty_field:
|
| 131 |
empty_field_instructions = pick_empty_field_instructions(empty_field)
|
| 132 |
+
prompt = TraumaPrompts.generate_invalid_response.replace("{instructions}", empty_field_instructions)
|
| 133 |
else:
|
| 134 |
prompt = TraumaPrompts.generate_invalid_response_with_recs
|
| 135 |
messages = [
|
trauma/api/message/ai/prompts.py
CHANGED
|
@@ -36,10 +36,10 @@ Je verzamelt informatie over een patiënt, hun ziekte en de behandelmethode zoda
|
|
| 36 |
}
|
| 37 |
```
|
| 38 |
|
| 39 |
-
- **age**: leeftijd van de
|
| 40 |
-
- **treatmentArea**: Het type
|
| 41 |
-
- **treatmentMethod**:
|
| 42 |
-
- **location**:
|
| 43 |
- **postalCode**: Postcode van de facility.
|
| 44 |
|
| 45 |
## Regels voor het bijwerken van Entity Data
|
|
@@ -59,11 +59,9 @@ Je bent een informaticus die gegevens verzamelt over de patiënt en de gewenste
|
|
| 59 |
|
| 60 |
**Gemist veld**:
|
| 61 |
```
|
| 62 |
-
{
|
| 63 |
```
|
| 64 |
|
| 65 |
-
- {instructions}
|
| 66 |
-
|
| 67 |
**Gespreksgeschiedenis**:
|
| 68 |
```
|
| 69 |
{message_history}
|
|
@@ -84,11 +82,9 @@ Je bent een informatieve assistent die gegevens verzamelt over de patiënt en de
|
|
| 84 |
|
| 85 |
**Ontbrekend veld**:
|
| 86 |
```
|
| 87 |
-
{
|
| 88 |
```
|
| 89 |
|
| 90 |
-
- {instructions}
|
| 91 |
-
|
| 92 |
**Berichten geschiedenis**:
|
| 93 |
```
|
| 94 |
{message_history}
|
|
@@ -193,7 +189,7 @@ Je moet de gebruiker vriendelijk laten weten dat hun verzoek (`Gebruikersquery`)
|
|
| 193 |
|
| 194 |
**Verplichte veld**:
|
| 195 |
```
|
| 196 |
-
{
|
| 197 |
```
|
| 198 |
|
| 199 |
## Belangrijke opmerkingen
|
|
|
|
| 36 |
}
|
| 37 |
```
|
| 38 |
|
| 39 |
+
- **age**: De leeftijd van het kind of de jongere waarvoor hulp wordt gezocht, uitgedrukt in jaren (bijvoorbeeld 4, 10 of 15 jaar).
|
| 40 |
+
- **treatmentArea**: Het type trauma of onderliggende problematiek waarmee het kind of de jongere te maken heeft, zoals hechtingsproblemen, verlieservaringen, mishandeling, seksueel misbruik of verwaarlozing.
|
| 41 |
+
- **treatmentMethod**: De behandelmethode die wordt ingezet of gezocht bij het kind of de jongere, zoals EMDR, speltherapie, cognitieve gedragstherapie of de Slapende Honden-methode.
|
| 42 |
+
- **location**: De gewenste locatie voor de behandeling, bijvoorbeeld een stad, wijk, of regio waar de zorgverlener zich bij voorkeur bevindt.
|
| 43 |
- **postalCode**: Postcode van de facility.
|
| 44 |
|
| 45 |
## Regels voor het bijwerken van Entity Data
|
|
|
|
| 59 |
|
| 60 |
**Gemist veld**:
|
| 61 |
```
|
| 62 |
+
{instructions}
|
| 63 |
```
|
| 64 |
|
|
|
|
|
|
|
| 65 |
**Gespreksgeschiedenis**:
|
| 66 |
```
|
| 67 |
{message_history}
|
|
|
|
| 82 |
|
| 83 |
**Ontbrekend veld**:
|
| 84 |
```
|
| 85 |
+
{instructions}
|
| 86 |
```
|
| 87 |
|
|
|
|
|
|
|
| 88 |
**Berichten geschiedenis**:
|
| 89 |
```
|
| 90 |
{message_history}
|
|
|
|
| 189 |
|
| 190 |
**Verplichte veld**:
|
| 191 |
```
|
| 192 |
+
{instructions}
|
| 193 |
```
|
| 194 |
|
| 195 |
## Belangrijke opmerkingen
|
trauma/api/message/utils.py
CHANGED
|
@@ -54,15 +54,16 @@ def prepare_final_entities_str(entities: list[EntityModel]) -> str:
|
|
| 54 |
|
| 55 |
def pick_empty_field_instructions(empty_field: str) -> str:
|
| 56 |
if empty_field == "age":
|
| 57 |
-
return "
|
| 58 |
elif empty_field == "treatmentMethod":
|
| 59 |
-
return "
|
| 60 |
elif empty_field == "location":
|
| 61 |
-
return "
|
| 62 |
elif empty_field == "treatmentArea":
|
| 63 |
-
return "
|
| 64 |
return None
|
| 65 |
|
|
|
|
| 66 |
def find_matching_age_group(entity: EntityModel, entity_data: dict) -> AgeGroup:
|
| 67 |
age_groups = entity.ageGroups
|
| 68 |
best_match = None
|
|
|
|
| 54 |
|
| 55 |
def pick_empty_field_instructions(empty_field: str) -> str:
|
| 56 |
if empty_field == "age":
|
| 57 |
+
return "De leeftijd van het kind of de jongere waarvoor hulp wordt gezocht, uitgedrukt in jaren (bijvoorbeeld 4, 10 of 15 jaar)."
|
| 58 |
elif empty_field == "treatmentMethod":
|
| 59 |
+
return "De behandelmethode die wordt ingezet of gezocht bij het kind of de jongere, zoals EMDR, speltherapie, cognitieve gedragstherapie of de Slapende Honden-methode."
|
| 60 |
elif empty_field == "location":
|
| 61 |
+
return "De gewenste locatie voor de behandeling, bijvoorbeeld een stad, wijk, of regio waar de zorgverlener zich bij voorkeur bevindt."
|
| 62 |
elif empty_field == "treatmentArea":
|
| 63 |
+
return "Het type trauma of onderliggende problematiek waarmee het kind of de jongere te maken heeft, zoals hechtingsproblemen, verlieservaringen, mishandeling, seksueel misbruik of verwaarlozing."
|
| 64 |
return None
|
| 65 |
|
| 66 |
+
|
| 67 |
def find_matching_age_group(entity: EntityModel, entity_data: dict) -> AgeGroup:
|
| 68 |
age_groups = entity.ageGroups
|
| 69 |
best_match = None
|