Skip to content

Integrate Memory Modules into Your AI Assistant

Overview

Memory modules are crucial components in building AI assistants that can retain context, personalize interactions, and provide coherent responses over time. By integrating memory modules, your assistant can remember previous conversations, utilize environment variables, and retrieve relevant information to enhance user experience. This tutorial will guide you through configuring and using memory modules.

Configuration

To leverage memory modules in your assistant, you need to define their configurations in a YAML file. This configuration specifies the types of memory modules and their parameters, allowing your assistant to store and access different kinds of information.

Configuration Parameters

  • memory_config: Top-level key for memory module configurations.
  • memory_modules: A list of memory modules to integrate.
    • memory_name: A unique identifier for the memory module.
    • memory_class: The class name of the memory module.
    • kwargs: Additional parameters specific to the memory module.

Here’s an example configuration (config.yaml):

memory_config:  
  memory_modules:  
    - memory_name: chat_history  
      memory_class: ChatMemoryModule  
      kwargs:  
        n_rounds: 5  

    - memory_name: env_variable  
      memory_class: VariableMemoryModule  
      kwargs:  
        variables:  
          campaign_title: "Life Trends"  
          campaign_year: "2025"  
          company_name: "Accenture"  
          competitors: "Deloitte, McKinsey, KPMG"  

orchestrator:  
  agent_list:  
    - agent_name: "Search Agent"  
    - agent_name: "Data Scientist Agent"  

utility_agents:  
  - agent_class: SearchAgent  
    agent_name: "Search Agent"  

  - agent_class: CustomAgent  
    agent_name: "Data Scientist Agent"  
    agent_description: "The data scientist can help you perform data analytics."  
    config: {}  

Memory Modules Explained

Chat History Memory Module (ChatMemoryModule)

  • Purpose: Stores previous conversation rounds to maintain context.
  • Parameters:
  • n_rounds: The number of previous conversation rounds to remember.

Environment Variable Memory Module (VariableMemoryModule)

  • Purpose: Stores environment variables that can be used to personalize responses.
  • Parameters:
  • variables: A dictionary of key-value pairs representing environment variables.

Example Usage

This section demonstrates how to use memory modules in your AI assistant through code examples.

Define Your Custom Python Agent

First, define a custom agent that utilizes the memory modules. For example, a simple agent that responds based on user queries, environment variables, and chat history:

import os  
import asyncio  
from openai import AsyncOpenAI  
from air import login, DistillerClient  

# Authenticate credentials  
auth = login(  
    account=str(os.getenv("ACCOUNT")),  
    api_key=str(os.getenv("API_KEY")),  
)  

async def simple_agent(query: str, **kwargs):  
    """  
    A simple agent that generates responses based on user queries,  
    environment variables, and chat history.  

    Args:  
        query (str): User query to be processed.  
        **kwargs: Optional memory components like 'chat_history' and 'env_variable'.  

    Returns:  
        str: Assistant's response.  
    """  
    # Extract memory content from kwargs  
    chat_history = kwargs.get('chat_history', '')  
    env_variables = kwargs.get('env_variable', '')  

    # Construct the prompt with environment variables and chat history  
    prompt = f"""  
You are a helpful personal assistant. Use the user's environment variables and previous conversation to provide a personalized and relevant response to the user's query. Ensure that your response is informed by their preferences and past interactions.  

Environment Variables:  
{env_variables}  

Chat History:  
{chat_history}  

User Query:  
{query}  

Assistant:"""  

    # Initialize the OpenAI client using the authenticated credentials  
    client = AsyncOpenAI(**auth.openai())  

    # Get the response from the language model  
    response = await client.chat.completions.create(  
        messages=[{"role": "user", "content": prompt}],  
        model="gpt-3.5-turbo",  
    )  

    # Return the assistant's response  
    return response.choices[0].message.content.strip()  

Initialize the Distiller Client and Add Memory Modules

Next, initialize the DistillerClient, create a project, define custom agent mappings, and add memory entries.

