Generating replies using Langchain multiple chains and Gemini in NestJS

Generating replies using Langchain multiple chains and Gemini in NestJS




Introduction

In this blog post, I demonstrated how to generate replies with multiple Langchain chains. Buyers can provide ratings and comments on sales transactions in auction sites such as eBay. When the feedback is negative, the seller must reply promptly to resolve the dispute. This demo aims to generate responses in the same language of the buyer according to the the tone (positive, neutral or negative) and topics. Previous chains obtain answers from the Gemini model and become the next chain’s output. Similarly, the model receives the new prompt to generate the final reply to keep customers happy.



Generate Gemini API Key

Go to https://aistudio.google.com/app/apikey to generate an API key for a new or an existing Google Cloud project.



Create a new NestJS Project

nest new nestjs-langchain-customer-feedback
Enter fullscreen mode

Exit fullscreen mode



Install dependencies

npm i --save-exact @nestjs/swagger @nestjs/throttler dotenv compression helmet @google/generative-ai class-validator class-transformer langchain @langchain/core @langchain/google-genai
Enter fullscreen mode

Exit fullscreen mode



Generate a Feedback Module

nest g mo advisoryFeedback
nest g co advisoryFeedback/presenters/http/advisoryFeedback --flat
nest g s advisoryFeedback/application/advisoryFeedback --flat
nest g s advisoryFeedback/application/advisoryFeedbackPromptChainingService --flat
Enter fullscreen mode

Exit fullscreen mode

Create an AdvisoryFeedbackModule module, a controller, a service for the API, and another service to build chained prompts.



Define Gemini environment variables

// .env.example

PORT=3002
GOOGLE_GEMINI_API_KEY=<google gemini api key>
GOOGLE_GEMINI_MODEL=gemini-1.5-pro-latest
Enter fullscreen mode

Exit fullscreen mode

Copy .env.example to .env, and replace GOOGLE_GEMINI_API_KEY and GOOGLE_GEMINI_MODEL with the actual API Key and the Gemini model, respectively.

  • PORT – port number of the NestJS application
  • GOOGLE_GEMINI_API_KEY – API Key of Gemini
  • GOOGLE_GEMINI_MODEL – Google model and I used Gemini 1.5 Pro in this demo

Add .env to the .gitignore file to prevent accidentally committing the Gemini API Key to the GitHub repo.



Add configuration files

The project has 3 configuration files. validate.config.ts validates the payload is valid before any request can route to the controller to execute.

// validate.config.ts

import { ValidationPipe } from '@nestjs/common';

export const validateConfig = new ValidationPipe({
  whitelist: true,
  stopAtFirstError: true,
  forbidUnknownValues: false,
});
Enter fullscreen mode

Exit fullscreen mode

env.config.ts extracts the environment variables from process.env and stores the values in the env object.

// env.config.ts

import dotenv from 'dotenv';

dotenv.config();

export const env = {
  PORT: parseInt(process.env.PORT || '3000'),
  GEMINI: {
    API_KEY: process.env.GOOGLE_GEMINI_API_KEY || '',
    MODEL_NAME: process.env.GOOGLE_GEMINI_MODEL || 'gemini-pro',
  },
};
Enter fullscreen mode

Exit fullscreen mode

throttler.config.ts defines the rate limit of the API

// throttler.config.ts

import { ThrottlerModule } from '@nestjs/throttler';

export const throttlerConfig = ThrottlerModule.forRoot([
  {
    ttl: 60000,
    limit: 10,
  },
]);
Enter fullscreen mode

Exit fullscreen mode

Each route allows ten requests in 60,000 milliseconds or 1 minute.



Bootstrap the application

// bootstrap.ts

export class Bootstrap {
  private app: NestExpressApplication;

  async initApp() {
    this.app = await NestFactory.create(AppModule);
  }

  enableCors() {
    this.app.enableCors();
  }

  setupMiddleware() {
    this.app.use(express.json({ limit: '1000kb' }));
    this.app.use(express.urlencoded({ extended: false }));
    this.app.use(compression());
    this.app.use(helmet());
  }

  setupGlobalPipe() {
    this.app.useGlobalPipes(validateConfig);
  }

  async startApp() {
    await this.app.listen(env.PORT);
  }

  setupSwagger() {
    const config = new DocumentBuilder()
      .setTitle('ESG Advisory Feedback with Langchain multiple chains and Gemini')
      .setDescription('Integrate with Langchain to improve ESG advisory feebacking by prompt chaining')
      .setVersion('1.0')
      .addTag('Langchain, Gemini 1.5 Pro Model, Multiple Chains')
      .build();
    const document = SwaggerModule.createDocument(this.app, config);
    SwaggerModule.setup('api', this.app, document);
  }
}
Enter fullscreen mode

Exit fullscreen mode

Added a Bootstrap class to set up Swagger, middleware, global validation, CORS, and finally, application start.

// main.ts

import { env } from '~configs/env.config';
import { Bootstrap } from '~core/bootstrap';

async function bootstrap() {
  const bootstrap = new Bootstrap();
  await bootstrap.initApp();
  bootstrap.enableCors();
  bootstrap.setupMiddleware();
  bootstrap.setupGlobalPipe();
  bootstrap.setupSwagger();
  await bootstrap.startApp();
}

bootstrap()
  .then(() => console.log(`The application starts successfully at port ${env.PORT}`))
  .catch((error) => console.error(error));
Enter fullscreen mode

Exit fullscreen mode

The bootstrap function enabled CORS, registered middleware to the application, set up Swagger documentation, and validated payloads using a global pipe.

I have laid down the groundwork and the next step is to add an endpoint to receive payload for generating replies with prompt chaining.



Define Feedback DTO

// feedback.dto.ts

import { IsNotEmpty, IsString } from 'class-validator';

export class FeedbackDto {
  @IsString()
  @IsNotEmpty()
  prompt: string;
}
Enter fullscreen mode

Exit fullscreen mode

FeedbackDto accepts a prompt, which is customer feedback.



Construct Gemini Model

// gemini.constant.ts

export const GEMINI_CHAT_MODEL = 'GEMINI_CHAT_MODEL';
Enter fullscreen mode

Exit fullscreen mode

// gemini-chat-model.provider.ts

export const GeminiChatModelProvider: Provider<ChatGoogleGenerativeAI> = {
  provide: GEMINI_CHAT_MODEL,
  useFactory: () =>
    new ChatGoogleGenerativeAI({
      apiKey: env.GEMINI.API_KEY,
      model: env.GEMINI.MODEL_NAME,
      safetySettings: [
        {
          category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
          threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
        },
        {
          category: HarmCategory.HARM_CATEGORY_HARASSMENT,
          threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
        },
        {
          category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
          threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
        },
        {
          category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
          threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
        },
      ],
      temperature: 0.5,
      topK: 10,
      topP: 0.5,
      maxOutputTokens: 2048,
    }),
};
Enter fullscreen mode

Exit fullscreen mode

GeminiChatModelProvider is a Gemini model that writes a short reply in the same language as the feedback.



Implement Reply Service

// customer-feedback.type.ts

export type CustomerFeedback = { 
      feedback: string;
};
Enter fullscreen mode

Exit fullscreen mode

// advisory-feedback-prompt-chaining.service.ts

// Omit the import statements 

@Injectable()
export class AdvisoryFeedbackPromptChainingService {
  private readonly logger = new Logger(AdvisoryFeedbackPromptChainingService.name);

  constructor(@Inject(GEMINI_CHAT_MODEL) private model: ChatGoogleGenerativeAI) {}

  private createFindLanguageChain() {
    const languageTemplate = `What is the language of this feedback? 
    When the feedback is written in Traditional Chinese, return Traditional Chinese. When the feedback is written in 
    Simplified Chinese, return Simplified Chinese.
    Please give me the language name, and nothing else. Delete the trailing newline character
    Feedback: {feedback}`;
    const languagePrompt = PromptTemplate.fromTemplate<CustomerFeedback>(languageTemplate);

    return languagePrompt.pipe(this.model).pipe(new StringOutputParser());
  }

  private createTopicChain() {
    const topicTemplate = `What is the topic of this feedback?
    Just the topic and explanation is not needed. Delete the trailing newline character
    Feedback: {feedback}`;
    const topicPrompt = PromptTemplate.fromTemplate<CustomerFeedback>(topicTemplate);

    return topicPrompt.pipe(this.model).pipe(new StringOutputParser());
  }

  private createSentimentChain() {
    const sentimentTemplate = `What is the sentiment of this feedback? No explaination is needed.
    When the sentiment is positive, return 'POSITIVE', is neutral, return 'NEUTRAL', is negative, return 'NEGATIVE'.
    Feedback: {feedback}`;
    const sentimentPrompt = PromptTemplate.fromTemplate<CustomerFeedback>(sentimentTemplate);

    return sentimentPrompt.pipe(this.model).pipe(new StringOutputParser());
  }

  async generateReply(feedback: string): Promise<string> {
    try {
      const chainMap = RunnableMap.from<CustomerFeedback>({
        language: this.createFindLanguageChain(),
        sentiment: this.createSentimentChain(),
        topic: this.createTopicChain(),
        feedback: ({ feedback }) => feedback,
      });

      const replyPrompt =
        PromptTemplate.fromTemplate(`The customer wrote a {sentiment} feedback about {topic} in {language}. Feedback: {feedback}
        Please give a short reply in the same language.`);

      const combinedChain = RunnableSequence.from([chainMap, replyPrompt, this.model, new StringOutputParser()]);

      const response = await combinedChain.invoke({
        feedback,
      });

      this.logger.log(response);

      return response;
    } catch (ex) {
      console.error(ex);
      throw ex;
    }
  }
}
Enter fullscreen mode

Exit fullscreen mode

