!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Use cases](/v0.1/docs/use_cases/)
    
    *   [SQL](/v0.1/docs/use_cases/sql/)
        
    *   [Chatbots](/v0.1/docs/use_cases/chatbots/)
        
    *   [Extraction](/v0.1/docs/use_cases/extraction/)
        
    *   [Query Analysis](/v0.1/docs/use_cases/query_analysis/)
        
    *   [Q&A with RAG](/v0.1/docs/use_cases/question_answering/)
        
    *   [Tool use](/v0.1/docs/use_cases/tool_use/)
        
    *   [Interacting with APIs](/v0.1/docs/use_cases/api/)
    *   [Tabular Question Answering](/v0.1/docs/use_cases/tabular/)
    *   [Graphs](/v0.1/docs/use_cases/graph/)
        
    *   [Summarization](/v0.1/docs/use_cases/summarization/)
    *   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
        
        *   [Generative Agents](/v0.1/docs/use_cases/agent_simulations/generative_agents/)
        *   [Violation of Expectations Chain](/v0.1/docs/use_cases/agent_simulations/violation_of_expectations_chain/)
    *   [Autonomous Agents](/v0.1/docs/use_cases/autonomous_agents/)
        
    *   [Code Understanding](/v0.1/docs/use_cases/code_understanding/)
    *   [Audio/Video Structured Extraction](/v0.1/docs/use_cases/media/)

*   [](/v0.1/)
*   [Use cases](/v0.1/docs/use_cases/)
*   [Agent Simulations](/v0.1/docs/use_cases/agent_simulations/)
*   Generative Agents

Generative Agents
=================