async def custom_demo():  
    """  
    Demonstrates the use of environment variables and chat history as memory in an AI assistant.  
    """  
    # Initialize a Distiller client for managing interactions  
    distiller_client = DistillerClient()  

    # Create/register a new Distiller project  
    distiller_client.create_project(config_path="config.yaml", project="memory")  

    # Define custom agent mappings  
    custom_agent_gallery = {  
        "Data Scientist Agent": simple_agent,  
    }  

    # Use the Distiller client  
    async with distiller_client(  
        project="memory",  
        uuid="test_user_memory",  
        custom_agent_gallery=custom_agent_gallery,  
    ) as dc:  

        # Add environment variables to the memory  
        await dc.add_memory(  
            source="env_variable",  
            variables_dict={  
                "campaign_description": "Accenture's Life Trends campaign focuses on identifying and analyzing key trends that are shaping consumer behavior and expectations. These trends are often driven by technological advancements, societal changes, and shifting cultural norms.",  
                "preferred_language": "Spanish",  
            },  
        )  
        print("\n[INFO] Environment variables added to memory.")  

        # List of user queries to process  
        queries = [  
            "Share some data about life trends.",  
            "Gather data on recent consumer behaviors.",  
            "What are the key trends shaping consumer behavior?",  
            "What year is the life trends campaign from?",  
        ]  

        print("\n\n=== Custom Demo ===")  
        for query in queries:  
            responses = await dc.query(query=query)  
            print("-------------------------")  
            print(f"\nUser Query: {query}")  
            async for response in responses:  
                print(f"Response:\n{response['content']}")  

Explanation

  • DistillerClient: Manages interactions with the Distiller framework.
  • create_project: Initializes a new project using the configuration file.
  • custom_agent_gallery: Maps the agent name to the custom agent function.
  • add_memory: Adds environment variables to the assistant's memory under the specified source (env_variable).
  • Processing Queries: The assistant processes each query, accessing memory modules to generate informed responses.

Expected Output

After running the custom_demo function, the assistant should provide responses that utilize both the environment variables and chat history. The output might look like:

[INFO] Environment variables added to memory.  

=== Custom Demo ===
-------------------------

User Query: Share some data about life trends.
Response:
¡Hola! I see you're interested in learning more about life trends. Based on our previous conversations, I'll provide you with some updated insights.

In the context of Accenture's Life Trends campaign, our research has identified several key trends that are shaping consumer behavior and expectations in 2025. Here are a few data points that might interest you:

1. **Sustainable Living**: ...
2. **Digital Wellness**: ...
3. **Personalization**: ...

These trends are driven by technological advancements, societal changes, and shifting cultural norms. As a leader in the industry, Accenture is well-positioned to help clients like yours navigate these changes and capitalize on emerging opportunities.

Would you like me to elaborate on any of these trends or explore other topics related to life trends?
-------------------------

User Query: Gather data on recent consumer behaviors.
Response:
¡Hola! I'd be happy to provide you with some recent data on consumer behaviors, tailored to the Spanish market and informed by Accenture's Life Trends campaign.

Based on our latest research, here are some key findings on recent consumer behaviors in Spain:

1. **E-commerce growth**: ...
2. **Social media influence**: ...
3. **Experiential consumption**: ...
4. **Health and wellness**: ...
5. **Sustainable consumption**: ...

These consumer behaviors are shaped by the trends I mentioned earlier, such as sustainable living, digital wellness, and personalization. As a leader in the industry, Accenture can help clients like yours understand and respond to these changing consumer behaviors.

Would you like me to dive deeper into any of these findings or explore other topics related to consumer behaviors?
-------------------------

User Query: What are the key trends shaping consumer behavior?
Response:
¡Hola de nuevo! I'm happy to provide more information on the key trends shaping consumer behavior, as identified by Accenture's Life Trends campaign.

As I mentioned earlier, our research has pinpointed several trends that are driving changes in consumer behavior. Here are some of the most significant ones:

