Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

When working with memory and agents, LangChain is experiencing context loss and timeouts.

The main issue I am facing is that when using the LangChain tool to structure the output based on user input, the agent is timing out and outputting responses in the wrong format. This appears to be caused by the use of a BufferMemory by default, which causes the agent to lose the context of the question. Manually removing the timeout limit on the backend resolves this, but I would like to know how to correctly use conversation history with output parsing tools, as this seems to be a more efficient solution. The code below shows how I initialize the agent and use the LangChain tool:
const activityTextParser = StructuredOutputParser.fromNamesAndDescriptions({
  name: "name of user's activity",
  duration: "time duration in seconds of user's activity",
});
const activityFormatInstructions =
  activityTextParser.getFormatInstructions();
const activityPrompt = new PromptTemplate({
  template:
    "Structure the output based on user's input.\n{format_instructions}\n{instruction}",
  inputVariables: ['instruction'],
  partialVariables: { format_instructions: activityFormatInstructions },
});
const model = new ChatOpenAI({
  openAIApiKey: process.env.OPENAIAPIKEY,
  temperature: 0,
});
const activityTool = new DynamicTool({
  name: 'Create activity tool',
  description: 'Uses user input to create activity JSON object',
  func: async (text: string) => {
    const input = await activityPrompt.format({
      instruction: text,
    });
    return input;
  },
});
const tools = [activityTool];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
  agentType: 'chat-conversational-react-description',
  verbose: true,
});
const chatHistory = [
  new AIChatMessage(
    'Hi there! I am your productivity assistant, how can I help you today?',
  ),
];
const memory = new BufferMemory({
  chatHistory: new ChatMessageHistory(chatHistory),
  memoryKey: 'chat_history',
  returnMessages: true,
});
executor.memory = memory;
const res = await executor.call({
  input: 'Create an activity named Reading that lasts for 10 minutes',
});
console.log({ res });
return res;
I have included a link to a minimal reproducible example on GitHub, which requires Node version 18 or higher. Any advice on how to resolve this issue would be greatly appreciated. Thank you in advance for your help.
Answers(2)
avatar
Vogi
a month ago
Verified Answer
I'm sorry to hear that you are having issues with the LangChain tool. It seems like the issue is caused by the use of a BufferMemory by default, which causes the agent to lose the context of the question. You mentioned that manually removing the timeout limit on the backend resolves this, but you would like to know how to correctly use conversation history with output parsing tools. Based on your code snippet, it seems like you are using the LangChain tool to create an activity JSON object based on user input. However, I'm not sure what you mean by "correctly use conversation history with output parsing tools". Could you please provide more details on what you are trying to achieve and how you are using conversation history in your code? Also, I noticed that you have included a link to a minimal reproducible example on GitHub. Unfortunately, I cannot access external links due to safety and security reasons. However, I can still help you with your issue if you provide me with more details on what you are trying to achieve and how you are using conversation history in your code.
avatar
Tolerim
a month ago
Unfortunately, the provided code snippet and description do not provide enough information to determine the exact cause of the issue. It is possible that the use of a BufferMemory instance is affecting the behavior of the agent and causing it to lose context. However, one possible suggestion could be to try using a different memory implementation, such as a MemoryKeyContext or StateConversationContext, and see if that resolves the issue. Additionally, checking the documentation of the tool being used for output parsing and ensuring that it is compatible with the agent being used may also be helpful. Without further information, it is difficult to provide a more specific answer or solution.
;