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

Fix folder2knowledge script #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 9 additions & 17 deletions scripts/folder2knowledge.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,24 @@ const findAndProcessFiles = async (dirPath) => {
withFileTypes: true,
});

const documents = [];
const chunks = [];
const contents = [];

for (const dirent of filesAndDirectories) {
const fullPath = path.join(dirPath, dirent.name);

if (dirent.isDirectory()) {
const { docs, chks } = await findAndProcessFiles(fullPath);
documents.push(...docs);
chunks.push(...chks);
const _contents = await findAndProcessFiles(fullPath);
contents.push(..._contents);
} else if (dirent.isFile()) {
const { document, chunks: fileChunks } = await processDocument(fullPath);
documents.push(document);
chunks.push(...fileChunks);
const content = await processDocument(fullPath);
contents.push(content);
}
}

return { docs: documents, chks: chunks };
return contents;
} catch (error) {
console.error(`Error processing directory ${dirPath}: ${error}`);
return { docs: [], chks: [] };
return [];
}
};

Expand Down Expand Up @@ -156,15 +153,10 @@ const main = async () => {
}

console.log(`Searching for files in: ${path}`);
const { docs, chks } = await findAndProcessFiles(path);

const output = {
documents: docs,
chunks: chks
};
const contents = await findAndProcessFiles(path);

// Save the output to knowledge.json
await fs.writeFile('knowledge.json', JSON.stringify(output, null, 2));
await fs.writeFile('knowledge.json', JSON.stringify(contents, null, 2));

console.log('Done processing files and saved memories to knowledge.json.');
} catch (error) {
Expand Down