Step-by-step guide for integrating new language models into the Python, Node.js, and Next.js stacks.

Integration Types

OpenRouter Integration

For models accessed through OpenRouter (RAG, Simple Tool Chat, CustomGPT)

Direct SDK Integration

For direct API integration (OpenAI, Claude, Groq, Mistral)

Python Stack Integration

OpenRouter-Based Models

Service Layer: src/chatflow_langchain/service/
  • Add model service folders (custom_gpt, title, simple_chat)
Controller Logic: src/chatflow_langchain/controller/
  • Update controllers for OpenRouter registration and routing
Prompt Templates: src/prompt/langchain/
  • Define prompt templates and configuration
Callbacks: src/customlib/langchain/
  • Implement MongoDB persistence
  • Token usage tracking
  • Streaming response management

Direct SDK Integration

Dependencies: requirements.txt
  • Add required SDKs (openai, anthropic, mistral)
Model Configuration: src/chatflow_langchain/service/model/config.py
  • Add default behavior and token limits
  • Define model metadata
Optional Caching: chatopenai_cache.py
  • Add caching logic (OpenAI-specific)
  • Usage tracking and refresh/expiry logic
Controller Updates: src/chatflow_langchain/controller/
  • Add routing and provider logic
Prompt Updates: src/prompt/langchain/
  • Align templates for model behavior

Node.js Backend Configuration

Constants Configuration

AI Model Constants: src/config/constants/aimodal.js
  • Add new model constant
  • Update OPENROUTER_PROVIDER, MODAL_NAME
Common Constants: src/config/constants/common.js
  • Add model code in MODEL_CODE

API Key Management

Company Service: src/services/company.js
  • Extend checkApiKey() for new model support
  • Define API validation checker function

Next.js Frontend Integration

Assets and Constants

Model Icon: Place logo in public/ directory Constant Mapping: src/utils/constant.ts
  • Add model code, display name, image path
  • Configure credit info and sequence list

Feature Support

Helper Functions: src/utils/helper.ts
  • Update allowImageGeneration if supported
  • Update allowImageConversation if supported

Database Schema

Model Collection

Global model metadata:
{
  "title": "Model Name",
  "code": "MODEL_CODE", 
  "isActive": true
}

CompanyModel Collection

Company-specific model configuration:
{
  "name": "model-name",
  "bot": {
    "title": "Model Name",
    "code": "MODEL_CODE",
    "id": "ModelCollectionId"
  },
  "company": {
    "name": "Company Name",
    "slug": "company-slug", 
    "id": "CompanyId"
  },
  "config": {
    "apikey": "your-api-key"
  },
  "modelType": 2,
  "dimensions": 1536,
  "isActive": true,
  "extraConfig": {
    "stop": [],
    "temperature": 0.7,
    "tools": []
  }
}

Integration Checklist

ComponentPurposePath
Service LayerModel-specific servicessrc/chatflow_langchain/service/
ControllersModel routing and registrationsrc/chatflow_langchain/controller/
Prompt TemplatesModel prompt configurationsrc/prompt/langchain/
CallbacksToken tracking and persistencesrc/customlib/langchain/
DependenciesRequired SDKsrequirements.txt
Node ConstantsModel constants and codessrc/config/constants/
API Key LogicValidation and checkingsrc/services/company.js
Frontend ConstantsUI metadata and configurationsrc/utils/constant.ts
Model AssetsLogos and iconspublic/

Testing Requirements

  • End-to-end testing from model selection to response
  • Prompt behavior validation
  • Tool compatibility verification
  • API key validation testing
  • Cost tracking accuracy
Always test the complete integration flow including model selection, prompt processing, response generation, and cost tracking before deployment.