-
Notifications
You must be signed in to change notification settings - Fork 38
/
03_sequentialchain.js
65 lines (57 loc) · 1.66 KB
/
03_sequentialchain.js
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
57
58
59
60
61
62
63
64
65
import { config } from "dotenv";
config();
import { SequentialChain, LLMChain } from "langchain/chains";
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
const llm = new OpenAI({ temperature: 0 });
let template =
"You ordered {dish_name} and your experience was {experience}. Write a review: ";
let promptTemplate = new PromptTemplate({
template,
inputVariables: ["dish_name", "experience"],
});
const reviewChain = new LLMChain({
llm,
prompt: promptTemplate,
outputKey: "review",
});
template = "Given the restaurant review: {review}, write a follow-up comment: ";
promptTemplate = new PromptTemplate({
template,
inputVariables: ["review"],
});
const commentChain = new LLMChain({
llm,
prompt: promptTemplate,
outputKey: "comment",
});
template = "Summarise the review in one short sentence: \n\n {review}";
promptTemplate = new PromptTemplate({
template,
inputVariables: ["review"],
});
const summaryChain = new LLMChain({
llm,
prompt: promptTemplate,
outputKey: "summary",
});
template = "Translate the summary to german: \n\n {summary}";
promptTemplate = new PromptTemplate({
template,
inputVariables: ["summary"],
});
const translationChain = new LLMChain({
llm,
prompt: promptTemplate,
outputKey: "german_translation",
});
const overallChain = new SequentialChain({
chains: [reviewChain, commentChain, summaryChain, translationChain],
inputVariables: ["dish_name", "experience"],
outputVariables: ["review", "comment", "summary", "german_translation"],
});
const result = await overallChain.call({
dish_name: "Pizza Salami",
experience: "It was awful!",
});
console.log(result);