Skip to content

Commit d598869

Browse files
authored
feat: RAG AI Example (#1354)
- Added AI integration example featuring Retrieval-Augmented Generation (RAG) along with updates to related documentation, routes, and dependency patches. The new code is in a new ai.js controller, and not in api.js, because most AI integrations are more than just an API call. Other AI related examples that are in api.js controller will be moved to ai.js in the near future. - Added new views for RAG examples (rag.pug and index.pug) and configured corresponding routes in app.js. - Removed legacy navbar code and added an "AI Examples" link in the header. - Updated package dependencies and added patches for LangChain, LangChain MongoDB, and Hugging Face integrations.
1 parent 8b23467 commit d598869

14 files changed

+3023
-404
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ GOOGLE_MAP_API_KEY=google-map-api-key
5151

5252
HERE_API_KEY=9bxxxxxxxxxxxxxxxxJFHg
5353

54+
HUGGINGFACE_KEY=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
55+
HUGGINGFACE_EMBEDDING_MODEL=BAAI/bge-small-en-v1.5
56+
HUGGINGFACE_PROVIDER=hf-inference
57+
5458
LASTFM_KEY=c8c0ea1c4a6b199b3429722512fbd17f
5559
LASTFM_SECRET=is cb7857b8fba83f819ea46ca13681fe71
5660

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ bower_components
3232
# Uploads
3333
uploads
3434

35+
# Ingestion folders
36+
rag_input
37+
3538
# Editors
3639
.idea
3740
.vscode

README.md

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ I also tried to make it as **generic** and **reusable** as possible to cover mos
8383
- Contact Form (powered by SMTP via Sendgrid, Mailgun, AWS SES, etc.)
8484
- File upload
8585
- Device camera
86+
- **AI Examples and Boilerplates**
87+
- RAG with semantic and embedding caching
88+
- Llama Instruct, Llama Vision
89+
- OpenAI Moderation
90+
- Support for a range of foundational and embedding models (DeepSeek, Llama, Mistral, Sentence Transformers, etc.) via LangChain, Together.AI, and Hugging Face
8691
- **API Examples**
8792

88-
- **AI:** OpenAI Moderation, LLAMA instruct, LLAMA vision (via Together AI serverless foundational models - Deepseek, Llama, Mistral, etc.)
8993
- **Backoffice:** Lob (USPS Mail), Paypal, Quickbooks, Stripe, Twilio (text messaging)
9094
- **Data, Media & Entertainment:** Alpha Vantage (stocks and finance info) with ChartJS, Github, Foursquare, Last.fm, New York Times, Trakt.tv (movies/TV), Twitch, Tumblr (OAuth 1.0a example), Web Scraping
9195
- **Maps and Location:** Google Maps, HERE Maps
@@ -292,6 +296,14 @@ Obtain SMTP credentials from a provider for transactional emails. Set the SMTP_U
292296

293297
<hr>
294298

299+
<img src="https://i.imgur.com/OEVF7HK.png" height="75">
300+
301+
- Go to <a href="https://huggingface.co" target="_blank">https://huggingface.co</a> and create an account.
302+
- Go to your Account Settings and create a new Access Token. Make sure you have granted the **"Make calls to Inference Provider"** permission to your token.
303+
- Add your token as `HUGGINGFACE_KEY` to your `.env` file or as an environment variable.
304+
305+
<hr>
306+
295307
<img src="https://i.imgur.com/Lw5Jb7A.png" height="50">
296308

297309
- Go to <a href="https://developer.intuit.com/app/developer/qbo/docs/get-started" target="_blank">https://developer.intuit.com/app/developer/qbo/docs/get-started</a>
@@ -328,6 +340,17 @@ Obtain SMTP credentials from a provider for transactional emails. Set the SMTP_U
328340

329341
<hr>
330342

343+
<img src="https://i.imgur.com/iCsCgp6.png" height="75">
344+
345+
The OpenAI moderation API for checking harmful inputs is free to use as long as you have paid credits in your OpenAI developer account. The cost of using their other models depends on the model, as well as the input and output size of the API call.
346+
347+
- Visit <a href="https://platform.openai.com/api-keys" target="_blank">OpenAI API Keys</a>
348+
- Sign in or create an OpenAI account.
349+
- Click on **Create new secret key** to generate an API key.
350+
- Copy and paste the generated API key into your `.env` file as `OPENAI_API_KEY` or set it as an environment variable.
351+
352+
<hr>
353+
331354
<img src="https://imgur.com/VpWnjp1.png" height="75">
332355

333356
- Visit <a href="https://developer.paypal.com" target="_blank">PayPal Developer</a>
@@ -369,6 +392,16 @@ Obtain SMTP credentials from a provider for transactional emails. Set the SMTP_U
369392

370393
<hr>
371394

395+
<img src="https://i.imgur.com/dOCkJxT.png" height="50">
396+
397+
- Visit <a href="https://www.together.ai" target="_blank">Together AI</a>
398+
- Sign in or create a Together AI account.
399+
- Click on **Create API Key** to generate a new key. You will also be able to access your API key under your account settings in the API Keys tab.
400+
- Copy and paste the generated API key into your `.env` file as `TOGETHERAI_API_KEY` or set it as an environment variable.
401+
- Go to Together AI's <a href="https://api.together.ai/models" target="_blank"> Models</a> page and pick a model based on your use case and budget and specify it as `TOGETHERAI_MODEL` in your `.env` file or as an environment variable (e.g. `togethercomputer/llama-3-70b-chat`).
402+
403+
<hr>
404+
372405
<img src="https://i.imgur.com/Adtl9qg.png" height="75">
373406

374407
- Sign up or sign in to your trakt.tv account and go to <a href="https://trakt.tv/oauth/applications" target="_blank">Trakt.tv Applications</a>.
@@ -415,27 +448,6 @@ Obtain SMTP credentials from a provider for transactional emails. Set the SMTP_U
415448

416449
<hr>
417450

418-
<img src="https://i.imgur.com/iCsCgp6.png" height="75">
419-
420-
The OpenAI moderation API for checking harmful inputs is free to use as long as you have paid credits in your OpenAI developer account. The cost of using their other models depends on the model, as well as the input and output size of the API call.
421-
422-
- Visit <a href="https://platform.openai.com/api-keys" target="_blank">OpenAI API Keys</a>
423-
- Sign in or create an OpenAI account.
424-
- Click on **Create new secret key** to generate an API key.
425-
- Copy and paste the generated API key into your `.env` file as `OPENAI_API_KEY` or set it as an environment variable.
426-
427-
<hr>
428-
429-
<img src="https://i.imgur.com/dOCkJxT.png" height="50">
430-
431-
- Visit <a href="https://www.together.ai" target="_blank">Together AI</a>
432-
- Sign in or create a Together AI account.
433-
- Click on **Create API Key** to generate a new key. You will also be able to access your API key under your account settings in the API Keys tab.
434-
- Copy and paste the generated API key into your `.env` file as `TOGETHERAI_API_KEY` or set it as an environment variable.
435-
- Go to Together AI's <a href="https://api.together.ai/models" target="_blank"> Models</a> page and pick a model based on your usecase and budget and specify it the `TOGETHERAI_MODEL` in your `.env` file or as an environment variable (e.g. `togethercomputer/llama-3-70b-chat`).
436-
437-
<hr>
438-
439451
<img src="https://i.imgur.com/QMjwCk6.png" height="50">
440452

441453
- Sign in at <a href="https://developer.x.com/" target="_blank">https://developer.x.com/</a>
@@ -481,6 +493,7 @@ The metadata for Open Graph is only set up for the home page (`home.pug`). Updat
481493
| **config**/morgan.js | Configuration for request logging with morgan. |
482494
| **config**/nodemailer.js | Configuration and helper function for sending email with nodemailer. |
483495
| **config**/passport.js | Passport Local and OAuth strategies, plus login middleware. |
496+
| **controllers**/ai.js | Controller for /ai route and all ai examples and boilerplates. |
484497
| **controllers**/api.js | Controller for /api route and all api examples. |
485498
| **controllers**/contact.js | Controller for contact form. |
486499
| **controllers**/home.js | Controller for home page (index). |
@@ -490,21 +503,22 @@ The metadata for Open Graph is only set up for the home page (`home.pug`). Updat
490503
| **public**/**js**/application.js | Specify client-side JavaScript dependencies. |
491504
| **public**/**js**/app.js | Place your client-side JavaScript here. |
492505
| **public**/**css**/main.scss | Main stylesheet for your app. |
493-
| **test**/\*.js | Unit tests |
506+
| **test**/\*.js | Unit tests. |
494507
| **views/account**/ | Templates for _login, password reset, signup, profile_. |
495-
| **views/api**/ | Templates for API Examples. |
508+
| **views/ai**/ | Templates for AI examples and boilerplates. |
509+
| **views/api**/ | Templates for API examples. |
496510
| **views/partials**/flash.pug | Error, info and success flash notifications. |
497511
| **views/partials**/header.pug | Navbar partial template. |
498512
| **views/partials**/footer.pug | Footer partial template. |
499513
| **views**/layout.pug | Base template. |
500514
| **views**/home.pug | Home page template. |
501515
| .dockerignore | Folder and files ignored by docker usage. |
502516
| .env.example | Your API keys, tokens, passwords and database URI. |
503-
| eslint.config.mjs | Rules for eslint linter. |
504517
| .gitignore | Folder and files ignored by git. |
505518
| app.js | The main application file. |
506519
| docker-compose.yml | Docker compose configuration file. |
507520
| Dockerfile | Docker configuration file. |
521+
| eslint.config.mjs | Rules for eslint linter. |
508522
| package.json | NPM dependencies. |
509523
| package-lock.json | Contains exact versions of NPM dependencies in package.json. |
510524

@@ -525,6 +539,11 @@ Required to run the project before your modifications
525539
| @fortawesome/fontawesome-free | Symbol and Icon library. |
526540
| @googleapis/drive | Google Drive API integration library. |
527541
| @googleapis/sheets | Google Sheets API integration library. |
542+
| @huggingface/inference | Client library for Hugging Face Inference providers |
543+
| @langchain/community | Third party integrations for Langchain |
544+
| @langchain/core | Base LangChain abstractions and Expression Language |
545+
| @langchain/mongodb | MongoDB integrations for LangChain |
546+
| @langchain/textsplitters | LangChain text splitters for RAG pipelines |
528547
| @lob/lob-typescript-sdk | Lob (USPS mailing / physical mailing service) library. |
529548
| @naandalist/patch-package | Fix broken node modules ahead of fixes by maintainers. |
530549
| @node-rs/bcrypt | Library for hashing and salting user passwords. |
@@ -545,10 +564,12 @@ Required to run the project before your modifications
545564
| express-rate-limit | Rate limiting middleware for abuse protection. |
546565
| express-session | Simple session middleware for Express. |
547566
| jquery | Front-end JS library to interact with HTML elements. |
567+
| langchain | Framework for developing LLM applications |
548568
| lastfm | Last.fm API library. |
549569
| lusca | CSRF middleware. |
550570
| mailchecker | Verifies that an email address is valid and not a disposable address. |
551571
| moment | Parse, validate, compute dates and times. |
572+
| mongodb | MongoDB driver |
552573
| mongoose | MongoDB ODM. |
553574
| morgan | HTTP request logger middleware for node.js. |
554575
| multer | Node.js middleware for handling `multipart/form-data`. |
@@ -563,6 +584,7 @@ Required to run the project before your modifications
563584
| passport-oauth2-refresh | A library to refresh OAuth 2.0 access tokens using refresh tokens. |
564585
| passport-openidconnect | Sign-in with OpenID Connect |
565586
| passport-steam-openid | OpenID 2.0 Steam plugin. |
587+
| pdfjs-dist | PDF parser |
566588
| pug | Template engine for Express. |
567589
| sass | Sass compiler to generate CSS with superpowers. |
568590
| sinon | Test spies, stubs and mocks for JavaScript. |
@@ -581,6 +603,7 @@ Required during code development for testing, Hygiene, code styling, etc.
581603
| @eslint/compat | Compatibility utilities for ESLin (eslint v8 support in v9). |
582604
| @eslint/eslintrc | Support for legacy ESLintRC config file format for ESLint. |
583605
| @eslint/js | ESLint JavaScript language implementation. |
606+
| @prettier/plugin-pug | Prettier plugin for formatting pug templates |
584607
| c8 | Coverage test. |
585608
| chai | BDD/TDD assertion library. |
586609
| eslint-config-airbnb-base-ex... | Replacement for eslint-config-airbnb-base pending its upgrade to eslint v9. |
@@ -704,7 +727,9 @@ mongoose.connection.on('error', (err) => {
704727
You need to have a MongoDB server running before launching `app.js`. You can download MongoDB [here](https://www.mongodb.com/try/download/community), or install it via a package manager.
705728
Windows users, read [Install MongoDB on Windows](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows//).
706729

707-
**Tip:** If you are always connected to the internet, you could just use [MongoDB Atlas](https://www.mongodb.com) instead of downloading and installing MongoDB locally. You will only need to update database credentials in `.env` file.
730+
**Tip:** If you are always connected to the internet, you could just use [MongoDB Atlas](https://www.mongodb.com) instead of downloading and installing MongoDB locally. You will only need to update the database credentials in the `.env` file.
731+
732+
**NOTE:** MongoDB Atlas (cloud database) is required for vector store, index, and search features used in AI integrations. These features are NOT available in locally installed MongoDBs.
708733

709734
### I get an error when I deploy my app, why?
710735

@@ -1373,9 +1398,9 @@ Using a local instance on your laptop with ngrok is a good solution for your dem
13731398
- Fill in your information then hit **Create your Atlas account**
13741399
- You will be redirected to Create New Cluster page.
13751400
- Select a **Cloud Provider and Region**
1376-
- Select cluster Tier to Free forever **Shared** Cluster
1401+
- Set the cluster Tier to Free Forever **Shared** Cluster
13771402
- Give Cluster a name (default: Cluster0)
1378-
- Click on green **:zap:Create Cluster button**
1403+
- Click on the green **:zap:Create Cluster button**
13791404
- Now, to access your database you need to create a DB user. To create a new MongoDB user, from the **Clusters view**, select the **Security tab**
13801405
- Under the **MongoDB Users** tab, click on **+Add New User**
13811406
- Fill in a username and password and give it either **Atlas Admin** User Privilege

app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ else numberOfProxies = 0;
6666
const homeController = require('./controllers/home');
6767
const userController = require('./controllers/user');
6868
const apiController = require('./controllers/api');
69+
const aiController = require('./controllers/ai');
6970
const contactController = require('./controllers/contact');
7071

7172
/**
@@ -231,12 +232,20 @@ app.get('/api/chart', apiController.getChart);
231232
app.get('/api/google/sheets', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getGoogleSheets);
232233
app.get('/api/quickbooks', passportConfig.isAuthenticated, passportConfig.isAuthorized, apiController.getQuickbooks);
233234
app.get('/api/trakt', apiController.getTrakt);
235+
236+
/**
237+
* AI Integrations and Boilerplate example routes.
238+
*/
239+
app.get('/ai', aiController.getAi);
234240
app.get('/api/openai-moderation', apiController.getOpenAIModeration);
235241
app.post('/api/openai-moderation', apiController.postOpenAIModeration);
236242
app.get('/api/togetherai-classifier', apiController.getTogetherAIClassifier);
237243
app.post('/api/togetherai-classifier', apiController.postTogetherAIClassifier);
238244
app.get('/api/togetherai-camera', lusca({ csrf: true }), apiController.getTogetherAICamera);
239245
app.post('/api/togetherai-camera', strictLimiter, apiController.imageUploadMiddleware, lusca({ csrf: true }), apiController.postTogetherAICamera);
246+
app.get('/ai/rag', aiController.getRag);
247+
app.post('/ai/rag/ingest', aiController.postRagIngest);
248+
app.post('/ai/rag/ask', aiController.postRagAsk);
240249

241250
/**
242251
* OAuth authentication failure handler (common for all providers)

0 commit comments

Comments
 (0)