# 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.

```python
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**.

```python
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.

```python
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**.

```python
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.

```python
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.

```python
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.

```python
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`.

```python
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**.

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

***

### **Example API Request & Response**

#### **Request Payload**

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

#### **Response**

```json
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."
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.groclake.ai/grocagent/editor-2/workflow-of-reflection-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
