# nestjs-openai

> NestJS OpenAI Client

Latest version **0.0.1** (published 2023-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-openai
pnpm add nestjs-openai
yarn add nestjs-openai
bun add nestjs-openai
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2023-03-06 |
| First published | 2023-03-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Chung Hwan Han |
| Maintainers | hanch |
| Keywords | nestjs, openai, nestjs-openai |

## Links

- npm: https://www.npmjs.com/package/nestjs-openai
- Repository: https://github.com/hanchchch/nestjs-openai
- Homepage: https://github.com/hanchchch/nestjs-openai#readme
- Issues: https://github.com/hanchchch/nestjs-openai
- npm.io page: https://npm.io/package/nestjs-openai

## Dependencies (3)

- [rxjs](https://npm.io/package/rxjs.md) ^7.x
- [openai](https://npm.io/package/openai.md) ^3.2.1
- [@golevelup/nestjs-modules](https://npm.io/package/@golevelup/nestjs-modules.md) ^0.6.0

## Recent versions

- 0.0.1 (latest) — 2023-03-06

## README

<h1 align="center"></h1>

<div align="center">
  <a href="http://nestjs.com/" target="_blank">
    <img src="https://nestjs.com/img/logo_text.svg" width="150" alt="Nest Logo" />
  </a>
</div>

<h3 align="center">NestJS OpenAI</h3>

---

# Features

- Configure and inject the OpenAI API client.

# Installation

```
yarn add nestjs-openai
```

# Usage

## Settings

### `OpenAIModule.register`

Import the module at your module. Configure the OpenAI API client.

```typescript
import { OpenAIModule } from "nestjs-openai";

@Module({
  imports: [
    OpenAIModule.register({
      apiKey: "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

### `OpenAIModule.registerAsync`

You can also use `OpenAIModule.registerAsync` to configure it asynchronously.
This way you can hide your api key from the code. It's recommended to use this method.

```typescript
import { OpenAIModule } from "nestjs-openai";

@Module({
  imports: [
    OpenAIModule.registerAsync({
      useFactory: (config: ConfigService<EnvVars>) => ({
        apiKey: config.get("OPENAI_API_KEY"),
      }),
      inject: [ConfigService],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

## Inject `OpenAIService`

Inject `OpenAIService` anywhere. Injected service is identical to OpenAI API client.

```typescript
@Injectable()
export class TextCompletionService {
  constructor(private readonly openai: OpenAIService) {}

  async completion(
    messages: CreateChatCompletionRequest["messages"],
  ): Promise<string> {
    const { data } = await this.openai.createCompletion({
      model: "davinci",
      prompt: "This is a test",
    });
    return data.choices[0].text;
  }
}
```

---
_Source: https://npm.io/package/nestjs-openai · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