AdvisoryFeedbackPromptChainingService injects three chat models in the constructor.

  • model – A chat model for a multi-turn conversation to generate a reply.
  • createFindLanguageChain – a chain to identify the language of the feedback.
  • createSentimentChain – a chain to determine the feedback’s sentiment (POSITIVE, NEUTRAL, NEGATIVE).
  • createTopicChain – a chain to determine the feedback topics.
  • generateReply – this method executed multiple chains in parallel and the outputs became the inputs of the replyPrompt. Then, the combinedChain invoked the replyPrompt to generate replies in the same language based on sentiment and topics.

The process for generating replies ended by producing the text output from generateReply. The method asked questions concurrently and wrote a descriptive prompt for the LLM to draft a reply that was polite and addressed the need of the customer.

// advisory-feedback.service.ts

// Omit the import statements to save space

@Injectable()
export class AdvisoryFeedbackService {
  constructor(private promptChainingService: AdvisoryFeedbackPromptChainingService) {}

  generateReply(prompt: string): Promise<string> {
    return this.promptChainingService.generateReply(prompt);
  }
}
Enter fullscreen mode

Exit fullscreen mode

AdvisoryFeedbackService injects AdvisoryFeedbackPromptChainingService and constructs multiple chains to ask the chat model to generate a reply.



Implement Advisory Feedback Controller

// advisory-feedback.controller.ts

// Omit the import statements to save space

@Controller('esg-advisory-feedback')
export class AdvisoryFeedbackController {
  constructor(private service: AdvisoryFeedbackService) {}

  @Post()
  generateReply(@Body() dto: FeedbackDto): Promise<string> {
    return this.service.generateReply(dto.prompt);
  }
}
Enter fullscreen mode

Exit fullscreen mode

The AdvisoryFeedbackController injects AdvisoryFeedbackService using Langchain and Gemini 1.5 Pro model. The endpoint invokes the method to generate a reply from the prompt.

  • /esg-advisory-feedback – generate a reply from a prompt



Module Registration

The AdvisoryFeedbackModule provides AdvisoryFeedbackPromptChainingService, AdvisoryFeedbackService and GeminiChatModelProvider. The module has one controller that is AdvisoryFeedbackController.

// advisory-feedback.module.ts

// Omit the import statements due to brevity reason 

@Module({
  controllers: [AdvisoryFeedbackController],
  providers: [GeminiChatModelProvider, AdvisoryFeedbackService, AdvisoryFeedbackPromptChainingService],
})
export class AdvisoryFeedbackModule {}
Enter fullscreen mode

Exit fullscreen mode

Import AdvisoryFeedbackModule into AppModule.

// app.module.ts

@Module({
  imports: [throttlerConfig, AdvisoryFeedbackModule],
  controllers: [AppController],
  providers: [
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {}
Enter fullscreen mode

Exit fullscreen mode



Test the endpoints

I can test the endpoints with cURL, Postman or Swagger documentation after launching the application.

npm run start:dev
Enter fullscreen mode

Exit fullscreen mode

The URL of the Swagger documentation is http://localhost:3002/api.

In cURL

curl --location 'http://localhost:3002/esg-advisory-feedback' 
--header 'Content-Type: application/json' 
--data '{
    "prompt": "Looking ahead, the needs of our customers will increasingly be defined by sustainable choices. ESG reporting through diginex has brought us uniformity, transparency and direction. It provides us with a framework to be able to demonstrate to all stakeholders - customers, employees, and investors - what we are doing and to be open and transparent."
}'
Enter fullscreen mode

Exit fullscreen mode



Dockerize the application

// .dockerignore

.git
.gitignore
node_modules/
dist/
Dockerfile
.dockerignore
npm-debug.log
Enter fullscreen mode

Exit fullscreen mode

Create a .dockerignore file for Docker to ignore some files and directories.

// Dockerfile

# Use an official Node.js runtime as the base image
FROM node:20-alpine

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install the dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose a port (if your application listens on a specific port)
EXPOSE 3002

# Define the command to run your application
CMD [ "npm", "run", "start:dev"]
Enter fullscreen mode

Exit fullscreen mode

I added the Dockerfile that installed the dependencies, built the NestJS application, and started it at port 3002.

// docker-compose.yaml

version: '3.8'

services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - PORT=${PORT}
      - GOOGLE_GEMINI_API_KEY=${GOOGLE_GEMINI_API_KEY}
      - GOOGLE_GEMINI_MODEL=${GOOGLE_GEMINI_MODEL}
    ports:
      - "${PORT}:${PORT}"
    networks:
      - ai
    restart: unless-stopped
networks:
  ai:
Enter fullscreen mode

Exit fullscreen mode

I added the docker-compose.yaml in the current folder, which was responsible for creating the NestJS application container.



Launch the Docker application

docker-compose up
Enter fullscreen mode

Exit fullscreen mode

Navigate to http://localhost:3002/api to read and execute the API.

This concludes my blog post about using Langchain multiple chains and Gemini 1.5 Pro model to tackle generating replies regardless the written languages. Generating replies with multiple chains reduces the efforts that a writer needs to compose a polite reply to any customer. I only scratched the surface of Langchain and Gemini because Langchain integrates with many LLMs to create chatbots, RAG, and text embeddings applications. I hope you like the content and continue to follow my learning experience in Angular, NestJS, Generative AI, and other technologies.



Resources:



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.