1. **Sustainable living**: ...
2. **Digital wellness**: ...
3. **Personalization**: ...
4. **Experiential consumption**: ...
5. **Health and wellness**: Consumers are increasingly prioritizing their physical and mental well-being, seeking out products and services that promote healthy living and self-care.

These trends are not only shaping consumer behavior but also influencing business strategies and market dynamics. As a leader in the industry, Accenture can help clients like yours navigate these changes and capitalize on emerging opportunities.

Would you like me to explore any of these trends in more depth or discuss how Accenture can help your organization respond to these changing consumer behaviors?
-------------------------

User Query: What year is the life trends campaign from?
Response:
¡Hola! According to our previous conversation, the Life Trends campaign is from 2025. ¿Hay algo más en lo que pueda ayudarte?

Advanced Memory Retrieval with Search Demo

You can further explore the capabilities of memory modules with advanced querying and memory retrieval:

async def search_demo():  
    """  
    Demonstrates advanced querying and memory retrieval capabilities of the assistant.  
    """  
    # Initialize a Distiller client  
    distiller_client = DistillerClient()  

    # Create/register a new Distiller project  
    distiller_client.create_project(config_path="config.yaml", project="memory")  

    # Define custom agent mappings  
    custom_agent_gallery = {  
        "Data Scientist Agent": simple_agent,  
    }  

    # Use the Distiller client  
    async with distiller_client(  
        project="memory",  
        uuid="test_user_memory",  
        custom_agent_gallery=custom_agent_gallery,  
    ) as dc:  

        # List of user queries to process  
        queries = [  
            "What are current trends relevant to the campaign?",  
            "Research what our competitors are focusing on.",  
        ]  

        print("\n\n=== Search Demo ===")  
        for query in queries:  
            responses = await dc.query(query=query)  
            print("-------------------------")  
            print(f"\nUser Query: {query}")  
            async for response in responses:  
                print(f"Response:\n{response['content']}")  

        print("-------------------------")  
        # Retrieve chat history  
        retrieved_chat_history = await dc.retrieve_memory(  
            source="chat_history", n_rounds=3  
        )  
        print(f"\n[INFO] Retrieved Chat History:\n{retrieved_chat_history}\n")  

Explanation

  • Processing Advanced Queries: The assistant handles more complex queries, utilizing memory modules for informed responses.
  • retrieve_memory: Fetches the last n_rounds of chat history, demonstrating how memory can be accessed and displayed.

Expected Output

The assistant leverages memory modules to provide detailed responses and retrieves recent chat history:

=== Search Demo ===  
-------------------------  

User Query: What are current trends relevant to the campaign?
Response:
Searching over Web Search.
Response:
# Current Trends Relevant to the Life Trends Campaign

The current trends relevant to the Life Trends campaign in 2025 encompass various aspects of technology, society, and human behavior. Some key trends include:

### Technological Advancements

* Increased use of artificial intelligence (AI) and automation, potentially leading to the elimination of certain jobs and the creation of new ones [1].
* Advancements in remote work technologies, enabling more people to work from home and changing the nature of the traditional office environment [2].
* Growing importance of data privacy and security, with individuals becoming more aware of the need to protect their personal information [3].

...

These trends are likely to shape the Life Trends campaign in 2025 and beyond, as individuals, companies, and societies navigate the complexities of a rapidly changing world.

## References

[1] Accenture. (2024). Accenture Life Trends 2024. Retrieved from <https://www.accenture.com/content/dam/accenture/com/.../Accenture-Life-Trends-2024-Full-Report.pdf>

[2] Pew Research Center. (2021). Experts say the 'new normal' in 2025 will be far more tech-driven, presenting big challenges. Retrieved from <https://www.pewresearch.org/internet/2021/02/18/experts-say-new-normal-in-2025-far-more-tech-driven-presenting-big-challenges/>

