scrimba
Mistral AI
RAG - Completing the splitDocument function
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

RAG - Completing the splitDocument function
AboutCommentsNotes
RAG - Completing the splitDocument function
Expand for more info
index.js
run
preview
console
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";

// Challenge:
// Refactor this function so that it takes the path to the data/document as
// an argument, and so that it returns the splitted data as an array of string
async function splitDocument() {
const response = await fetch('handbook.txt');
const text = await response.text();
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 250,
chunkOverlap: 40
});
const output = await splitter.createDocuments([text]);
}

splitDocument();

Console
[
Document {pageContent:
"I. MISSION II. OVERVIEW The A..."
, metadata:
{loc:
{lines:
{from:
1
, to:
5
}
}
}
}
,
Document {pageContent:
"privileges and obligations of..."
, metadata:
{loc:
{lines:
{from:
6
, to:
7
}
}
}
}
,
Document {pageContent:
"intended to give rise to cont..."
, metadata:
{loc:
{lines:
{from:
8
, to:
9
}
}
}
}
,
Document {pageContent:
"exception of the voluntary at..."
, metadata:
{loc:
{lines:
{from:
10
, to:
13
}
}
}
}
,
Document {pageContent:
"Directors, which has delegate..."
, metadata:
{loc:
{lines:
{from:
14
, to:
15
}
}
}
}
,
Document {pageContent:
"specific policies. Employees..."
, metadata:
{loc:
{lines:
{from:
16
, to:
17
}
}
}
}
,
Document {pageContent:
"Questions about personnel mat..."
, metadata:
{loc:
{lines:
{from:
18
, to:
20
}
}
}
}
,
Document {pageContent:
"professional ethics and behav..."
, metadata:
{loc:
{lines:
{from:
21
, to:
22
}
}
}
}
,
Document {pageContent:
"diplomacy and courtesy in the..."
, metadata:
{loc:
{lines:
{from:
23
, to:
25
}
}
}
}
,
Document {pageContent:
"2 III. VOLUNTARY AT‐WILL EMPL..."
, metadata:
{loc:
{lines:
{from:
24
, to:
26
}
}
}
}
,
Document {pageContent:
"That means that employees may..."
, metadata:
{loc:
{lines:
{from:
27
, to:
28
}
}
}
}
,
Document {pageContent:
"Ambrics officer or employee c..."
, metadata:
{loc:
{lines:
{from:
29
, to:
32
}
}
}
}
,
Document {pageContent:
"IV. EQUAL EMPLOYMENT OPPORTUN..."
, metadata:
{loc:
{lines:
{from:
32
, to:
34
}
}
}
}
,
Document {pageContent:
"the Board of Directors and Ex..."
, metadata:
{loc:
{lines:
{from:
35
, to:
37
}
}
}
}
,
Document {pageContent:
"applicants without regard to ..."
, metadata:
{loc:
{lines:
{from:
38
, to:
39
}
}
}
}
,
Document {pageContent:
"matriculation or any other ch..."
, metadata:
{loc:
{lines:
{from:
40
, to:
41
}
}
}
}
,
Document {pageContent:
"policy regarding equal employ..."
, metadata:
{loc:
{lines:
{from:
42
, to:
43
}
}
}
}
,
Document {pageContent:
"benefits, wage and salary adm..."
, metadata:
{loc:
{lines:
{from:
44
, to:
45
}
}
}
}
,
Document {pageContent:
"agent in the full implementat..."
, metadata:
{loc:
{lines:
{from:
46
, to:
48
}
}
}
}
,
Document {pageContent:
"particular, any employee who ..."
, metadata:
{loc:
{lines:
{from:
49
, to:
51
}
}
}
}
,
Document {pageContent:
"violation to the Executive Di..."
, metadata:
{loc:
{lines:
{from:
51
, to:
53
}
}
}
}
,
Document {pageContent:
"counseling, warnings, suspens..."
, metadata:
{loc:
{lines:
{from:
54
, to:
55
}
}
}
}
,
Document {pageContent:
"violations of this policy wil..."
, metadata:
{loc:
{lines:
{from:
56
, to:
59
}
}
}
}
,
Document {pageContent:
"the investigation. 3 Ambrics ..."
, metadata:
{loc:
{lines:
{from:
58
, to:
62
}
}
}
}
,
Document {pageContent:
"and activities are conducted ..."
, metadata:
{loc:
{lines:
{from:
63
, to:
64
}
}
}
}
,
Document {pageContent:
"accommodation does not create..."
, metadata:
{loc:
{lines:
{from:
65
, to:
67
}
}
}
}
,
Document {pageContent:
"Executive Director. If y..."
, metadata:
{loc:
{lines:
{from:
67
, to:
71
}
}
}
}
,
Document {pageContent:
"employees that is free from s..."
, metadata:
{loc:
{lines:
{from:
72
, to:
74
}
}
}
}
,
Document {pageContent:
"for their co‐workers. Amb..."
, metadata:
{loc:
{lines:
{from:
74
, to:
78
}
}
}
}
,
Document {pageContent:
"policy against harassment and..."
, metadata:
{loc:
{lines:
{from:
79
, to:
80
}
}
}
}
, ...]
,
/index.html
-1:50