langchain.js
    Preparing search index...

    Implementation of a generative agent that can learn and form new memories over time. It extends the BaseChain class, which is a generic sequence of calls to components, including other chains.

    const tommie: GenerativeAgent = new GenerativeAgent(
    new OpenAI({ temperature: 0.9, maxTokens: 1500 }),
    new GenerativeAgentMemory(
    new ChatOpenAI({ model: "gpt-4o-mini" }),
    new TimeWeightedVectorStoreRetriever({
    vectorStore: new MemoryVectorStore(new OpenAIEmbeddings()),
    otherScoreKeys: ["importance"],
    k: 15,
    }),
    { reflectionThreshold: 8 },
    ),
    {
    name: "Tommie",
    age: 25,
    traits: "anxious, likes design, talkative",
    status: "looking for a job",
    },
    );

    await tommie.addMemory(
    "Tommie remembers his dog, Bruno, from when he was a kid",
    new Date(),
    );
    const summary = await tommie.getSummary({ forceRefresh: true });
    const response = await tommie.generateDialogueResponse(
    "USER says Hello Tommie, how are you today?",
    );

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    age?: number
    llm: BaseLanguageModelInterface
    longTermMemory: GenerativeAgentMemory
    memory?: any
    name: string
    status: string
    traits: string
    verbose: boolean

    Accessors

    • get inputKeys(): string[]

      Returns string[]

    • get lc_namespace(): string[]

      Returns string[]

    • get outputKeys(): string[]

      Returns string[]

    Methods

    • Run the core logic of this chain and return the output

      Parameters

      • values: ChainValues
      • OptionalrunManager: any

      Returns Promise<ChainValues>

    • Return the string type key uniquely identifying this class of chain.

      Returns string

    • Parameters

      • values: any

      Returns Promise<any>

    • Adds a memory to the agent's long-term memory.

      Parameters

      • memoryContent: string

        The content of the memory to add.

      • Optionalnow: Date

        Optional current date.

      • Optionalmetadata: Record<string, unknown>

        Optional metadata for the memory.

      • Optionalcallbacks: any

        Optional Callbacks instance.

      Returns Promise<ChainValues>

      The result of adding the memory to the agent's long-term memory.

    • Parameters

      • inputs: ChainValues[]
      • Optionalconfig: any[]

      Returns Promise<ChainValues[]>

      Use .batch() instead. Will be removed in 0.2.0.

      Call the chain on all inputs in the list

    • Parameters

      • values: any
      • Optionalconfig: any
      • Optionaltags: string[]

      Returns Promise<ChainValues>

      Use .invoke() instead. Will be removed in 0.2.0.

      Run the core logic of this chain and add to output if desired.

      Wraps _call and handles memory.

    • Creates a new LLMChain with the given prompt and the agent's language model, verbosity, output key, and memory.

      Parameters

      • prompt: PromptTemplate

        The prompt to use for the LLMChain.

      Returns LLMChain

      A new LLMChain instance.

    • Computes the agent's summary by summarizing the agent's core characteristics given the agent's relevant memories.

      Parameters

      • OptionalrunManager: any

        Optional CallbackManagerForChainRun instance.

      Returns Promise<string>

      The computed summary as a string.

    • Generates a dialogue response to the given observation.

      Parameters

      • observation: string

        The observation to generate a dialogue response for.

      • Optionalnow: Date

        Optional current date.

      Returns Promise<[boolean, string]>

      A boolean indicating whether to continue the dialogue and the output string.

    • Generates a reaction to the given observation.

      Parameters

      • observation: string

        The observation to generate a reaction for.

      • Optionalnow: Date

        Optional current date.

      Returns Promise<[boolean, string]>

      A boolean indicating whether to continue the dialogue and the output string.

    • Extracts the action of the given entity from the given observation.

      Parameters

      • observation: string

        The observation to extract the action from.

      • entityName: string

        The name of the entity to extract the action for.

      • OptionalrunManager: any

        Optional CallbackManagerForChainRun instance.

      Returns Promise<string>

      The extracted action as a string.

    • Extracts the observed entity from the given observation.

      Parameters

      • observation: string

        The observation to extract the entity from.

      • OptionalrunManager: any

        Optional CallbackManagerForChainRun instance.

      Returns Promise<string>

      The extracted entity as a string.

    • Returns a full header of the agent's status, summary, and current time.

      Parameters

      • config: { forceRefresh?: boolean; now?: Date } = {}

        Optional configuration object with current date and a boolean to force refresh.

      Returns string

      The full header as a string.

    • Gets the agent's summary, which includes the agent's name, age, traits, and a summary of the agent's core characteristics. The summary is updated periodically through probing the agent's memories.

      Parameters

      • Optionalconfig: { forceRefresh?: boolean; now?: Date }

        Optional configuration object with current date and a boolean to force refresh.

      • OptionalrunManager: any

        Optional CallbackManagerForChainRun instance.

      Returns Promise<string>

      The agent's summary as a string.

    • Invoke the chain with the provided input and returns the output.

      Parameters

      • input: ChainValues

        Input values for the chain run.

      • Optionaloptions: any

      Returns Promise<ChainValues>

      Promise that resolves with the output of the chain run.

    • Parses a newline-separated string into a list of strings.

      Parameters

      • text: string

        The string to parse.

      Returns string[]

      An array of strings parsed from the input text.

    • Parameters

      • inputs: Record<string, unknown>
      • outputs: Record<string, unknown>
      • returnOnlyOutputs: boolean = false

      Returns Promise<Record<string, unknown>>

    • Parameters

      • input: any
      • Optionalconfig: any

      Returns Promise<string>

      Use .invoke() instead. Will be removed in 0.2.0.

    • Summarizes memories that are most relevant to an observation.

      Parameters

      • observation: string

        The observation to summarize related memories for.

      • OptionalrunManager: any

        Optional CallbackManagerForChainRun instance.

      Returns Promise<string>

      The summarized memories as a string.

    • Load a chain from a json-like object describing it.

      Parameters

      Returns Promise<BaseChain<ChainValues, ChainValues>>

    • Returns string