langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

Motorhead memory example broken

Open azcoffeehabit opened this issue 1 year ago • 1 comments

The memory example for Motorhead Memory is broken. Appears to be pre 58 but also has syntax errors. Fixing the errors in the code to bring to 58 version allows the compilation but the the example code fails to run.

$ yarn run start ./src/memory/buffer_motorhead.ts /mnt/c/workspace/llm/langchainjs/examples/src/index.ts:35 throw new Error(Could not load example ${exampleName}: ${e}); ^

Error: Could not load example ./src/memory/buffer_motorhead.ts: TypeError: fetch failed at (/mnt/c/workspace/llm/langchainjs/examples/src/index.ts:35:9)

azcoffeehabit avatar Apr 19 '23 14:04 azcoffeehabit

code example

import { ConversationChain } from "langchain/chains";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { MotorheadMemory } from "langchain/memory";
import { ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate } from "langchain/prompts";

const model = new ChatOpenAI({});
const memory = new MotorheadMemory({
  returnMessages: true,
  memoryKey: "history",
  sessionId: "user-id",
  motorheadURL: "localhost:8080",
});

await memory.init(); // loads previous state from Motörhead 🤘
const context = memory.context
  ? `
Here's previous context: ${memory.context}`
  : "";

const chatPrompt = ChatPromptTemplate.fromPromptMessages([
  SystemMessagePromptTemplate.fromTemplate(
    `The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.${context}`
  ),
  HumanMessagePromptTemplate.fromTemplate("{input}"),
]);

const chain = new ConversationChain({
  memory,
  prompt: chatPrompt,
  llm: model,
});

const res1 = await chain.call({ input: "Hi! I'm Jim." });
console.log({ res1 });

azcoffeehabit avatar Apr 19 '23 14:04 azcoffeehabit

Hey @azcoffeehabit here's a working example https://github.com/getmetal/motorhead/tree/main/examples/chat-js

Czechh avatar May 11 '23 19:05 Czechh

Hi, @azcoffeehabit! I'm helping the LangChain team manage their backlog and wanted to let you know that we are marking this issue as stale.

Based on my understanding, the Motorhead memory example you provided is currently broken, with syntax errors and a failure to run. However, Czechh has kindly provided a working example in the comments, which can be found at this link.

Before we close this issue, we wanted to check with you if it is still relevant to the latest version of the LangChain repository. If it is, please let us know by commenting on the issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution and we appreciate your understanding!

dosubot[bot] avatar Aug 19 '23 16:08 dosubot[bot]