[3] Kantar. (n.d.). Marketing trends: What marketers need to know. Retrieved from <https://www.kantar.com/campaigns/marketing-trends>

-------------------------

User Query: Research what our competitors are focusing on.
Response:
Searching over Web Search.
Response:
Our competitors are focusing on various areas to stay ahead in the industry. Deloitte is emphasizing the importance of accelerating the adoption of automation and machine learning tools to digitize manual, paper-based processes in the banking sector [1]. They also highlight the need for banks to modernize their legacy infrastructure to harness the transformative power of AI [2].

McKinsey, on the other hand, is exploring the potential of Web3 and its impact on various industries, including finance, healthcare, and education [3]. ...

Overall, our competitors are focusing on leveraging technology to drive innovation, improve operational efficiency, and enhance customer experiences, while also prioritizing sustainability and social responsibility.

## References

[1] Deloitte. (2024). Banking Outlook. Retrieved from <https://www2.deloitte.com/insights/industry/financial-services/outlooks/banking-outlook>

[2] Deloitte. (2024). Financial Services Outlooks. Retrieved from <https://www2.deloitte.com/insights/industry/financial-services/outlooks>

[3] McKinsey. (2022). Tech Trends Outlook 2022. Retrieved from <https://www.mckinsey.com/business-functions/digital-mckinsey/our-insights/top-trends-in-tech>

-------------------------

[INFO] Retrieved Chat History:
Search Agent:
# Current Trends Relevant to the Life Trends Campaign

The current trends relevant to the Life Trends campaign in 2025 encompass various aspects of technology, society, and human behavior. Some key trends include:

### Technological Advancements

* Increased use of artificial intelligence (AI) and automation, potentially leading to the elimination of certain jobs and the creation of new ones [1].
* Advancements in remote work technologies, enabling more people to work from home and changing the nature of the traditional office environment [2].
* Growing importance of data privacy and security, with individuals becoming more aware of the need to protect their personal information [3].

...

These trends are likely to shape the Life Trends campaign in 2025 and beyond, as individuals, companies, and societies navigate the complexities of a rapidly changing world.

## References

[1] Accenture. (2024). Accenture Life Trends 2024. Retrieved from <https://www.accenture.com/content/dam/accenture/com/.../Accenture-Life-Trends-2024-Full-Report.pdf>

[2] Pew Research Center. (2021). Experts say the 'new normal' in 2025 will be far more tech-driven, presenting big challenges. Retrieved from <https://www.pewresearch.org/internet/2021/02/18/experts-say-new-normal-in-2025-far-more-tech-driven-presenting-big-challenges/>

[3] Kantar. (n.d.). Marketing trends: What marketers need to know. Retrieved from <https://www.kantar.com/campaigns/marketing-trends>


user:
Research what our competitors are focusing on.

Search Agent:
Our competitors are focusing on various areas to stay ahead in the industry. Deloitte is emphasizing the importance of accelerating the adoption of automation and machine learning tools to digitize manual, paper-based processes in the banking sector [1]. They also highlight the need for banks to modernize their legacy infrastructure to harness the transformative power of AI [2].

McKinsey, on the other hand, is exploring the potential of Web3 and its impact on various industries, including finance, healthcare, and education [3]....

Overall, our competitors are focusing on leveraging technology to drive innovation, improve operational efficiency, and enhance customer experiences, while also prioritizing sustainability and social responsibility.

## References

[1] Deloitte. (2024). Banking Outlook. Retrieved from <https://www2.deloitte.com/insights/industry/financial-services/outlooks/banking-outlook>

[2] Deloitte. (2024). Financial Services Outlooks. Retrieved from <https://www2.deloitte.com/insights/industry/financial-services/outlooks>

Next Steps

  • Expand Memory Modules: Explore other memory modules or create custom ones to fit specific use cases.
  • Integrate Additional Agents: Incorporate more utility agents to handle diverse tasks and queries.
  • Enhance Personalization: Collect and utilize more user-specific data (with appropriate consent) to further personalize interactions.