-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy path11.ts
executable file
·35 lines (31 loc) · 1.15 KB
/
11.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
import * as fs from "fs";
import {TextLoader} from "langchain/document_loaders/fs/text";
import {ChatOpenAI} from "langchain/chat_models/openai";
import {Document} from "langchain/document";
import {HumanMessage, SystemMessage} from "langchain/schema";
const loader = new TextLoader("11_docs/docs.md");
const [doc] = await loader.load();
const documents = doc.pageContent.split("\n\n").map((content) => {
return new Document({
pageContent: content,
})
});
console.log(documents);
const model = new ChatOpenAI({maxConcurrency: 5});
const descriptionPromise = [];
for (const doc of documents) {
descriptionPromise.push(model.invoke([
new SystemMessage(`
Describe the following document with one of the following keywords:
Mateusz, Jakub, Adam. Return the keyword and nothing else.
`),
new HumanMessage(
`Document: ${doc.pageContent}`
)
]));
}
const descriptions = await Promise.all(descriptionPromise);
descriptions.forEach((description, index) => {
documents[index].metadata.source = description.content;
});
fs.writeFileSync("11_docs/docs.json", JSON.stringify(documents, null, 2));