// Import LLMChain, ChatOpenAI and prompt templates from langchain library
import { LLMChain } from "langchain";
import { ChatOpenAI } from "langchain/chat_models";
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
} from "langchain/prompts";
// Create a chat model instance using your OpenAI API key from an environment variable
const chat = new ChatOpenAI({
openAIApiKey: process.env.NEXT_PUBLIC_OPENAI_API_KEY,
temperature: 0,
});
// Create an array of mental models as objects with name and description properties
const mentalModels = [
{
name: "Pareto Principle",
description:
"The Pareto principle states that 80% of outcomes result from 20% of causes.",
},
{
name: "Occam's Razor",
description:
"Occam's razor states that the simplest explanation is usually the correct one.",
},
{
name: "Confirmation Bias",
description:
"Confirmation bias is the tendency to seek out or interpret information that confirms our existing beliefs.",
},
];
// Create a function that returns a random element from an array
const randomElement = (array) => {
return array[Math.floor(Math.random() * array.length)];
};
// Create a function that returns a prompt template for introducing a mental model
const introduceMentalModel = () => {
const model = randomElement(mentalModels); // Pick a random mental model
const message = `Let me introduce you to ${model.name}. ${model.description}.`; // Compose a message explaining the mental model
const prompt = new SystemMessagePromptTemplate(message); // Create a system message prompt template with the message
return prompt; // Return the prompt template
};
// Create a function that returns a prompt template for asking user about their scenario or problem
const askUserScenario = () => {
const message =
"Now tell me about your scenario or problem where you want to apply this mental model."; // Compose a message asking user about their scenario or problem
const prompt = new SystemMessagePromptTemplate(message); // Create a system message prompt template with the message
return prompt; // Return the prompt template
};
// Create a function that returns a human message prompt template based on user input
const getUserScenario = (userInput) => {
const input = userInput; // Get user input as string
const prompt = new HumanMessagePromptTemplate(input); // Create human message prompt template with user input as text property
return prompt; // Return the prompt template
};
// Create a function that returns an empty system message prompt template for generating response based on user input and mental model name
const generateResponse = (userInput, modelName) => {
const input = userInput; // Get user input as string
const name = modelName; // Get mental model name as string
const textProperty = `Here is how you can apply ${name} to your scenario or problem: ${input}`; // Compose text property for system message by appending user input after colon
const textPropertyWithLLM = `${textProperty}`; // Wrap text property with tags for LLM processing
const emptyTextProperty = ""; // Set empty text property for system message
const llmProperty = true; // Set llm property as true for system message
const llmOptionsProperty = {}; // Set llm options property as empty object for system message
const prompt = new SystemMessagePromptTemplate(
emptyTextProperty,
llmProperty,
llmOptionsProperty
); // Create an empty system message prompt template with llm properties
prompt.text = textPropertyWithLLM; // Assign text property with LLM tags to prompt
return prompt; // Return the prompt template
};