Skip to content

This is an Express service that provides authorization functionality and includes gen-AI features using RAG and Redis.

Notifications You must be signed in to change notification settings

WillACosta/genai-langchain-api

Repository files navigation

Gen-AI API with Langchain and NodeJS

This is an Express service written in TypeScript that provides authorization functionality and includes gen-AI features, using RAG concepts, vector database and implements AI memory history with Redis DB.

Swagger UI screenshot

Resources

Project Structure

common: common code and utilities for all modules
di: dependency injection container
modules: application features
  module_name:
    adapters: class adapters for external resources
      dataproviders:
      services:

    application: application layer for controllers and routes
      controllers:
      routes:

    core: application core logic
      entities:
      usecases:
      utils: class and constant utilities for this module

Search In Documents Flow

To perform a search in documents and ask questions about them, we have two endpoints for that:

  • POST /resources/docs
  • POST /genai/search-in-documents

On /resources/docs the documents are uploaded to local system and then broke down into small chunks to be stored on a vector database.

The /genai/search-in-documents endpoint uses RAG concept, it's responsible to create an Embedding of the user question, and retrieve the most relevant chunks from vector database using it as context for follow-up questions. Also, it uses Redis to store and retrieve chat history during user's session.

Check this process on the diagram below:

GenAI Search in Document Flow

Prerequisites

Before running the application, make sure you have the following installed:

  • NodeJS (v20.12.1)
  • PNPM (v9.7.0)
  • Docker (v27.2.0)

Running the application

  1. Clone this repository
git clone https://github.com/WillACosta/genai-langchain-api

Copy the .env.example file to .env and fill it with your own credentials. Get here: Google Gemini | LangChain

cp .env.example .env
  1. You'll need Docker to setup and running the application services

From the app's root directory, run the following command to build and running docker containers:

make run

The application will be available at http://localhost:3000.
For more commands see Makefile or run make help.

Documentation

The documentation process is automated using swagger-ui-express and swagger-jsdoc libraries, on each application route definition you will find a comment with the necessary specifications for that route in particular.

During the build process the application will handle the route comments and generate the final OpenApi specification for the Swagger UI.

After that you will be able to access: localhost:3000/docs in your browser and see the docs.

The application will be available at http://localhost:3000.

Prisma ORM

This project uses Prisma ORM (Object-Relational Mapping), at every modifications related to the database schema, a new migration is needed, create a new one running:

npx prisma migrate dev

After that generate a new prisma client instance with:

npx prisma generate

Authenticating and RBAC

This project uses jwt for authenticating users and managing sessions. It also uses Role-Based Access Control to limit some aspects of the API, such as user management.

Current supported roles are: [admin, user]:

Endpoint Admin User
GET users/ [x] [x]
PATCH users/ [x] [x]
PATCH users/change-role/:id [x]
GET users/all [x]
DELETE users/:id [x]
GET status/ [x] [x]
POST /genai/translate [x] [x]
POST /genai/search-in-document [x] [x]
GET /genai/chat-history [x] [x]
POST /resources/docs [x] [x]

Services Dashboards

Qdrant Dashboard: https://localhost:6333/dashboard PostgreSQL via Prisma: npx prisma studio Redis: Access via vs code extension Redis for VS Code