This script implements a generative agent based on the paper [Generative Agents: Interactive Simulacra of Human Behavior](https://arxiv.org/abs/2304.03442) by Park, et. al.

In it, we leverage a time-weighted Memory object backed by a LangChain retriever. The script below creates two instances of Generative Agents, Tommie and Eve, and runs a simulation of their interaction with their observations. Tommie takes on the role of a person moving to a new town who is looking for a job, and Eve takes on the role of a career counselor.

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/openai

    yarn add @langchain/openai

    pnpm add @langchain/openai

    import { OpenAI, OpenAIEmbeddings } from "@langchain/openai";import { MemoryVectorStore } from "langchain/vectorstores/memory";import { TimeWeightedVectorStoreRetriever } from "langchain/retrievers/time_weighted";import {  GenerativeAgentMemory,  GenerativeAgent,} from "langchain/experimental/generative_agents";const Simulation = async () => {  const userName = "USER";  const llm = new OpenAI({    temperature: 0.9,    maxTokens: 1500,  });  const createNewMemoryRetriever = async () => {    // Create a new, demo in-memory vector store retriever unique to the agent.    // Better results can be achieved with a more sophisticatd vector store.    const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());    const retriever = new TimeWeightedVectorStoreRetriever({      vectorStore,      otherScoreKeys: ["importance"],      k: 15,    });    return retriever;  };  // Initializing Tommie  const tommiesMemory: GenerativeAgentMemory = new GenerativeAgentMemory(    llm,    await createNewMemoryRetriever(),    { reflectionThreshold: 8 }  );  const tommie: GenerativeAgent = new GenerativeAgent(llm, tommiesMemory, {    name: "Tommie",    age: 25,    traits: "anxious, likes design, talkative",    status: "looking for a job",  });  console.log("Tommie's first summary:\n", await tommie.getSummary());  /*    Tommie's first summary:    Name: Tommie (age: 25)    Innate traits: anxious, likes design, talkative    Tommie is an individual with no specific core characteristics described.  */  // Let's give Tommie some memories!  const tommieObservations = [    "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 (const observation of tommieObservations) {    await tommie.addMemory(observation, new Date());  }  // Checking Tommie's summary again after giving him some memories  console.log(    "Tommie's second summary:\n",    await tommie.getSummary({ forceRefresh: true })  );  /*    Tommie's second summary:    Name: Tommie (age: 25)    Innate traits: anxious, likes design, talkative    Tommie remembers his dog, is tired from driving, sees a new home with neighbors who have a cat, is aware of the noisy road at night, is hungry, and tries to get some rest.  */  const interviewAgent = async (    agent: GenerativeAgent,    message: string  ): Promise<string> => {    // Simple wrapper helping the user interact with the agent    const newMessage = `${userName} says ${message}`;    const response = await agent.generateDialogueResponse(newMessage);    return response[1];  };  // Let's have Tommie start going through a day in his life.  const 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 his way. We'll check in on his summary every few observations to watch him evolve  for (let i = 0; i < observations.length; i += 1) {    const observation = observations[i];    const [, reaction] = await tommie.generateReaction(observation);    console.log("\x1b[32m", observation, "\x1b[0m", reaction);    if ((i + 1) % 20 === 0) {      console.log("*".repeat(40));      console.log(        "\x1b[34m",        `After ${          i + 1        } observations, Tommie's summary is:\n${await tommie.getSummary({          forceRefresh: true,        })}`,        "\x1b[0m"      );      console.log("*".repeat(40));    }  }  /*    Tommie wakes up to the sound of a noisy construction site outside his window.  Tommie REACT: Tommie groans in frustration and covers his ears with his pillow.    Tommie gets out of bed and heads to the kitchen to make himself some coffee.  Tommie REACT: Tommie rubs his tired eyes before heading 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 REACT: Tommie groans and looks through his moving boxes in search of coffee filters.    Tommie finally finds the filters and makes himself a cup of coffee.  Tommie REACT: Tommie sighs in relief and prepares himself a much-needed cup of coffee.    The coffee tastes bitter, and Tommie regrets not buying a better brand.  Tommie REACT: Tommie frowns in disappointment as he takes a sip of the bitter coffee.    Tommie checks his email and sees that he has no job offers yet.  Tommie REACT: Tommie sighs in disappointment before pushing himself away from the computer with a discouraged look on his face.    Tommie spends some time updating his resume and cover letter.  Tommie REACT: Tommie takes a deep breath and stares at the computer screen as he updates his resume and cover letter.    Tommie heads out to explore the city and look for job openings.  Tommie REACT: Tommie takes a deep breath and steps out into the city, ready to find the perfect job opportunity.    Tommie sees a sign for a job fair and decides to attend.  Tommie REACT: Tommie takes a deep breath and marches towards the job fair, determination in his eyes.    The line to get in is long, and Tommie has to wait for an hour.  Tommie REACT: Tommie groans in frustration as he notices the long line.    Tommie meets several potential employers at the job fair but doesn't receive any offers.  Tommie REACT: Tommie's face falls as he listens to each potential employer's explanation as to why they can't hire him.    Tommie leaves the job fair feeling disappointed.  Tommie REACT: Tommie's face falls as he walks away from the job fair, disappointment evident in his expression.    Tommie stops by a local diner to grab some lunch.  Tommie REACT: Tommie smiles as he remembers Bruno as he walks into the diner, feeling both a sense of nostalgia and excitement.    The service is slow, and Tommie has to wait for 30 minutes to get his food.  Tommie REACT: Tommie sighs in frustration and taps his fingers on the table, growing increasingly impatient.    Tommie overhears a conversation at the next table about a job opening.  Tommie REACT: Tommie leans in closer, eager to hear the conversation.    Tommie asks the diners about the job opening and gets some information about the company.  Tommie REACT: Tommie eagerly listens to the diner's description of the company, feeling hopeful about the job opportunity.    Tommie decides to apply for the job and sends his resume and cover letter.  Tommie REACT: Tommie confidently sends in his resume and cover letter, determined to get the job.    Tommie continues his search for job openings and drops off his resume at several local businesses.  Tommie REACT: Tommie confidently drops his resume off at the various businesses, determined to find a job.    Tommie takes a break from his job search to go for a walk in a nearby park.  Tommie REACT: Tommie takes a deep breath of the fresh air and smiles in appreciation as he strolls through the park.    A dog approaches and licks Tommie's feet, and he pets it for a few minutes.  Tommie REACT: Tommie smiles in surprise as he pets the dog, feeling a sense of comfort and nostalgia.    ****************************************    After 20 observations, Tommie's summary is:    Name: Tommie (age: 25)    Innate traits: anxious, likes design, talkative    Tommie is a determined and resilient individual who remembers his dog from when he was a kid. Despite feeling tired from driving, he has the courage to explore the city, looking for job openings. He persists in updating his resume and cover letter in the pursuit of finding the perfect job opportunity, even attending job fairs when necessary, and is disappointed when he's not offered a job.    ****************************************    Tommie sees a group of people playing frisbee and decides to join in.  Tommie REACT: Tommie smiles and approaches the group, eager to take part in the game.    Tommie has fun playing frisbee but gets hit in the face with the frisbee and hurts his nose.  Tommie REACT: Tommie grimaces in pain and raises his hand to his nose, checking to see if it's bleeding.    Tommie goes back to his apartment to rest for a bit.  Tommie REACT: Tommie yawns and trudges back to his apartment, feeling exhausted from his busy day.    A raccoon tore open the trash bag outside his apartment, and the garbage is all over the floor.  Tommie REACT: Tommie shakes his head in annoyance as he surveys the mess.    Tommie starts to feel frustrated with his job search.  Tommie REACT: Tommie sighs in frustration and shakes his head, feeling discouraged from his lack of progress.    Tommie calls his best friend to vent about his struggles.  Tommie REACT: Tommie runs his hands through his hair and sighs heavily, overwhelmed by his job search.    Tommie's friend offers some words of encouragement and tells him to keep trying.  Tommie REACT: Tommie gives his friend a grateful smile, feeling comforted by the words of encouragement.    Tommie feels slightly better after talking to his friend.  Tommie REACT: Tommie gives a small smile of appreciation to his friend, feeling grateful for the words of encouragement.  */  // Interview after the day  console.log(    await interviewAgent(tommie, "Tell me about how your day has been going")  );  /*    Tommie said "My day has been pretty hectic. I've been driving around looking for job openings, attending job fairs, and updating my resume and cover letter. It's been really exhausting, but I'm determined to find the perfect job for me."  */  console.log(await interviewAgent(tommie, "How do you feel about coffee?"));  /*    Tommie said "I actually love coffee - it's one of my favorite things. I try to drink it every day, especially when I'm stressed from job searching."  */  console.log(    await interviewAgent(tommie, "Tell me about your childhood dog!")  );  /*    Tommie said "My childhood dog was named Bruno. He was an adorable black Labrador Retriever who was always full of energy. Every time I came home he'd be so excited to see me, it was like he never stopped smiling. He was always ready for adventure and he was always my shadow. I miss him every day."  */  console.log(    "Tommie's second summary:\n",    await tommie.getSummary({ forceRefresh: true })  );  /*    Tommie's second summary:    Name: Tommie (age: 25)    Innate traits: anxious, likes design, talkative    Tommie is a hardworking individual who is looking for new opportunities. Despite feeling tired, he is determined to find the perfect job. He remembers his dog from when he was a kid, is hungry, and is frustrated at times. He shows resilience when searching for his coffee filters, disappointment when checking his email and finding no job offers, and determination when attending the job fair.  */  // Let’s add a second character to have a conversation with Tommie. Feel free to configure different traits.  const evesMemory: GenerativeAgentMemory = new GenerativeAgentMemory(    llm,    await createNewMemoryRetriever(),    {      verbose: false,      reflectionThreshold: 5,    }  );  const eve: GenerativeAgent = new GenerativeAgent(llm, evesMemory, {    name: "Eve",    age: 34,    traits: "curious, helpful",    status:      "just started her new job as a career counselor last week and received her first assignment, a client named Tommie.",    // dailySummaries: [    //   "Eve started her new job as a career counselor last week and received her first assignment, a client named Tommie."    // ]  });  const eveObservations = [    "Eve overhears her colleague say something about a new client being hard to work with",    "Eve wakes up and hears 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 (const observation of eveObservations) {    await eve.addMemory(observation, new Date());  }  const eveInitialSummary: string = await eve.getSummary({    forceRefresh: true,  });  console.log("Eve's initial summary\n", eveInitialSummary);  /*    Eve's initial summary    Name: Eve (age: 34)    Innate traits: curious, helpful    Eve is an attentive listener, helpful colleague, and sociable friend who enjoys playing tennis.  */  // Let’s “Interview” Eve before she speaks with Tommie.  console.log(await interviewAgent(eve, "How are you feeling about today?"));  /*    Eve said "I'm feeling a bit anxious about meeting my new client, but I'm sure it will be fine! How about you?".  */  console.log(await interviewAgent(eve, "What do you know about Tommie?"));  /*    Eve said "I know that Tommie is a recent college graduate who's been struggling to find a job. I'm looking forward to figuring out how I can help him move forward."  */  console.log(    await interviewAgent(      eve,      "Tommie is looking to find a job. What are are some things you'd like to ask him?"    )  );  /*    Eve said: "I'd really like to get to know more about Tommie's professional background and experience, and why he is looking for a job. And I'd also like to know more about his strengths and passions and what kind of work he would be best suited for. That way I can help him find the right job to fit his needs."  */  // 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.  const runConversation = async (    agents: GenerativeAgent[],    initialObservation: string  ): Promise<void> => {    // Starts the conversation bewteen two agents    let [, observation] = await agents[1].generateReaction(initialObservation);    console.log("Initial reply:", observation);    // eslint-disable-next-line no-constant-condition    while (true) {      let breakDialogue = false;      for (const agent of agents) {        const [stayInDialogue, agentObservation] =          await agent.generateDialogueResponse(observation);        console.log("Next reply:", agentObservation);        observation = agentObservation;        if (!stayInDialogue) {          breakDialogue = true;        }      }      if (breakDialogue) {        break;      }    }  };  const agents: GenerativeAgent[] = [tommie, eve];  await runConversation(    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?"  );  /*    Initial reply: Eve said "Of course, Tommie. I'd be happy to share about my experience. What specific questions do you have?"    Next reply: 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?"    Next reply: 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?"    Next reply: Tommie said "Thank you, Eve. That's really helpful advice. Did you have any specific ways of networking that worked well for you?"    Next reply: 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?"    Next reply: Tommie said "That's really helpful, thank you for sharing. Did you find that you were able to make meaningful connections through LinkedIn?"    Next reply: 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?"    Next reply: 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."    Next reply: Eve said "Glad I could help, Tommie. Is there anything else you want to know?"    Next reply: Tommie said "Thanks again, Eve. I really appreciate your advice and I'll definitely put it into practice. Have a great day!"    Next reply: Eve said "You're welcome, Tommie! Don't hesitate to reach out if you have any more questions. Have a great day too!"  */  // Since the generative agents retain their memories from the day, we can ask them about their plans, conversations, and other memories.  const tommieSummary: string = await tommie.getSummary({    forceRefresh: true,  });  console.log("Tommie's third and final summary\n", tommieSummary);  /*    Tommie's third and final summary    Name: Tommie (age: 25)    Innate traits: anxious, likes design, talkative    Tommie is a determined individual, who demonstrates resilience in the face of disappointment. He is also a nostalgic person, remembering fondly his childhood pet, Bruno. He is resourceful, searching through his moving boxes to find what he needs, and takes initiative to attend job fairs to look for job openings.  */  const eveSummary: string = await eve.getSummary({ forceRefresh: true });  console.log("Eve's final summary\n", eveSummary);  /*    Eve's final summary    Name: Eve (age: 34)    Innate traits: curious, helpful    Eve is a helpful and encouraging colleague who actively listens to her colleagues and offers advice on how to move forward. She is willing to take time to understand her clients and their goals, and is committed to helping them succeed.  */  const interviewOne: string = await interviewAgent(    tommie,    "How was your conversation with Eve?"  );  console.log("USER: How was your conversation with Eve?\n");  console.log(interviewOne);  /*    Tommie said "It was great. She was really helpful and knowledgeable. I'm thankful that she took the time to answer all my questions."  */  const interviewTwo: string = await interviewAgent(    eve,    "How was your conversation with Tommie?"  );  console.log("USER: How was your conversation with Tommie?\n");  console.log(interviewTwo);  /*    Eve said "The conversation went very well. We discussed his goals and career aspirations, what kind of job he is looking for, and his experience and qualifications. I'm confident I can help him find the right job."  */  const interviewThree: string = await interviewAgent(    eve,    "What do you wish you would have said to Tommie?"  );  console.log("USER: What do you wish you would have said to Tommie?\n");  console.log(interviewThree);  /*    Eve said "It's ok if you don't have all the answers yet. Let's take some time to learn more about your experience and qualifications, so I can help you find a job that fits your goals."  */  return {    tommieFinalSummary: tommieSummary,    eveFinalSummary: eveSummary,    interviewOne,    interviewTwo,    interviewThree,  };};const runSimulation = async () => {  try {    await Simulation();  } catch (error) {    console.log("error running simulation:", error);    throw error;  }};await runSimulation();

#### API Reference:

*   [OpenAI](https://api.js.langchain.com/classes/langchain_openai.OpenAI.html) from `@langchain/openai`
*   [OpenAIEmbeddings](https://api.js.langchain.com/classes/langchain_openai.OpenAIEmbeddings.html) from `@langchain/openai`
*   [MemoryVectorStore](https://api.js.langchain.com/classes/langchain_vectorstores_memory.MemoryVectorStore.html) from `langchain/vectorstores/memory`
*   [TimeWeightedVectorStoreRetriever](https://api.js.langchain.com/classes/langchain_retrievers_time_weighted.TimeWeightedVectorStoreRetriever.html) from `langchain/retrievers/time_weighted`
*   [GenerativeAgentMemory](https://api.js.langchain.com/classes/langchain_experimental_generative_agents.GenerativeAgentMemory.html) from `langchain/experimental/generative_agents`
*   [GenerativeAgent](https://api.js.langchain.com/classes/langchain_experimental_generative_agents.GenerativeAgent.html) from `langchain/experimental/generative_agents`

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Agent Simulations

](/v0.1/docs/use_cases/agent_simulations/)[

Next

Violation of Expectations Chain

](/v0.1/docs/use_cases/agent_simulations/violation_of_expectations_chain/)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.