Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed chunks bug in folder2knowledge script #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"bin": {
"tweets2character": "scripts/tweets2character.js",
"folder2knowledge": "scripts/folder2knowledge.js",
"knowledge2character": "scripts/knowledge2character.js"
},
"knowledge2character": "scripts/knowledge2character.js" },
"scripts": {
"tweets2character": "node scripts/tweets2character.js",
"folder2knowledge": "node scripts/folder2knowledge.js",
Expand All @@ -28,6 +27,7 @@
"node-fetch": "^3.3.2",
"node-llama-cpp": "^3.0.0-beta.44",
"node-stream-zip": "^1.15.0",
"pdfjs-dist": "^2.16.105",
"systeminformation": "^5.23.5",
"tiktoken": "^1.0.16"
},
Expand Down
32 changes: 31 additions & 1 deletion scripts/folder2knowledge.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ const getApiKey = async () => {
}
};

// Add this function to handle text chunking
const chunkText = (text, maxChunkSize = 1000) => {
const chunks = [];
const sentences = text.split(/(?<=[.!?])\s+/);
let currentChunk = '';

for (const sentence of sentences) {
if ((currentChunk + sentence).length > maxChunkSize && currentChunk.length > 0) {
chunks.push(currentChunk.trim());
currentChunk = '';
}
currentChunk += (currentChunk ? ' ' : '') + sentence;
}

if (currentChunk.trim()) {
chunks.push(currentChunk.trim());
}

return chunks;
};

const processDocument = async (filePath) => {
console.log(`Processing file: ${filePath}`);

Expand All @@ -89,7 +110,13 @@ const processDocument = async (filePath) => {
content = await fs.readFile(filePath, 'utf8');
}

return content;
// Create chunks from the content
const chunks = chunkText(content);

return {
document: content,
chunks: chunks
};
};

// Asynchronous function to recursively find files and process them
Expand All @@ -103,6 +130,9 @@ const findAndProcessFiles = async (dirPath) => {
const chunks = [];

for (const dirent of filesAndDirectories) {
// Skip .DS_Store files
if (dirent.name === '.DS_Store') continue;

const fullPath = path.join(dirPath, dirent.name);

if (dirent.isDirectory()) {
Expand Down