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.

pythonCopyEditmemory_context = {
    "context_id": self._agent_self_uuid,
    "memory_type": 0  # Fetch bad memories
}

bad_client_memory = memory_lake.short_memory_read(self._agent_self_uuid, memory_context, n=5)

Processing Bad Memories

  • Iterates over the fetched bad memories.

  • Extracts query_text and response_text.

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

bad_queries_responses = []
if isinstance(bad_client_memory, dict):
    for memory_key, memory_data in bad_client_memory.items():
        if isinstance(memory_data, dict):
            memory_query_text = memory_data.get('query_text', '')
            memory_response_text = memory_data.get('response_text', '')
            if memory_query_text and memory_response_text:
                bad_queries_responses.append((memory_query_text, memory_response_text, -1))

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.

memory_context = {
    "context_id": self._agent_self_uuid,
    "memory_type": 1  # Fetch good memories
}

good_client_memory = memory_lake.short_memory_read(self._agent_self_uuid, memory_context, n=5)

Processing Good Memories

  • Iterates over retrieved positive experiences.

  • Assigns a reward of +1 to useful interactions.

good_queries_responses = []
if isinstance(good_client_memory, dict):
    for memory_key, memory_data in good_client_memory.items():
        if isinstance(memory_data, dict):
            memory_query_text = memory_data.get('query_text', '')
            memory_response_text = memory_data.get('response_text', '')
            if memory_query_text and memory_response_text:
                good_queries_responses.append((memory_query_text, memory_response_text, 1))

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.

chat_payload = {
    "messages": [
        {
            "role": "system",
            "content": (
                f"You are an advanced assistant designed to refine prompts based on an agent's past experiences. "
                f"Your task is to improve the given prompt by considering both good and bad memories of the agent. "
                f"Use the reward values to guide the refinement process and optimize the prompt for high-quality responses."
            )
        },
        {
            "role": "user",
            "content": (
                f"Here is a prompt that needs refinement: {query_text}\n\n"
                f"### Past Experiences with Rewards\n\n"
                f"### Bad Memories (Reward: -1)\n"
                f"{bad_queries_responses_text}\n\n"
                f"### Good Memories (Reward: +1)\n"
                f"{good_queries_responses_text}\n\n"
                "Refine the prompt to maximize response quality while avoiding past mistakes."
            )
        }
    ]
}

5. Generating a Refined Response

  • Calls chat_complete() to generate a refined response.

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

try:
    chat_response = self.model.chat_complete(chat_payload)
except Exception as e:
    return {
        "query_text": query_text,
        "response_text": f"Error with chat completion: {str(e)}"
    }
  • Extracts the refined answer from the response.

  • Saves the improved prompt using PromptLake.

response_data = chat_response.get("answer", "")

try:
    promptlake.save_prompt(self._agent_self_uuid, response_data)
except Exception as e:
    return {
        "query_text": query_text,
        "response_text": f"Error saving prompt: {str(e)}"
    }

6. Returning the Final Response

  • Returns the refined query and optimized response.

return {
    "query_text": query_text,
    "response_text": response_data,
}

Example API Request & Response

Request Payload

Edit{
    "query_text": "How can I improve AI model accuracy?"
}

Response

Edit{
    "query_text": "How can I improve AI model accuracy?",
    "response_text": "To enhance AI model accuracy, consider data augmentation, hyperparameter tuning, and leveraging transfer learning techniques."
}

Last updated