@bytebitlabs/nest-google-genai
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

nest-google-genai

ByteBitLabs logo

Google Generative AI module for nestjs

NPM Version Package License NPM Downloads

Description

Google Generative AI module for Nest based on the official @google/generative-ai package.

Installation

$ npm i --save @bytebitlabs/nest-google-genai @google/generative-ai

Usage

Import GoogleGenAiModule:

@Module({
  imports: [GoogleGenAiModule.register({
    apiKey: 'Abc...',
  })],
  providers: [...],
})
export class AiModule {}

Inject GoogleGenAiService:

@Injectable()
export class AiService {
  constructor(private readonly googleGenAiService: GoogleGenAiService) {}
}

Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

1. Use factory

GoogleGenAiModule.registerAsync({
  useFactory: () => ({
    apiKey: 'Abc...'
  })
});

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

GoogleGenAiModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    apiKey: configService.get('GOOGLE_GEN_AI_API_KEY'),
  }),
  inject: [ConfigService],
}),

2. Use class

GoogleGenAiModule.registerAsync({
  useClass: GoogleGenAiConfigService
});

Above construction will instantiate GoogleGenAiConfigService inside GoogleGenAiModule and will leverage it to create options object.

class GoogleGenAiConfigService implements OpenaiOptionsFactory {
  createOpenaiOptions(): GoogleGenAiModuleOptions {
    return {
      apiKey: 'Abc...'
    };
  }
}

3. Use existing

GoogleGenAiModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

It works the same as useClass with one critical difference - GoogleGenAiModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

API Spec

The GoogleGenAiService wraps the GoogleGenerativeAI from the official @google/generative-ai methods. The GoogleGenAiModule.register() takes options object as an argument, read more.

Readme

Keywords

none

Package Sidebar

Install

npm i @bytebitlabs/nest-google-genai

Weekly Downloads

8

Version

1.1.0

License

MIT

Unpacked Size

11.8 kB

Total Files

20

Last publish

Collaborators

  • bytebitlabs