-
Notifications
You must be signed in to change notification settings - Fork 5
/
03_sequentialchain.js
72 lines (58 loc) · 1.87 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
66
67
68
69
70
71
72
import { config } from "dotenv";
config();
import { RunnableSequence } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { ConsoleCallbackHandler } from "@langchain/core/tracers/console";
const model = new OpenAI({
temperature: 0,
// These tags will be attached to all calls made with this LLM.
tags: ["example", "callbacks", "constructor"],
// This handler will be used for all calls made with this LLM.
callbacks: [new ConsoleCallbackHandler()],
});
let reviewPromptTemplate = PromptTemplate.fromTemplate(
`You ordered {book_name} and your experience was {experience}. Write a review: `
)
const reviewChain = reviewPromptTemplate.pipe(model).pipe(new StringOutputParser());
let commentPromptTemplate = PromptTemplate.fromTemplate(
`Given the book review: {review}, write a follow-up comment: `
)
const commentChain = RunnableSequence.from([
{
review: reviewChain
},
commentPromptTemplate,
model,
new StringOutputParser(),
]);
const summaryPromptTemplate = PromptTemplate.fromTemplate(
`Summarise the review in one short sentence: \n\n {review}
Summary: `
)
const summaryChain = RunnableSequence.from([
{
review: commentChain
},
summaryPromptTemplate,
model,
new StringOutputParser(),
]);
const translationPromptTemplate = PromptTemplate.fromTemplate(
`Translate the summary to {language}: \n\n {summary}`
)
const translationChain = RunnableSequence.from([
{
summary: summaryChain,
language: (input) => input.language,
},
translationPromptTemplate,
model,
new StringOutputParser(),
]);
const result = await translationChain.invoke({
book_name: "Harry Potter And The Half-Blood Prince",
experience: "It is the best!",
language: "Filipino",});
console.log(result);