-
Notifications
You must be signed in to change notification settings - Fork 1
/
langchain-splitter.js
60 lines (45 loc) · 1.32 KB
/
langchain-splitter.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
import {
CharacterTextSplitter,
RecursiveCharacterTextSplitter,
} from "langchain/text_splitter";
import { Document } from "langchain/document";
async function splitText(text) {
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 32,
chunkOverlap: 0,
separators: ["|", "##", ">", "-", "\n"],
});
const output = await splitter.splitDocuments([
new Document({ pageContent: text }),
]);
return output;
}
const text = `# GenAI for Developer Course
## Introduction
This is a material for GenAI for Developer Course. This course is designed to help developers to learn about AI and how to build AI applications. This course is designed for developers who have no experience in AI.
## Example prompt
- Tell me programming joke.
`;
console.log(await splitText(text));
async function splitCode(code) {
const splitter = new CharacterTextSplitter({
chunkSize: 32,
chunkOverlap: 0,
separator: " ",
});
return await splitter.splitText(code);
}
async function splitJSCode(code) {
const splitter = RecursiveCharacterTextSplitter.fromLanguage("js", {
chunkSize: 64,
chunkOverlap: 32,
});
return await splitter.splitText(code);
}
const code = `function hello(text) {
return text.toUpperCase();
}
console.log(hello("Hi LangChain!"));
`;
console.log(await splitCode(code));
console.log(await splitJSCode(code));