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.
- Express and Node for creating backend service.
- LangChain for handling LLM and RAG functionality.
- JWT for handling authentication
- GoogleGenAI langchain plugin.
- Google Gemini - API
- Docker containers for setting up environment.
- Redis database for storing AI messages.
- ZOD as body parameters validation.
- PostgreSQL database for store user data
- Prisma ORM for managing the database
- Automated OpenAPI specifications using JsDocs and Swagger UI for generating documentation.
- Role-Based Access Control (RBAC) for managing API resources
- Rotate Logs using Winston
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
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:
Before running the application, make sure you have the following installed:
- NodeJS (v20.12.1)
- PNPM (v9.7.0)
- Docker (v27.2.0)
- 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
- 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 seeMakefile
or runmake help
.
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
.
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
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] |
Qdrant Dashboard: https://localhost:6333/dashboard
PostgreSQL via Prisma: npx prisma studio
Redis: Access via vs code extension Redis for VS Code