Skip to main content
`Ctrl`+`K`

# Generative Agents in LangChain

This notebook implements a generative agent based on the paper Generative Agents: Interactive Simulacra of Human Behavior by Park, et. al.

In it, we leverage a time-weighted Memory object backed by a LangChain Retriever.

    # Use termcolor to make it easy to colorize the outputs.
    !pip install termcolor > /dev/null

    import logging
    logging.basicConfig(level=logging.ERROR)

    from datetime import datetime, timedelta
    from typing import List
    from termcolor import colored

    from langchain.chat_models import ChatOpenAI
    from langchain.docstore import InMemoryDocstore
    from langchain.embeddings import OpenAIEmbeddings
    from langchain.retrievers import TimeWeightedVectorStoreRetriever
    from langchain.vectorstores import FAISS

    USER_NAME = "Person A" # The name you want to use when interviewing the agent.
    LLM = ChatOpenAI(max_tokens=1500) # Can be any LLM you want.

## Generative Agent Memory Components

This tutorial highlights the memory of generative agents and its impact on their behavior. The memory varies from standard LangChain Chat memory in two aspects:

1. Memory Formation

   Generative Agents have extended memories, stored in a single stream:

   1. Observations - from dialogues or interactions with the virtual world, about self or others

   2. Reflections - resurfaced and summarized core memories

2. Memory Recall

   Memories are retrieved using a weighted sum of salience, recency, and importance.

You can review the definitions of the `GenerativeAgent` and `GenerativeAgentMemory` in the reference documentation for the following imports, focusing on `add_memory` and `summarize_related_memories` methods.

    from langchain.experimental.generative_agents import GenerativeAgent, GenerativeAgentMemory

## Memory Lifecycle

Summarizing the key methods in the above: `add_memory` and `summarize_related_memories`.

When an agent makes an observation, it stores the memory:

1. Language model scores the memory’s importance (1 for mundane, 10 for poignant)

2. Observation and importance are stored within a document by TimeWeightedVectorStoreRetriever, with a `last_accessed_time`.

When an agent responds to an observation:

1. Generates query(s) for retriever, which fetches documents based on salience, recency, and importance.

2. Summarizes the retrieved information

3. Updates the `last_accessed_time` for the used documents.

## Create a Generative Character

Now that we’ve walked through the definition, we will create two characters named “Tommie” and “Eve”.

    import math
    import faiss

    def relevance_score_fn(score: float) -> float:
        """Return a similarity score on a scale [0, 1]."""
        # This will differ depending on a few things:
        # - the distance / similarity metric used by the VectorStore
        # - the scale of your embeddings (OpenAI's are unit norm. Many others are not!)
        # This function converts the euclidean norm of normalized embeddings
        # (0 is most similar, sqrt(2) most dissimilar)
        # to a similarity function (0 to 1)
        return 1.0 - score / math.sqrt(2)

    def create_new_memory_retriever():
        """Create a new vector store retriever unique to the agent."""
        # Define your embedding model
        embeddings_model = OpenAIEmbeddings()
        # Initialize the vectorstore as empty
        embedding_size = 1536
        index = faiss.IndexFlatL2(embedding_size)
        vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}, relevance_score_fn=relevance_score_fn)
        return TimeWeightedVectorStoreRetriever(vectorstore=vectorstore, other_score_keys=["importance"], k=15)

    tommies_memory = GenerativeAgentMemory(
        llm=LLM,
        memory_retriever=create_new_memory_retriever(),
        verbose=False,
        reflection_threshold=8 # we will give this a relatively low number to show how reflection works
    )

    tommie = GenerativeAgent(name="Tommie",
                  age=25,
                  traits="anxious, likes design, talkative", # You can add more persistent traits here
                  status="looking for a job", # When connected to a virtual world, we can have the characters update their status
                  memory_retriever=create_new_memory_retriever(),
                  llm=LLM,
                  memory=tommies_memory
                 )

    # The current "Summary" of a character can't be made because the agent hasn't made
    # any observations yet.
    print(tommie.get_summary())

    Name: Tommie (age: 25)
    Innate traits: anxious, likes design, talkative
    No statements were provided about Tommie's core characteristics.

    # We can add memories directly to the memory object
    tommie_observations = [
        "Tommie remembers his dog, Bruno, from when he was a kid",
        "Tommie feels tired from driving so far",
        "Tommie sees the new home",
        "The new neighbors have a cat",
        "The road is noisy at night",
        "Tommie is hungry",
        "Tommie tries to get some rest.",
    ]
    for observation in tommie_observations:
        tommie.memory.add_memory(observation)

    # Now that Tommie has 'memories', their self-summary is more descriptive, though still rudimentary.
    # We will see how this summary updates after more observations to create a more rich description.
    print(tommie.get_summary(force_refresh=True))

    Name: Tommie (age: 25)
    Innate traits: anxious, likes design, talkative
    Tommie is a tired and hungry person who is moving into a new home. He remembers his childhood dog and is aware of the new neighbors' cat. He is trying to get some rest despite the noisy road.

