import process from "node:process";
export const questionsWithGuidelinesChain = (async () => {
const { ChatOpenAI } = await import(
"https://esm.sh/langchain@0.0.150/chat_models/openai"
);
const { LLMChain } = await import("https://esm.sh/langchain@0.0.150/chains");
const { ChatPromptTemplate } = await import(
"https://esm.sh/langchain@0.0.150/prompts"
);
const { StringOutputParser } = await import(
"https://esm.sh/langchain@0.0.150/schema/output_parser"
);
const { RunnableSequence } = await import(
"https://esm.sh/langchain@0.0.150/schema/runnable"
);
const questionPrompt = ChatPromptTemplate.fromPromptMessages([
["user", "Generate 5 questions about the following paragraph: {paragraph}"],
]);
const questionChain = questionPrompt
.pipe(new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
})
.pipe(new StringOutputParser()));
const stylePrompt = ChatPromptTemplate.fromPromptMessages([
[
"user",
"Transform the following questions to meet the guidelines:\n\nQuestions:\n\n{questions}\n\nGuidelines:{guidelines}",
],
]);
const styleChain = stylePrompt
.pipe(
new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
}),
)
.pipe(new StringOutputParser());
const questionStyleChain = RunnableSequence.from([
{
questions: questionChain,
guidelines: (input) => input.guidelines,
},
styleChain,
]);
const result = await questionStyleChain.invoke({
paragraph:
"Harrison went to harvard and worked at kensho. Kensho is in boston. Harrison likes boston. harvard is in boston.",
guidelines: "Questions are in Spanish",
});
return result;
})();