langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.52k stars 2.13k forks source link

ConstitutionalChain with memory #2507

Closed engeman2008 closed 1 year ago

engeman2008 commented 1 year ago

I have prompt

return ChatPromptTemplate.fromPromptMessages([
            SystemMessagePromptTemplate.fromTemplate(
                PROMPT
            ),
            new MessagesPlaceholder("history"),
            HumanMessagePromptTemplate.fromTemplate("{input}"),
        ]);

and chain

this.chain = new ConversationChain({
            llm: this.model,
            prompt: this.prompt,
            memory: this.memory,
        });

and constitution chain

this.constitutionalChain = ConstitutionalChain.fromLLM(this.model, {
            chain: this.chain,
            constitutionalPrinciples: this.principles,
            memory: this.memory
        });
    }

when i try to cal the constitution chain

 const chainRes = await this.constitutionalChain.call({ input }, {
            callbacks: [this.callBackHandler]
         });

or

 const chainRes = await this.constitutionalChain.run({ input }, {
            callbacks: [this.callBackHandler]
         });

I get the following error when using call or run

throw new Error(`input values have ${keys.length} keys, you must specify an input key or pass only 1 key as input`);
              ^
Error: input values have 2 keys, you must specify an input key or pass only 1 key as input

am I missing something ? where should i put the memory and why i am getting that error

dosubot[bot] commented 1 year ago

🤖

Hello,

The error you're encountering is due to the fact that the call or run method of the ConstitutionalChain class is expecting a single key-value pair in the values parameter, but it is receiving an object with more than one key-value pair.

To resolve this issue, you need to specify an inputKey when creating your memory instance. This inputKey should match the key of the input you're passing to the call or run method. Here's an example of how to do it:

const memory = new BufferMemory({
  inputKey: "input",
  chatHistory: new DynamoDBChatMessageHistory({
    tableName: "langchain",
    partitionKey: "id",
    sessionId: new Date().toISOString(), // Or some other unique identifier for the conversation
    config: {
      region: "us-east-2",
      credentials: {
        accessKeyId: "<your AWS access key id>",
        secretAccessKey: "<your AWS secret access key>",
      },
    },
  }),
});

This solution was suggested in a similar issue: Error: input values have 2 keys when using abort signal mid streaming for ConversationChain

As for the memory placement, the context provided does not include any reference to memory or its management. Therefore, it's not possible to provide a specific location for memory placement based on the provided context.

I hope this helps! If you have any other questions, feel free to ask.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

engeman2008 commented 1 year ago

Thank you, this solved my issue !