File size: 2,177 Bytes
c169262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, status, HTTPException
from models.query import Query
from routes import search_products, purchase, order_management, account_management, customer_support
from services.nlp import recognize_intent, recognize_entities, extract_keywords, generate_response
from services.utils import clean_text, encode_and_normalize, extract_order_id_from_query


router = APIRouter()


FUNCTION_DESCRIPTIONS_FOR_PRODUCTS = {
    "search_products_by_keywords": "User wants to find products based on keywords",
    "search_products_by_filters": "User wants to refine search results with filters",
    "get_product_details": "User wants detailed information about a specific product"
}

FUNCTION_DESCRIPTIONS_FOR_ORDERS = {
    "get_order_location": "Find the location (city or state) of a specific order using an identification number order",
    "get_recent_order": "Track the most recent order of a customer",
    "get_order_details": "Get details about a specific order using an identification number order",
    "get_order_quantity": "Calculate the total number of products in a specific order",
    "get_order_amount": "Calculate the total amount spent in a specific order",
    "cancel_order": "Process order cancellation requests"
}


def query_processing(query: Query):
    cleaned_text = clean_text(query.text)
    query.intent = recognize_intent(cleaned_text)
    query.entities = recognize_entities(cleaned_text)
    query.keywords = extract_keywords(cleaned_text)
    encoded_query = encode_and_normalize(cleaned_text)

    if query.intent == "search for products":
        return {"products": search_products.handle_search_products_by_keywords(encoded_query)}

    elif query.intent == "order management":
        order_id = extract_order_id_from_query(query.text)
        if order_id:
            return order_management.handle_track_order(order_id)
        else:
            return "Please provide an Order Number"
    else:
        return None


@router.post("/")
async def handle_response(query: Query):
    context_from_elasticsearch = query_processing(query)
    return {"generative response": generate_response(query, context_from_elasticsearch)}