1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
// This is an LLMChain to write a synopsis given a title of a play and the era it is set in.
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],
});
// This is an LLMChain to write a review of a play given a synopsis.
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],
});
})();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023