Saturday, December 20, 2025

How to integrate LLMss with backend using langchain.js

Integrating LLMs with a backend using LangChain.js involves creating a Node.js API that uses the LangChain library to communicate with an LLM provider (like OpenAI), process requests, and return responses to the client. The backend acts as a secure intermediary between your frontend application and the LLM API. [1, 2]

Core Concepts

LangChain.js simplifies the process by providing abstractions and components:
  • LLMs & Chat Models: Classes for connecting to various language models (e.g., ).
  • Prompt Templates: Reusable structures to format user input for the model.
  • Chains: Workflows that combine prompts, models, and other logic into a single sequence of calls.
  • Memory: Components that allow chains to remember past interactions for conversational context. [8, 9, 10]
Step-by-Step Integration Guide (Node.js/Express Backend) [11]

This guide assumes you have a Node.js project initialized and a frontend (e.g., React) that sends requests to your backend API. [12, 13, 14]

1. Set Up Your Backend Project
Initialize your Node.js project and install necessary packages: [15, 16, 17, 18]
mkdir llm-backend
cd llm-backend
npm init -y
npm install express dotenv langchain @langchain/openai


2. Secure Your API Key

# .env file

Store your LLM provider's API key securely in a file in the project root: [1]

# .env file
OPENAI_API_KEY="your_api_key_here"

3. Define the LLM Logic in the Backend
Create a file (e.g., ) to handle the LangChain logic. This code defines how the prompt is structured and sent to the LLM. [1, 10]

// llmService.js
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "langchain/prompts";
import { LLMChain } from "langchain/chains";
import * as dotenv from "dotenv";

dotenv.config();

// Initialize the model (using environment variable for key)
const model = new ChatOpenAI({
  temperature: 0.7, // Adjust creativity
  openAIApiKey: process.env.OPENAI_API_KEY,
});

// Define a prompt template
const promptTemplate = new PromptTemplate({
  template: "Generate a fun fact about {topic}",
  inputVariables: ["topic"],
});

// Create a chain that combines the prompt and model
export const factChain = new LLMChain({
  llm: model,
  prompt: promptTemplate,
});


4. Create a Backend API Endpoint
Create your main server file (e.g., ) using Express to receive requests from the frontend and interact with the . [1, 15]

// server.js
import express from 'express';
import cors from 'cors';
import { factChain } from './llmService.js';

const app = express();
const port = 3001;

// Enable CORS and parse JSON bodies
app.use(cors());
app.use(express.json());

// API endpoint to process user requests
app.post('/api/generate-fact', async (req, res) => {
  const { topic } = req.body;

  if (!topic) {
    return res.status(400).json({ error: 'Topic is required' });
  }

  try {
    // Call the LangChain chain with the user input
    const response = await factChain.call({ topic });
    res.json({ fact: response.text });
  } catch (error) {
    console.error("Error calling LangChain:", error);
    res.status(500).json({ error: 'Failed to generate fact' });
  }
});

app.listen(port, () => {
  console.log(`Backend server listening at http://localhost:${port}`);
});


5. Integrate with Your Frontend
From your frontend application (e.g., in a React component), you can use or to make a POST request to the backend endpoint: [19, 20, 21]

// Example Frontend JS (runs in the browser)
const getFact = async (topic) => {
  const response = await fetch('http://localhost:3001/api/generate-fact', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({ topic }),
  });

  const data = await response.json();
  console.log(data.fact);
};

// Usage:
getFact('the moon');

No comments:

Post a Comment

How to integrate standard LLMs with custom data to create RAG applications

 Integrating standard Large Language Models (LLMs) with custom data to build Retrieval-Augmented Generation (RAG) applications involves a mu...