## Pre-Interview with Character

Before sending our character on their way, let’s ask them a few questions.

    def interview_agent(agent: GenerativeAgent, message: str) -> str:
        """Help the notebook user interact with the agent."""
        new_message = f"{USER_NAME} says {message}"
        return agent.generate_dialogue_response(new_message)[1]

    interview_agent(tommie, "What do you like to do?")

    'Tommie said "I really enjoy design and have been working on some projects in my free time. I\'m also quite talkative and enjoy meeting new people. What about you?"'

    interview_agent(tommie, "What are you looking forward to doing today?")

    'Tommie said "Well, today I\'m mostly focused on getting settled into my new home. But once that\'s taken care of, I\'m looking forward to exploring the neighborhood and finding some new design inspiration. What about you?"'

    interview_agent(tommie, "What are you most worried about today?")

    'Tommie said "Honestly, I\'m a bit anxious about finding a job in this new area. But I\'m trying to focus on settling in first and then I\'ll start my job search. How about you?"'

## Step through the day’s observations.

    # Let's have Tommie start going through a day in the life.
    observations = [
        "Tommie wakes up to the sound of a noisy construction site outside his window.",
        "Tommie gets out of bed and heads to the kitchen to make himself some coffee.",
        "Tommie realizes he forgot to buy coffee filters and starts rummaging through his moving boxes to find some.",
        "Tommie finally finds the filters and makes himself a cup of coffee.",
        "The coffee tastes bitter, and Tommie regrets not buying a better brand.",
        "Tommie checks his email and sees that he has no job offers yet.",
        "Tommie spends some time updating his resume and cover letter.",
        "Tommie heads out to explore the city and look for job openings.",
        "Tommie sees a sign for a job fair and decides to attend.",
        "The line to get in is long, and Tommie has to wait for an hour.",
        "Tommie meets several potential employers at the job fair but doesn't receive any offers.",
        "Tommie leaves the job fair feeling disappointed.",
        "Tommie stops by a local diner to grab some lunch.",
        "The service is slow, and Tommie has to wait for 30 minutes to get his food.",
        "Tommie overhears a conversation at the next table about a job opening.",
        "Tommie asks the diners about the job opening and gets some information about the company.",
        "Tommie decides to apply for the job and sends his resume and cover letter.",
        "Tommie continues his search for job openings and drops off his resume at several local businesses.",
        "Tommie takes a break from his job search to go for a walk in a nearby park.",
        "A dog approaches and licks Tommie's feet, and he pets it for a few minutes.",
        "Tommie sees a group of people playing frisbee and decides to join in.",
        "Tommie has fun playing frisbee but gets hit in the face with the frisbee and hurts his nose.",
        "Tommie goes back to his apartment to rest for a bit.",
        "A raccoon tore open the trash bag outside his apartment, and the garbage is all over the floor.",
        "Tommie starts to feel frustrated with his job search.",
        "Tommie calls his best friend to vent about his struggles.",
        "Tommie's friend offers some words of encouragement and tells him to keep trying.",
        "Tommie feels slightly better after talking to his friend.",
    ]

    # Let's send Tommie on their way. We'll check in on their summary every few observations to watch it evolve
    for i, observation in enumerate(observations):
        _, reaction = tommie.generate_reaction(observation)
        print(colored(observation, "green"), reaction)
        if ((i+1) % 20) == 0:
            print('*'*40)
            print(colored(f"After {i+1} observations, Tommie's summary is:\n{tommie.get_summary(force_refresh=True)}", "blue"))
            print('*'*40)

    Tommie wakes up to the sound of a noisy construction site outside his window. Tommie groans and covers his head with a pillow to try and block out the noise.
    Tommie gets out of bed and heads to the kitchen to make himself some coffee. Tommie stretches his arms and yawns before making his way to the kitchen.
    Tommie realizes he forgot to buy coffee filters and starts rummaging through his moving boxes to find some. Tommie sighs in frustration but continues to search through the boxes.
    Tommie finally finds the filters and makes himself a cup of coffee. Tommie takes a sip of the coffee and smiles, feeling a bit more awake and energized.
    The coffee tastes bitter, and Tommie regrets not buying a better brand. Tommie grimaces and sets down the coffee, disappointed in the taste.
    Tommie checks his email and sees that he has no job offers yet. Tommie Tommie's shoulders slump and he sighs, feeling discouraged.
    Tommie spends some time updating his resume and cover letter. Tommie nods to himself, feeling productive and hopeful.
    Tommie heads out to explore the city and look for job openings. Tommie said "Do you have any recommendations for good places to look for job openings in the area?"
    Tommie sees a sign for a job fair and decides to attend. Tommie said "That job fair could be a great opportunity for me to network and find some job leads. Thanks for letting me know."
    The line to get in is long, and Tommie has to wait for an hour. Tommie sighs and looks around, feeling impatient and frustrated.
    Tommie meets several potential employers at the job fair but doesn't receive any offers. Tommie Tommie's shoulders slump and he sighs, feeling discouraged.
    Tommie leaves the job fair feeling disappointed. Tommie Tommie's shoulders slump and he sighs, feeling discouraged.
    Tommie stops by a local diner to grab some lunch. Tommie said "Can I get a burger and fries to go, please?"
    The service is slow, and Tommie has to wait for 30 minutes to get his food. Tommie sighs and looks at his phone, feeling impatient.
    Tommie overhears a conversation at the next table about a job opening. Tommie said "Excuse me, I couldn't help but overhear your conversation about the job opening. Do you have any more information about it?"
    Tommie asks the diners about the job opening and gets some information about the company. Tommie said "Thank you for the information, I will definitely look into that company."
    Tommie decides to apply for the job and sends his resume and cover letter. Tommie nods to himself, feeling hopeful and motivated.
    Tommie continues his search for job openings and drops off his resume at several local businesses. Tommie nods to himself, feeling proactive and hopeful.
    Tommie takes a break from his job search to go for a walk in a nearby park. Tommie takes a deep breath of fresh air and feels a sense of calm.
    A dog approaches and licks Tommie's feet, and he pets it for a few minutes. Tommie smiles and enjoys the moment of affection from the dog.
    ****************************************
    After 20 observations, Tommie's summary is:
    Name: Tommie (age: 25)
    Innate traits: anxious, likes design, talkative
    Tommie is hopeful and proactive in his job search, but easily becomes discouraged when faced with setbacks. He enjoys spending time outdoors and interacting with animals. Tommie is also productive and enjoys updating his resume and cover letter. He is talkative, enjoys meeting new people, and has an interest in design. Tommie is also a coffee drinker and seeks advice from others on finding job openings.
    ****************************************
    Tommie sees a group of people playing frisbee and decides to join in. Do nothing.
    Tommie has fun playing frisbee but gets hit in the face with the frisbee and hurts his nose. Tommie winces and touches his nose, feeling a bit of pain.
    Tommie goes back to his apartment to rest for a bit. Tommie takes a deep breath and sinks into his couch, feeling grateful for a moment of relaxation.
    A raccoon tore open the trash bag outside his apartment, and the garbage is all over the floor. Tommie sighs and grabs a broom and dustpan to clean up the mess.
    Tommie starts to feel frustrated with his job search. Tommie sighs and feels discouraged.
    Tommie calls his best friend to vent about his struggles. Tommie said "Hey, can I vent to you for a bit about my job search? I'm feeling pretty discouraged."
    Tommie's friend offers some words of encouragement and tells him to keep trying. Tommie said "Thank you for the encouragement, it means a lot to me."
    Tommie feels slightly better after talking to his friend. Tommie nods to himself, feeling grateful for the support from his friend.

