Spaces:
Sleeping
Sleeping
fix
Browse files- trauma/api/data/db_requests.py +20 -0
- trauma/api/data/dto.py +0 -1
- trauma/api/data/schemas.py +8 -1
- trauma/api/data/views.py +14 -2
- trauma/api/message/ai/engine.py +9 -4
trauma/api/data/db_requests.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import HTTPException
|
| 2 |
|
| 3 |
from trauma.api.data.model import EntityModel
|
|
|
|
| 4 |
from trauma.core.config import settings
|
| 5 |
|
| 6 |
|
|
@@ -15,3 +19,19 @@ async def get_all_model_obj() -> list[EntityModel]:
|
|
| 15 |
sort_v = -1
|
| 16 |
objects = await settings.DB_CLIENT.entities.find({}, {"embedding": 0}).sort("_id", sort_v).to_list(length=None)
|
| 17 |
return objects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
from fastapi import HTTPException
|
| 5 |
|
| 6 |
from trauma.api.data.model import EntityModel
|
| 7 |
+
from trauma.api.data.schemas import SearchRequest
|
| 8 |
from trauma.core.config import settings
|
| 9 |
|
| 10 |
|
|
|
|
| 19 |
sort_v = -1
|
| 20 |
objects = await settings.DB_CLIENT.entities.find({}, {"embedding": 0}).sort("_id", sort_v).to_list(length=None)
|
| 21 |
return objects
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
async def search_facilities_obj(data: SearchRequest) -> tuple[list[EntityModel], int]:
|
| 25 |
+
skip = data.pageSize * data.pageIndex
|
| 26 |
+
sort_v = -1
|
| 27 |
+
regex_filter = {"name": {"$regex": f"^{re.escape(data.name)}", "$options": "i"}}
|
| 28 |
+
objects, total_count = await asyncio.gather(
|
| 29 |
+
settings.DB_CLIENT.entities
|
| 30 |
+
.find(regex_filter, {"embeddings": 0})
|
| 31 |
+
.sort("_id", sort_v)
|
| 32 |
+
.skip(skip)
|
| 33 |
+
.limit(data.pageSize)
|
| 34 |
+
.to_list(length=data.pageSize),
|
| 35 |
+
settings.DB_CLIENT.entities.count_documents(regex_filter)
|
| 36 |
+
)
|
| 37 |
+
return [EntityModel.from_mongo(ent) for ent in objects], total_count
|
trauma/api/data/dto.py
CHANGED
|
@@ -11,4 +11,3 @@ class ContactDetails(BaseModel):
|
|
| 11 |
website: str | None = None
|
| 12 |
address: str | None = None
|
| 13 |
postalCode: str | None = None
|
| 14 |
-
|
|
|
|
| 11 |
website: str | None = None
|
| 12 |
address: str | None = None
|
| 13 |
postalCode: str | None = None
|
|
|
trauma/api/data/schemas.py
CHANGED
|
@@ -9,5 +9,12 @@ class AllFacilitiesResponse(BaseModel):
|
|
| 9 |
paging: Paging
|
| 10 |
data: list[EntityModel]
|
| 11 |
|
|
|
|
| 12 |
class AllFacilitiesWrapper(TraumaResponseWrapper[AllFacilitiesResponse]):
|
| 13 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
paging: Paging
|
| 10 |
data: list[EntityModel]
|
| 11 |
|
| 12 |
+
|
| 13 |
class AllFacilitiesWrapper(TraumaResponseWrapper[AllFacilitiesResponse]):
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class SearchRequest(BaseModel):
|
| 18 |
+
name: str
|
| 19 |
+
pageSize: int
|
| 20 |
+
pageIndex: int
|
trauma/api/data/views.py
CHANGED
|
@@ -4,9 +4,9 @@ from trauma.api.account.dto import AccountType
|
|
| 4 |
from trauma.api.account.model import AccountModel
|
| 5 |
from trauma.api.common.dto import Paging
|
| 6 |
from trauma.api.data import facility_router
|
| 7 |
-
from trauma.api.data.db_requests import get_facility_by_id, get_all_model_obj
|
| 8 |
from trauma.api.data.model import EntityModel
|
| 9 |
-
from trauma.api.data.schemas import AllFacilitiesWrapper, AllFacilitiesResponse
|
| 10 |
from trauma.core.security import PermissionDependency
|
| 11 |
from trauma.core.wrappers import TraumaResponseWrapper
|
| 12 |
|
|
@@ -23,6 +23,18 @@ async def get_all_countries(
|
|
| 23 |
return AllFacilitiesWrapper(data=response)
|
| 24 |
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@facility_router.get('/{facilityId}')
|
| 27 |
async def get_country(
|
| 28 |
facilityId: str,
|
|
|
|
| 4 |
from trauma.api.account.model import AccountModel
|
| 5 |
from trauma.api.common.dto import Paging
|
| 6 |
from trauma.api.data import facility_router
|
| 7 |
+
from trauma.api.data.db_requests import get_facility_by_id, get_all_model_obj, search_facilities_obj
|
| 8 |
from trauma.api.data.model import EntityModel
|
| 9 |
+
from trauma.api.data.schemas import AllFacilitiesWrapper, AllFacilitiesResponse, SearchRequest
|
| 10 |
from trauma.core.security import PermissionDependency
|
| 11 |
from trauma.core.wrappers import TraumaResponseWrapper
|
| 12 |
|
|
|
|
| 23 |
return AllFacilitiesWrapper(data=response)
|
| 24 |
|
| 25 |
|
| 26 |
+
@facility_router.post('/search')
|
| 27 |
+
async def search_facilities(
|
| 28 |
+
data: SearchRequest,
|
| 29 |
+
) -> AllFacilitiesWrapper:
|
| 30 |
+
countries, total_count = await search_facilities_obj(data)
|
| 31 |
+
response = AllFacilitiesResponse(
|
| 32 |
+
paging=Paging(pageSize=data.pageSize, pageIndex=data.pageIndex, totalCount=total_count),
|
| 33 |
+
data=countries
|
| 34 |
+
)
|
| 35 |
+
return AllFacilitiesWrapper(data=response)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
@facility_router.get('/{facilityId}')
|
| 39 |
async def get_country(
|
| 40 |
facilityId: str,
|
trauma/api/message/ai/engine.py
CHANGED
|
@@ -8,10 +8,15 @@ from trauma.api.data.model import EntityModel, EntityModelExtended
|
|
| 8 |
from trauma.api.message.ai.openai_request import (update_entity_data_with_ai,
|
| 9 |
generate_next_question,
|
| 10 |
generate_search_request,
|
| 11 |
-
generate_final_response,
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from trauma.api.message.db_requests import (save_assistant_user_message,
|
| 16 |
filter_entities_by_age_location,
|
| 17 |
update_entity_data_obj, get_entity_by_index)
|
|
|
|
| 8 |
from trauma.api.message.ai.openai_request import (update_entity_data_with_ai,
|
| 9 |
generate_next_question,
|
| 10 |
generate_search_request,
|
| 11 |
+
generate_final_response,
|
| 12 |
+
convert_value_to_embeddings,
|
| 13 |
+
choose_closest_treatment_method,
|
| 14 |
+
choose_closest_treatment_area,
|
| 15 |
+
check_is_valid_request,
|
| 16 |
+
generate_invalid_response,
|
| 17 |
+
set_entity_score,
|
| 18 |
+
retrieve_semantic_answer,
|
| 19 |
+
generate_searched_entity_response)
|
| 20 |
from trauma.api.message.db_requests import (save_assistant_user_message,
|
| 21 |
filter_entities_by_age_location,
|
| 22 |
update_entity_data_obj, get_entity_by_index)
|