LogoLogo
  • 👋Welcome to Groclake
  • ⏩Jump right in
  • 🗣️Introduction to Groclake
  • 🧠High level Concepts
    • Agent Discovery
    • Agent Registry
    • Agent Communication
      • Agent Text Transfer Protocol - ATTP
    • Agent Security
      • Agent Private Cloud - APC
      • Authentication & Encryption
      • Zero Trust Policy
  • 💽Installation & Guide
  • 🏗️Groclake Use Cases
  • 📰Groclake Records
  • Example Codes
  • GrocAgent
    • What is GrocAgent?
    • Example Chat Agent
    • Reflections in GrocAgent
      • Workflow of Reflection Handler
  • Lakes
    • 💾Data & Model Management
      • Datalake
        • Create Datalake
        • Retrieve Document
        • Upload Documents
        • Datalake Connections
          • Snowflake integration
      • Vectorlake
        • Creating vector
        • Generating Vector
        • Pushing Vector
        • Retrieve Document
        • Searching Vector
      • Modellake
        • Create Modellake
        • Language Translation
        • Conversation AI
        • Text to Speech
        • Chat Completion
      • Knowledgelake
        • Create Knowledge Base
        • Push Documents from a URL
        • Push Documents from Local Storage
        • Searching for Information
    • ⚒️Tool Management & Gateway
      • Toollake
        • Tools
        • Salesforce CRM Integration
        • Slack Communication Module
        • New Relic Integration
        • Google Calendar Integration
          • Check Slot Availability
          • Get Available Slots
          • Delete Event
          • Create new event
          • Create new calendar event
    • 🤖Agent Management & Deployment
      • Agentlake
        • Register your agent
        • Fetch agent details & categories
        • Create Agent Private Cloud (APC)
        • Assign Agent Private Cloud (APC) to an Agent
      • Promptlake
        • Setting Connection & Initializing
        • Storing a Prompt
        • Fetching a Prompt
        • Example API Calls
      • Memorylake
        • Context Component Examples
        • Value Structure
        • Setup & Guide
        • Storing & Retrieving Memory
        • Wildcard Search
        • Updating Memory Quality
    • 🗃️Index Stores
      • Cataloglake
        • Create catalog
        • Generate Product Data
        • Fetch Catalog Data
        • Push Product Data
        • Optimize Data Retrieval with Catalog Caching
        • Search for Products
        • Filter Product Search
        • Update Product Data
        • Recommend Products Based on Product Name
        • Update Inventory in Catalog
        • Fetch Inventory Details from Catalog
        • Fetch Product Price
        • Update Product Price in Catalog
        • Cache Image in Catalog
        • Sync Your Catalog with external ecomm platforms
        • Deleting items
        • Address Parsing and Intent Extraction
        • Creating Mapper
        • Convert Mapper's Metadata
        • Fetching Mapper
        • Updating Mapper
        • Example use case of Cataloglake
      • Joblake
        • Joblake Mapping
        • Creating a Joblake
      • Resumelake
        • Resumelake Mapping
        • Creating a Resumelake
Powered by GitBook
On this page
  • Method: reflection_handler()
  • Step-by-Step Workflow
  • Example API Request & Response
  1. GrocAgent
  2. Reflections in GrocAgent

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."
}
PreviousReflections in GrocAgentNextData & Model Management

Last updated 2 months ago