## Interview after the day

    interview_agent(tommie, "Tell me about how your day has been going")

    'Tommie said "Well, it\'s been a bit of a mixed day. I\'ve had some setbacks in my job search, but I also had some fun playing frisbee and spending time outdoors. How about you?"'

    interview_agent(tommie, "How do you feel about coffee?")

    'Tommie said "I really enjoy coffee, it helps me feel more awake and energized. But sometimes I regret not buying a better brand and finding the taste bitter. How about you?"'

    interview_agent(tommie, "Tell me about your childhood dog!")

    'Tommie said "I actually didn\'t have a childhood dog, but I\'ve always loved animals. Do you have any pets?"'

## Adding Multiple Characters

Let’s add a second character to have a conversation with Tommie. Feel free to configure different traits.

    eves_memory = GenerativeAgentMemory(
        llm=LLM,
        memory_retriever=create_new_memory_retriever(),
        verbose=False,
        reflection_threshold=5
    )

    eve = GenerativeAgent(name="Eve",
                  age=34,
                  traits="curious, helpful", # You can add more persistent traits here
                  status="N/A", # When connected to a virtual world, we can have the characters update their status
                  llm=LLM,
                  daily_summaries = [
                      ("Eve started her new job as a career counselor last week and received her first assignment, a client named Tommie.")
                  ],
                  memory=eves_memory
                 )

    yesterday = (datetime.now() - timedelta(days=1)).strftime("%A %B %d")
    eve_observations = [
        "Eve overhears her colleague say something about a new client being hard to work with",
        "Eve wakes up and hear's the alarm",
        "Eve eats a boal of porridge",
        "Eve helps a coworker on a task",
        "Eve plays tennis with her friend Xu before going to work",
        "Eve overhears her colleague say something about Tommie being hard to work with",
    ]
    for observation in eve_observations:
        eve.memory.add_memory(observation)

    print(eve.get_summary())

    Name: Eve (age: 34)
    Innate traits: curious, helpful
    Eve is a helpful and active person who enjoys playing tennis, maintaining a healthy diet, and staying aware of her surroundings. She is a responsible employee who is attentive to her coworkers' comments and willing to assist them with tasks.

