import { getLangSmithBuilder } from "https://esm.town/v/webup/getLangSmithBuilder";
import { getModelBuilder } from "https://esm.town/v/webup/getModelBuilder";
export const sampleSequentialChain = (async () => {
const { SequentialChain, LLMChain } = await import("npm:langchain/chains");
const { PromptTemplate } = await import("npm:langchain/prompts");
const mb = await getModelBuilder();
const llm = await mb();
const tb = await getLangSmithBuilder();
const tracer = await tb();
const template =
`You are a playwright. Given the title of play and the era it is set in, it is your job to write a synopsis for that title.
Title: {title}
Era: {era}
Playwright: This is a synopsis for the above play:`;
const promptTemplate = new PromptTemplate({
template,
inputVariables: ["title", "era"],
});
const synopsisChain = new LLMChain({
llm,
prompt: promptTemplate,
outputKey: "synopsis",
callbacks: [tracer],
});
const reviewTemplate =
`You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis:
{synopsis}
Review from a New York Times play critic of the above play:`;
const reviewPromptTemplate = new PromptTemplate({
template: reviewTemplate,
inputVariables: ["synopsis"],
});
const reviewChain = new LLMChain({
llm,
prompt: reviewPromptTemplate,
outputKey: "review",
callbacks: [tracer],
});
const overallChain = new SequentialChain({
chains: [synopsisChain, reviewChain],
inputVariables: ["era", "title"],
outputVariables: ["synopsis", "review"],
callbacks: [tracer],
});
return await overallChain.call({
title: "Tragedy at sunset on the beach",
era: "Victorian England",
callbacks: [tracer],
});
})();