-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.building.blocks.ts
110 lines (75 loc) · 2.31 KB
/
01.building.blocks.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
} from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
//Language model
const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo-1106",
openAIApiKey: '' // your openai api key here
});
const langModel = await model.invoke([
new HumanMessage("Tell me a joke.")
]);
console.log('langModel', langModel);
//## Prompt template
const prompt = ChatPromptTemplate.fromTemplate(
`What are three good names for a company that makes {product}?`
)
const prompt1 = await prompt.format({
product: "samosas"
});
console.log('prompt1 - ', prompt1)
const promptFromMessages = ChatPromptTemplate.fromMessages([
SystemMessagePromptTemplate.fromTemplate(
"You are an expert at picking company names."
),
HumanMessagePromptTemplate.fromTemplate(
"What are three good names for a company that makes {product}?"
)
]);
const prompt2 = await promptFromMessages.formatMessages({
product: "shiny objects"
});
console.log('prompt2 - ', prompt2)
//LangChain Expression Language (LCEL)
const chain = prompt.pipe(model);
const prompt3 = await chain.invoke({
product: "colorful socks"
});
console.log('prompt3 - ', prompt3)
//Output parser
const outputParser = new StringOutputParser();
const nameGenerationChain = prompt.pipe(model).pipe(outputParser);
const response4 = await nameGenerationChain.invoke({
product: "fancy cookies"
});
console.log('response4 - ', response4)
//RunnableSequence
const nameGenerationChain1 = RunnableSequence.from([
prompt,
model,
outputParser
])
const response5 = await nameGenerationChain1.invoke({
product: "fancy cookies"
});
console.log('response5 - ', response5);
//Stream
const stream = await nameGenerationChain.stream({
product: "really cool robots",
});
for await (const chunk of stream) {
console.log(chunk);
}
//batch
const inputs = [
{ product: "large calculators" },
{ product: "alpaca wool sweaters" }
];
const batchresponse = await nameGenerationChain.batch(inputs);
console.log('batchresponse - ', batchresponse)