## Pre-conversation interviews

Let’s “Interview” Eve before she speaks with Tommie.

    interview_agent(eve, "How are you feeling about today?")

    'Eve said "I\'m feeling pretty good, thanks for asking! How about you?"'

    interview_agent(eve, "What do you know about Tommie?")

    'Eve said "I don\'t know much about Tommie, why do you ask?"'

    interview_agent(eve, "Tommie is looking to find a job. What are are some things you'd like to ask him?")

    'Eve said "That\'s interesting. I don\'t know much about Tommie, but if I had the chance, I would ask him about his previous work experience and what kind of job he\'s looking for. What about you, what would you ask him?"'

    interview_agent(eve, "You'll have to ask him. He may be a bit anxious, so I'd appreciate it if you keep the conversation going and ask as many questions as possible.")

    'Eve said "Sure, I can definitely ask him a lot of questions to keep the conversation going. Thanks for the heads up about his anxiety."'

## Dialogue between Generative Agents

Generative agents are much more complex when they interact with a virtual environment or with each other. Below, we run a simple conversation between Tommie and Eve.

    def run_conversation(agents: List[GenerativeAgent], initial_observation: str) -> None:
        """Runs a conversation between agents."""
        _, observation = agents[1].generate_reaction(initial_observation)
        print(observation)
        turns = 0
        while True:
            break_dialogue = False
            for agent in agents:
                stay_in_dialogue, observation = agent.generate_dialogue_response(observation)
                print(observation)
                # observation = f"{agent.name} said {reaction}"
                if not stay_in_dialogue:
                    break_dialogue = True
            if break_dialogue:
                break
            turns += 1

    agents = [tommie, eve]
    run_conversation(agents, "Tommie said: Hi, Eve. Thanks for agreeing to meet with me today. I have a bunch of questions and am not sure where to start. Maybe you could first share about your experience?")

    Eve said "Of course, Tommie. I'd be happy to share about my experience. What specific questions do you have?"
    Tommie said "Thank you, Eve. I'm curious about what strategies you used in your own job search. Did you have any specific tactics that helped you stand out to employers?"
    Eve said "Sure, Tommie. I found that networking and reaching out to professionals in my field was really helpful. I also made sure to tailor my resume and cover letter to each job I applied to. Do you have any specific questions about those strategies?"
    Tommie said "Thank you, Eve. That's really helpful advice. Did you have any specific ways of networking that worked well for you?"
    Eve said "Sure, Tommie. I found that attending industry events and connecting with professionals on LinkedIn were both great ways to network. Do you have any specific questions about those tactics?"
    Tommie said "That's really helpful, thank you for sharing. Did you find that you were able to make meaningful connections through LinkedIn?"
    Eve said "Yes, definitely. I was able to connect with several professionals in my field and even landed a job through a LinkedIn connection. Have you had any luck with networking on LinkedIn?"
    Tommie said "That's really impressive! I haven't had much luck yet, but I'll definitely keep trying. Thank you for the advice, Eve."
    Eve said "Glad I could help, Tommie. Is there anything else you want to know?"
    Tommie said "Thanks again, Eve. I really appreciate your advice and I'll definitely put it into practice. Have a great day!"
    Eve said "You're welcome, Tommie! Don't hesitate to reach out if you have any more questions. Have a great day too!"

