Skip to content

Commit 589a62c

Browse files
committed
Added validation methods for API endpoints.
1 parent 5ce3a35 commit 589a62c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

app/backend/api/endpoints.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
from baml_handlers.extract_product_information_from_message_handler import extract_product_information_from_message
33
from models.request.create_chat_message_request import CreateChatMessageRequest
44
from models.response.create_chat_message_response import CreateChatMessageResponse
5-
from models.request.get_product_price_range_request import GetProductPriceRangeRequest
5+
from models.request.get_products_in_price_range_request import GetProductsInPriceRangeRequest
66
from core.similarity_helper import *
7-
from database.data_helpers import *
7+
from database.data_helpers import is_product_description_valid, is_price_range_valid, apply_mask_for_df, get_products_summary
88
from database.in_memory_db import products_embeddings
99
from core.constants import AssistantMessages
10+
from core.constants import *
1011

1112
@app.post("/v1/chat/message/")
1213
async def handle_new_chat_message_from_user(request: CreateChatMessageRequest):
1314
extracted_chat_gpt_request = await extract_product_information_from_message(request.text)
1415
product_description = extracted_chat_gpt_request.description
1516
product_price_range = extracted_chat_gpt_request.price_range
17+
global products
1618
products = products_embeddings
1719
filter_was_active = False
1820

@@ -24,7 +26,7 @@ async def handle_new_chat_message_from_user(request: CreateChatMessageRequest):
2426
products = apply_mask_for_df(products, product_price_range)
2527
filter_was_active = True
2628

27-
if len(products) is 0 or filter_was_active is False:
29+
if len(products) == 0 or filter_was_active is False:
2830
return CreateChatMessageResponse(text=AssistantMessages.ASSISTANT_MESSAGE_PRODUCT_SIMILARITY_SEARCH_FAILURE)
2931

3032
product_summary = get_products_summary(products)
@@ -35,7 +37,7 @@ async def handle_product_description_request(request: CreateChatMessageRequest):
3537
extracted_chat_gpt_request = await extract_product_information_from_message(request.text)
3638
product_description = extracted_chat_gpt_request.description
3739

38-
if not is_product_description_valid(product_description):
40+
if is_product_description_valid(product_description) is False:
3941
return CreateChatMessageResponse(text=AssistantMessages.ASSISTANT_MESSAGE_INVALID_DESCRIPTION)
4042

4143
products = get_similarity_df(product_description, products_embeddings, request.similarities_results_max_count, request.similarities_results_threshold, OPENAI.EMBEDDING_MODEL)
@@ -46,16 +48,16 @@ async def handle_product_description_request(request: CreateChatMessageRequest):
4648
return CreateChatMessageResponse(text=product_summary)
4749

4850
@app.post("/v1/chat/product/price/")
49-
async def handle_product_price_range_request(request: GetProductPriceRangeRequest):
51+
async def handle_product_price_range_request(request: GetProductsInPriceRangeRequest):
5052
extracted_chat_gpt_request = await extract_product_information_from_message(request.text)
5153
product_price_range = extracted_chat_gpt_request.price_range
5254

53-
if not is_price_range_valid(product_price_range):
55+
if is_price_range_valid(product_price_range) is False:
5456
return CreateChatMessageResponse(text=AssistantMessages.ASSISTANT_MESSAGE_INVALID_PRICE_RANGE)
5557

5658
products = apply_mask_for_df(products_embeddings, product_price_range)
5759
if len(products) == 0:
58-
return CreateChatMessageResponse(text=AssistantMessages.ASSISTANT_MESSAGE_PRODUCT_SIMILARITY_SEARCH_FAILURE)
60+
return CreateChatMessageResponse(text=AssistantMessages.ASSISTANT_MESSAGE_INVALID_PRICE_RANGE)
5961

6062
product_summary = get_products_summary(products)
6163
return CreateChatMessageResponse(text=product_summary)

app/backend/core/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class General:
1616
class AssistantMessages:
1717
ASSISTANT_MESSAGE_PRODUCT_SIMILARITY_SEARCH_FAILURE = "We failed to find similar products from your search."
1818
ASSISTANT_MESSAGE_INVALID_DESCRIPTION = "We failed to found products that correspond to your description."
19+
ASSISTANT_MESSAGE_INVALID_PRICE_RANGE = "We failed to get products between the price range."
1920

2021
class OPENAI:
2122
API_KEY = str(os.environ['OPENAI_API_KEY'])

0 commit comments

Comments
 (0)