Workflow of Reflection Handler

Method: reflection_handler()

The reflection_handler() method processes the incoming query and refines it based on previous interactions.

Step-by-Step Workflow

1. Validating the Input Query

  • Checks if query_text is present in the payload.

  • If missing, it returns an error response.

  • Ensures the Agent UUID is initialized before proceeding.

pythonCopyEditif not query_text:
    return {
        "query_text": query_text,
        "response_text": "Missing required fields"
    }

if not self._agent_self_uuid:
    return {
        "query_text": query_text,
        "response_text": "Missing client_agent_uuid in the init method"
    }

2. Fetching Bad Memories

  • Retrieves bad memories from MemoryLake using short_memory_read().

  • Extracts past query-response pairs with a reward score of -1.

Processing Bad Memories

  • Iterates over the fetched bad memories.

  • Extracts query_text and response_text.

  • Assigns a reward of -1 to negative past experiences.


3. Fetching Good Memories

  • Retrieves good memories using MemoryLake with a different memory_type.

  • Extracts positive query-response pairs with a reward score of +1.

Processing Good Memories

  • Iterates over retrieved positive experiences.

  • Assigns a reward of +1 to useful interactions.


4. Refining the Prompt

  • Constructs a chat payload with:

    • System message defining the AI’s goal.

    • User message with:

      • The new query.

      • Previously bad responses.

      • Previously good responses.


5. Generating a Refined Response

  • Calls chat_complete() to generate a refined response.

  • If an error occurs, it returns an appropriate error message.

  • Extracts the refined answer from the response.

  • Saves the improved prompt using PromptLake.


6. Returning the Final Response

  • Returns the refined query and optimized response.


Example API Request & Response

Request Payload

Response

Last updated