## Let’s interview our agents after their conversation

Since the generative agents retain their memories from the day, we can ask them about their plans, conversations, and other memoreis.

    # We can see a current "Summary" of a character based on their own perception of self
    # has changed
    print(tommie.get_summary(force_refresh=True))

    Name: Tommie (age: 25)
    Innate traits: anxious, likes design, talkative
    Tommie is a hopeful and proactive individual who is searching for a job. He becomes discouraged when he doesn't receive any offers or positive responses, but he tries to stay productive and calm by updating his resume, going for walks, and talking to friends for support. He is also grateful for any encouragement he receives and is motivated to continue his job search. Additionally, he has a fond memory of his childhood pet and enjoys taking breaks to relax.

    print(eve.get_summary(force_refresh=True))

    Name: Eve (age: 34)
    Innate traits: curious, helpful
    Eve is a helpful and friendly coworker who enjoys playing tennis and eating breakfast. She is attentive and observant, often overhearing conversations around her. She is also proactive and willing to offer advice and assistance to colleagues, particularly in job searching and networking. She is considerate of others' feelings and strives to keep conversations going to make others feel comfortable.

    interview_agent(tommie, "How was your conversation with Eve?")

    'Tommie said "It was really helpful actually! Eve gave me some great advice on job search strategies and networking. Have you ever tried networking on LinkedIn?"'

    interview_agent(eve, "How was your conversation with Tommie?")

    'Eve said "It was great, thanks for asking! Tommie had some really insightful questions about job searching and networking, and I was happy to offer my advice. How about you, have you had a chance to speak with Tommie recently?"'

    interview_agent(eve, "What do you wish you would have said to Tommie?")

    'Eve said "Well, I think I covered most of the topics Tommie was interested in, but if I had to add one thing, it would be to make sure to follow up with any connections you make during your job search. It\'s important to maintain those relationships and keep them updated on your progress. Did you have any other questions, Person A?"'