Skip to content

Commit ed76426

Browse files
committed
Rag v5
1 parent 092b641 commit ed76426

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ console.log('Run this app using "npm start" to include sass/scss/css builds.\n')
8787
/**
8888
* Connect to MongoDB.
8989
*/
90-
mongoose.connect(process.env.MONGODB_URI, { dbName: 'hackathonstarter' });
90+
mongoose.connect(process.env.MONGODB_URI);
9191
mongoose.connection.on('error', (err) => {
9292
console.error(err);
9393
console.log('MongoDB connection error. Please make sure MongoDB is running.');
@@ -117,7 +117,7 @@ app.use(
117117
maxAge: 1209600000, // Two weeks in milliseconds
118118
secure: secureTransfer,
119119
},
120-
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI, dbName: 'hackathonstarter' }),
120+
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI }),
121121
}),
122122
);
123123
app.use(passport.initialize());

controllers/api.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,9 +1205,6 @@ exports.getGoogleSheets = (req, res) => {
12051205
);
12061206
};
12071207

1208-
/**
1209-
* Helper function to ensure vector search index exists for RAG Example
1210-
*/
12111208
/**
12121209
* Helper function to ensure vector search index exists for RAG Example
12131210
*/
@@ -1229,11 +1226,10 @@ async function ensureVectorIndex(db) {
12291226

12301227
// Ensure hash index exists
12311228
const indexes = await collection.listIndexes().toArray();
1232-
const hashIndexExists = indexes.some((index) => index.key && index.key['metadata.fileHash']);
1233-
1229+
const hashIndexExists = indexes.some((index) => index.key && index.key.fileHash === 1);
12341230
if (!hashIndexExists) {
1235-
await collection.createIndex({ 'metadata.fileHash': 1 });
1236-
console.log('Created index on metadata.fileHash');
1231+
await collection.createIndex({ fileHash: 1 });
1232+
console.log('Created index on fileHash');
12371233
}
12381234

12391235
// Check if vector search index exists
@@ -1300,9 +1296,9 @@ exports.getRag = async (req, res) => {
13001296
// List all files in MongoDB vector DB
13011297
let ingestedFiles = [];
13021298
try {
1303-
const client = new MongoClient(process.env.MONGODB_URI, { dbName: 'hackathonstarter_rag' });
1299+
const client = new MongoClient(process.env.MONGODB_URI);
13041300
await client.connect();
1305-
const db = client.db('hackathonstarter_rag');
1301+
const db = client.db();
13061302
const collection = await ensureVectorIndex(db);
13071303

13081304
ingestedFiles = await collection.distinct('source');
@@ -1357,7 +1353,7 @@ exports.postRagIngest = async (req, res) => {
13571353

13581354
try {
13591355
await client.connect();
1360-
const db = client.db('hackathonstarter_rag');
1356+
const db = client.db();
13611357
const collection = await ensureVectorIndex(db);
13621358

13631359
// Process files sequentially using reduce
@@ -1402,7 +1398,7 @@ exports.postRagIngest = async (req, res) => {
14021398

14031399
const embeddings = new HuggingFaceInferenceEmbeddings({
14041400
apiKey: process.env.HUGGINGFACE_KEY,
1405-
model: process.env.HUGGINGFACE_EMBEDING_MODEL || 'sentence-transformers/all-MiniLM-L6-v2',
1401+
model: process.env.HUGGINGFACE_EMBEDING_MODEL,
14061402
});
14071403

14081404
await MongoDBAtlasVectorSearch.fromDocuments(chunksWithMetadata, embeddings, {
@@ -1421,9 +1417,13 @@ exports.postRagIngest = async (req, res) => {
14211417
}
14221418
}, Promise.resolve());
14231419

1424-
if (processed.length > 0) {
1420+
if (processed.length > 0 && skipped.length > 0) {
1421+
req.flash('success', {
1422+
msg: `Successfully ingested ${processed.length} file(s): ${processed.join(', ')}. Skipped ${skipped.length} existing file(s): ${skipped.join(', ')}`,
1423+
});
1424+
} else if (processed.length > 0) {
14251425
req.flash('success', {
1426-
msg: `Successfully ingested ${processed.length} new file(s): ${processed.join(', ')}`,
1426+
msg: `Successfully ingested ${processed.length} file(s): ${processed.join(', ')}`,
14271427
});
14281428
} else if (skipped.length > 0) {
14291429
req.flash('info', {
@@ -1456,9 +1456,9 @@ exports.postRagAsk = async (req, res) => {
14561456
// Get list of ingested files for display
14571457
let ingestedFiles = [];
14581458
try {
1459-
const client = new MongoClient(process.env.MONGODB_URI, { dbName: 'hackathonstarter_rag' });
1459+
const client = new MongoClient(process.env.MONGODB_URI);
14601460
await client.connect();
1461-
const db = client.db('hackathonstarter_rag');
1461+
const db = client.db();
14621462
const collection = await ensureVectorIndex(db);
14631463

14641464
ingestedFiles = await collection.distinct('source');
@@ -1467,7 +1467,7 @@ exports.postRagAsk = async (req, res) => {
14671467
// Setup vector store and embeddings
14681468
const embeddings = new HuggingFaceInferenceEmbeddings({
14691469
apiKey: process.env.HUGGINGFACE_KEY,
1470-
model: process.env.HUGGINGFACE_EMBEDING_MODEL || 'BAAI/bge-large-en-v1.5',
1470+
model: process.env.HUGGINGFACE_EMBEDING_MODEL,
14711471
});
14721472
const vectorStore = new MongoDBAtlasVectorSearch(embeddings, {
14731473
collection,

views/api/rag.pug

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
extends ../layout
22

33
block content
4+
.pb-2.mt-2.mb-4.border-bottom
5+
h2
6+
i.fas.fa-robot.iconpadding
7+
| Retrieval-Augmented Generation (RAG) Demo
8+
9+
.btn-group.d-flex(role='group')
10+
a.btn.btn-primary.w-100(href='https://huggingface.co/docs/api-inference/index', target='_blank')
11+
i.fas.fa-brain.fa-sm.iconpadding
12+
| Hugging Face Inference API
13+
a.btn.btn-primary.w-100(href='https://www.mongodb.com/docs/atlas/atlas-vector-search/', target='_blank')
14+
i.fas.fa-database.fa-sm.iconpadding
15+
| MongoDB Vector Search
16+
a.btn.btn-primary.w-100(href='https://js.langchain.com/docs/modules/indexes/vector_stores/integrations/mongodb_atlas', target='_blank')
17+
i.fas.fa-link.fa-sm.iconpadding
18+
| LangChain.js MongoDB
19+
420
.pb-2.mt-2.mb-4.border-bottom
521
h2
622
i.fas.fa-robot.fa-sm

0 commit comments

Comments
 (0)