Skip to content

Initialize LangChain in Backend for AI Processing #20

@Sc-Designs

Description

@Sc-Designs

🔹 Summary

Set up LangChain in the backend to handle AI-related operations (system instructions, prompts, chains, etc.).
This includes initializing the LLM client, creating a reusable LangChain service, and preparing basic chains that can be used across the project.

👤 User Story

As a developer,
I want a centralized LangChain setup in the backend,
So that I can easily build and reuse AI features (instruction handling, chat, RAG, etc.) without rewriting LLM logic each time.

✅ Scope / Requirements

  1. Environment & Config

Add required environment variables:

OPENAI_API_KEY (or other provider key if used)

Create a config file/module to read AI-related env vars.

Make model name configurable, e.g.:

OPENAI_MODEL=gpt-4.1-mini (or similar)
  1. LangChain Initialization

Install required dependencies:

@langchain/openai

langchain

Create a dedicated file/module, e.g.:

src/lib/langchainClient.js or src/services/langchain.service.js

Inside it:

Initialize ChatOpenAI instance.

Export a reusable model instance.

Example (for dev reference, not required in issue body):

import { ChatOpenAI } from "@langchain/openai";

export const llm = new ChatOpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  model: process.env.OPENAI_MODEL || "gpt-4.1-mini",
});
  1. Basic Prompt + Chain Setup

Create a simple system + user prompt flow using ChatPromptTemplate.

Wrap model + prompt into a RunnableSequence chain.

Expose a function, e.g.:

runSimplePrompt({ system, user })

This should:

Accept parameters (input text, system instructions).

Return message.content as plain text.

  1. Integration Point

Prepare this service so it can later be used with:

System instructions from DB (Excel/Word upload feature).

Future endpoints like /api/ai/chat, /api/ai/generate, etc.

Keep the service decoupled from HTTP layer (no req, res inside it).

🔍 Expected Flow

Backend starts.

LangChain client initializes using env vars.

Other modules import llm or runSimplePrompt() from the LangChain service.

When called, the service:

Builds prompt.

Invokes model.

Returns response as text/JSON to the caller.

✅ Acceptance Criteria

@langchain/openai and langchain are installed and listed in package.json.

A LangChain service/module exists and exports at least:

Initialized LLM client.

One reusable function to run a basic AI prompt.

No hard-coded API keys or model names in code (must come from env).

Code is modular and can be used easily from controllers/routes.

Error handling is implemented for missing API key or failed AI calls.

💡 Nice-to-Have (Optional for this issue)

Logging for each AI call (prompt + timestamp).

Configurable timeout / max tokens / temperature.

Wrapper for future features like:

RAG (retriever + vector store).

System instruction injection based on DB content.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is neededquestionFurther information is requestedwocs
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions