@muzikanto/nestjs-openai
v1.0.1
Published
NestJS module for openai
Downloads
26
Maintainers
Readme
@muzikanto/nestjs-openai
A lightweight NestJS module for integrating the official openai npm package into your NestJS application.
Why Use This Module?
- Seamless integration with NestJS DI system
- Supports async configuration
- Multi-client support
- Clean and minimal implementation
Installation
yarn add @muzikanto/nestjs-openai
# also
yarn add openaiPeer dependencies: @nestjs/common, @nestjs/core, openai
Quick Start
Synchronous Registration (forRoot)
// app.module.ts
import { Module } from '@nestjs/common';
import { OpenAIModule } from '@muzikanto/nestjs-openai';
@Module({
imports: [
OpenAIModule.forRoot({
name: 'test',
apiKey: process.env.OPENAI_API_KEY,
}),
],
})
export class AppModule {}Using the Client in a Service
import { Inject, Injectable } from '@nestjs/common';
import OpenAI from 'openai';
import { InjectOpenAI } from '@muzikanto/nestjs-openai';
@Injectable()
export class AiService {
constructor(
@InjectOpenAI('test') private readonly openai: OpenAI,
) {}
async generateText() {
const response = await this.openai.responses.create({
model: 'gpt-4.1-mini',
input: 'Write a short joke about NestJS',
});
return response.output_text;
}
}Async Configuration (forRootAsync)
Useful when working with ConfigModule or external configuration providers.
Using useFactory
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { OpenAIModule } from '@muzikanto/nestjs-openai';
@Module({
imports: [
ConfigModule.forRoot(),
OpenAIModule.forRootAsync({
name: 'test',
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
apiKey: config.get<string>('OPENAI_API_KEY'),
}),
}),
],
})
export class AppModule {}Using useClass
import { Injectable } from '@nestjs/common';
import { OpenAIModuleOptionsFactory } from '@muzikanto/nestjs-openai';
@Injectable()
export class OpenAIConfigService
implements OpenAIModuleOptionsFactory
{
createOpenAIModuleOptions() {
return {
apiKey: process.env.OPENAI_API_KEY,
};
}
}OpenAIModule.forRootAsync({
useClass: OpenAIConfigService,
});Multi-Instance Support
You can register multiple OpenAI clients with different configurations.
@Module({
imports: [
OpenAIModule.forRoot({
name: 'primary',
apiKey: process.env.OPENAI_API_KEY,
}),
OpenAIModule.forRoot({
name: 'secondary',
apiKey: process.env.OPENAI_API_KEY_2,
}),
],
})
export class AppModule {}@Injectable()
export class MultiAiService {
constructor(
@InjectOpenAI('primary')
private readonly primaryClient: OpenAI,
@IInjectOpenAI('secondary')
private readonly secondaryClient: OpenAI,
) {}
}Contributing
Contributions are welcome! Please open issues or submit PRs.
Changelog
See CHANGELOG for detailed version history and updates.
