@nest-langchain/openai-compatible
v0.2.0
Published
OpenAI-compatible provider tokens and model factory wiring for Nest LangChain.
Maintainers
Readme
@nest-langchain/openai-compatible
OpenAI-compatible Chat Completions provider for NestJS dependency injection.
Use this package for providers that expose an OpenAI-compatible API, including MiniMax, Kimi/Moonshot, GLM/Z.AI, OpenRouter, Together, Fireworks, and internal gateway endpoints.
Install
pnpm add @nest-langchain/openai-compatible @langchain/openaiRegister Models
import { Module } from '@nestjs/common';
import { OpenAICompatibleProviderModule } from '@nest-langchain/openai-compatible';
@Module({
imports: [
OpenAICompatibleProviderModule.forRoot({
models: [
{
name: 'minimax',
apiKey: process.env.MINIMAX_API_KEY,
baseURL: 'https://api.minimax.io/v1',
model: 'MiniMax-M3',
temperature: 1,
modelKwargs: {
reasoning_split: true,
},
},
{
name: 'kimi',
apiKey: process.env.MOONSHOT_API_KEY,
baseURL: 'https://api.moonshot.ai/v1',
model: 'kimi-k2.7',
},
{
name: 'glm',
apiKey: process.env.ZAI_API_KEY,
baseURL: 'https://api.z.ai/api/paas/v4',
model: 'glm-5.2',
},
],
}),
],
})
export class AiModule {}Injection
import { Injectable } from '@nestjs/common';
import { ChatOpenAI } from '@langchain/openai';
import { InjectOpenAICompatibleModel } from '@nest-langchain/openai-compatible';
@Injectable()
export class ProductWorkflow {
constructor(
@InjectOpenAICompatibleModel('minimax')
private readonly model: ChatOpenAI,
) {}
async run(prompt: string) {
return this.model.invoke(prompt);
}
}InjectOpenAICompatibleModel() is a constructor-parameter helper. For dynamic
lookup, use getOpenAICompatibleModelToken(name).
Runtime Factory
Each named entry also exposes a factory. Inject it to create models with
per-call model ids / overrides while inheriting the entry's connection info
and defaults (temperature, modelKwargs, timeout, maxRetries):
import { Injectable } from '@nestjs/common';
import { ChatOpenAI } from '@langchain/openai';
import {
InjectOpenAICompatibleModelFactory,
OpenAICompatibleChatModelFactory,
} from '@nest-langchain/openai-compatible';
@Injectable()
export class ProductWorkflow {
constructor(
@InjectOpenAICompatibleModelFactory('minimax')
private readonly factory: OpenAICompatibleChatModelFactory,
) {}
run(model: string, prompt: string) {
return this.factory.create({ model, temperature: 0.5 }).invoke(prompt);
}
}For dynamic lookup use getOpenAICompatibleModelFactoryToken(name). model is
required on create().
Environment Fallbacks
For name: 'minimax', the module checks both long and short env names:
OPENAI_COMPATIBLE_MINIMAX_API_KEY
OPENAI_COMPATIBLE_MINIMAX_BASE_URL
OPENAI_COMPATIBLE_MINIMAX_MODEL
MINIMAX_API_KEY
MINIMAX_BASE_URL
MINIMAX_MODELThe unnamed default model reads:
OPENAI_COMPATIBLE_API_KEY
OPENAI_COMPATIBLE_BASE_URL
OPENAI_COMPATIBLE_MODELWhen a provider uses non-matching env names, bind them explicitly:
OpenAICompatibleProviderModule.forRoot({
name: 'kimi',
apiKeyEnv: 'MOONSHOT_API_KEY',
baseURLEnv: 'KIMI_BASE_URL',
modelEnv: 'KIMI_MODEL',
});Client Options
configurationis forwarded to the OpenAI client used by@langchain/openai.baseURLandbaseUrlboth work; the resolved value is written intoconfiguration.baseURL.defaultHeaders,timeout,maxRetries, andmodelKwargsare forwarded toChatOpenAI.
Demo
OPENAI_COMPATIBLE_API_KEY=...
OPENAI_COMPATIBLE_BASE_URL=https://api.example.com/v1
OPENAI_COMPATIBLE_MODEL=example-chat
pnpm --filter @nest-langchain/demo-providers start
curl -X POST "http://localhost:3006/providers/openai-compatible/invoke" \
-H "content-type: application/json" \
-d '{"prompt":"Write one sentence about compatible model endpoints."}'