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
import process from "node:process";
export const questionsWithGuidelinesChain = (async () => {
const { ChatOpenAI } = await import(
"https://esm.sh/langchain@0.0.150/chat_models/openai"
);
const { LLMChain } = await import("https://esm.sh/langchain@0.0.150/chains");
const { ChatPromptTemplate } = await import(
"https://esm.sh/langchain@0.0.150/prompts"
);
const { StringOutputParser } = await import(
"https://esm.sh/langchain@0.0.150/schema/output_parser"
);
const { RunnableSequence } = await import(
"https://esm.sh/langchain@0.0.150/schema/runnable"
);
const questionPrompt = ChatPromptTemplate.fromPromptMessages([
["user", "Generate 5 questions about the following paragraph: {paragraph}"],
]);
const questionChain = questionPrompt
.pipe(new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
})
.pipe(new StringOutputParser()));
const stylePrompt = ChatPromptTemplate.fromPromptMessages([
[
"user",
"Transform the following questions to meet the guidelines:\n\nQuestions:\n\n{questions}\n\nGuidelines:{guidelines}",
],
]);
const styleChain = stylePrompt
.pipe(
new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY,
}),
)
.pipe(new StringOutputParser());
// RunnableSequence.from() is equivalent to `.pipe().pipe()`
// but will coerce objects (and functions) into runnables
const questionStyleChain = RunnableSequence.from([
{
questions: questionChain,
// Each property in the object gets the same input,
// and we want to just pass through the "guidelines"
// property from the original input as a parameter
// to "styleChain".
guidelines: (input) => input.guidelines,
},
styleChain,
]);
const result = await questionStyleChain.invoke({
paragraph:
"Harrison went to harvard and worked at kensho. Kensho is in boston. Harrison likes boston. harvard is in boston.",
guidelines: "Questions are in Spanish",
});
return result;
})();
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.