[Python] FastAPI Small Talk Greeting Detection Returning Empty Response in TST Environment

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have implemented a greeting detection logic in my FastAPI application, which works correctly when tested via Postman but fails to return any response in the TST (Test) environment. The logic checks if a user query contains a greeting by comparing it with a predefined list of greeting keywords. If a match is found, it should return a structured response with a greeting message. However, in the TST environment, the function always returns an empty response and proceeds to the next step in the query handling process.

    I have verified that the greeting_keywords list is correctly loaded in both environments. When tested in Postman, the API correctly detects greetings like "Hello" or "Hi" and returns the expected response. However, in TST, even with the same input queries, the function does not detect greetings and does not trigger the expected response. Debugging logs indicate that the function does not identify any greeting, but there are no errors in the logs, and the API continues executing the next steps.

    The utility_greeting.py

    import re
    from small_talk_flow import greeting_keywords

    def detect_greeting(query):
    """Detects if the query is a greeting and returns an appropriate response."""
    query_lower = query.lower().strip()
    print(f"Received Query: {query_lower}") # Debugging

    for greeting in greeting_keywords:
    if greeting in query_lower: # ✅ More flexible check
    greeting_response = greeting_keywords[greeting]
    print(f"Greeting Matched: {greeting} -> {greeting_response}") # Debugging

    return {
    "response": greeting_response,
    "intent": "Greeting",
    "entity": ["No Entity Detected"],
    "search_results": ["No Search Results Found"]
    }

    print("No Greeting Detected") # Debugging
    return None

    # Build the final markdown summary
    def build_greeting_markdown(response_data):
    """Formats the greeting response as a markdown summary."""
    markdown_summary = f"""
    #### ** Greeting Response:**
    {response_data['response']}

    ---

    ## ** Intent Detection:**
    {response_data['intent']}

    ---

    ## **️ Entity Detection:**
    {', '.join(response_data['entity'])}

    ---
    """
    return markdown_summary

    def clean_text(text):
    """Removes special characters from the text."""
    return re.sub(r'[^a-zA-Z0-9\s]', '', text)


    the small_talk_flow.py is

    greeting_keywords = {
    # Basic Greetings
    "hello": "Hello! How can I assist you today?",
    "hi": "Hi there! What can I do for you?",
    "hey": "Hey! What can I do for you today?",
    "hi there": "Hi there! How's your day going?",
    "hey there": "Hey there! What do you need help with?",
    "hello there": "Hello there! How can I assist you?",
    "greetings": "Greetings! How can I be of service?",
    "salutations": "Salutations! How can I help today?",
    }


    the main where both of the py files are used is:

    @router.post("/v2", response_model=QueryResponse)
    async def query_handler(payload: QueryRequest, request: Request):
    start_time = time.time() # Track overall execution time
    logger.info(f"Received query: {payload.query} from user {payload.user_id}")

    try:
    func_start = time.time()
    user_data = request.state.user_data
    upn = user_data.get('upn')
    query = payload.query
    logger.info(f"Fetched user data in {time.time() - func_start:.4f} seconds")

    # Small Talk Logic - Greeting detection
    from small_talk_flow import greeting_keywords
    from app.utils.utility_greeting import detect_greeting, build_greeting_markdown
    query_lower = payload.query.lower().strip()

    # Check if the query contains any greeting
    for greeting in greeting_keywords:
    if greeting in query_lower:
    greeting_response = greeting_keywords[greeting] # Fetch the appropriate response
    logger.info(f"Greeting detected. Responding with: {greeting_response}")
    #return {"response": greeting_response, "intent": "Greeting", "entity": ["No Entity Detected"], "search_results": ["No Search Results Found"]}

    # Call the detect_greeting function from utility_greeting.py
    greeting_response_data = detect_greeting(payload.query)

    if greeting_response_data:
    logger.info(f"Greeting detected by detect_greeting. Responding with: {greeting_response_data['response']}")

    # Build markdown summary for greeting response
    markdown_summary = build_greeting_markdown(greeting_response_data)
    #print(markdown_summary)

    # Return response
    return {
    "response": greeting_response_data['response'],
    "intent": greeting_response_data['intent'],
    "entity": greeting_response_data['entity'],
    "search_results": greeting_response_data['search_results'],
    "markdown_summary": markdown_summary # Include the markdown summary in the response
    }


    #Step 1 : Get Chat data from redis
    func_start = time.time()
    user_chats = redis_client.get_last_chats(upn)
    logger.info(f"Fetched user chat data in {time.time() - func_start:.4f} seconds")


    What I Tried: I implemented a greeting detection mechanism within my API. The logic is defined in detect_greeting, which checks if a query contains any predefined greeting keywords from greeting_keywords. If a match is found, it returns a structured response with the detected greeting, intent, entities, and a markdown-formatted summary. I also added logging to verify that the function is being called and correctly processing queries. When testing through Postman, the API successfully detects greetings and returns the expected response. However, when deployed in the TST environment, the same logic does not return any response for greeting-related queries. Instead, it skips the greeting detection and proceeds with the next steps in the flow.

    Expected Outcome: The API should consistently detect greetings in both Postman and the TST environment. When a user sends a greeting query (e.g., "Hello" or "Hi"), the system should return the predefined response from greeting_keywords, along with the intent as "Greeting", an empty entity list, and a properly formatted markdown summary. I need help understanding why the greeting detection logic works in Postman but fails in the TST environment and how I can troubleshoot or fix this issue.

    Continue reading...

Compartilhe esta Página