Node-Boot starter package for OpenAI
npm install @nodeboot/starter-openaiThe @nodeboot/starter-openai package enables seamless integration of OpenAI into NodeBoot-based applications. It provides a structured way to configure the OpenAI client using dependency injection (DI) and app-config.yaml.
> This package can be used with any AI platform compatible with OpenAI, by providing the respective platform API url and key.
To use the OpenAI starter in your project, install it via npm or yarn:
``sh`
npm install openai @nodeboot/starter-openai
or
`sh`
pnpm add openai @nodeboot/starter-openai
Ensure that your app-config.yaml file includes the necessary OpenAI integration settings:
`yaml`
integrations:
openai:
apiKey: ${OPENAI_API_KEY}
organization: "your-org-id"
project: "your-project-id"
baseURL: "https://api.openai.com/v1/"
timeout: 5000
maxRetries: 2
- apiKey: Your OpenAI API key (required).organization
- : Your OpenAI organization ID (optional).project
- : OpenAI project ID (optional).baseURL
- : Custom base URL for the API (defaults to OpenAI's endpoint).timeout
- : Request timeout in milliseconds.maxRetries
- : Maximum number of retry attempts for failed requests.
In your main application class, use the @EnableOpenAI() decorator to enable OpenAI integration:
`typescript
import "reflect-metadata";
import {Container} from "typedi";
import {NodeBoot, NodeBootApp, NodeBootApplication, NodeBootAppView} from "@nodeboot/core";
import {ExpressServer} from "@nodeboot/express-server";
import {EnableDI} from "@nodeboot/di";
import {EnableComponentScan} from "@nodeboot/scan";
import {EnableOpenAI} from "@nodeboot/starter-openai";
@EnableDI(Container)
@EnableOpenAI()
@EnableComponentScan()
@NodeBootApplication()
export class MyApplication implements NodeBootApp {
start(): Promise
return NodeBoot.run(ExpressServer);
}
}
`
Once enabled, you can inject the OpenAI client into your services to interact with OpenAI's APIs:
`typescript
import {Inject, Service} from "typedi";
import OpenAI from "openai";
@Service()
export class MyService {
constructor(@Inject(() => OpenAI) private readonly openAiClient: OpenAI) {}
async generateText(prompt: string): Promise
const response = await this.openAiClient.chat.completions.create({
model: "gpt-4",
messages: [{role: "user", content: prompt}],
max_tokens: 100,
});
return response.choices[0].message.content;
}
}
`
1. Ensure your app-config.yaml contains the correct OpenAI API key and settings.pnpm start
2. Run your NodeBoot application ().
3. Check logs to confirm the OpenAI client was successfully configured.
Solution: Ensure that app-config.yaml includes the integrations.openai section with the correct apiKey and baseURL.
Solution: Verify that the API key is correctly set and has the required permissions.
The @nodeboot/starter-openai` package simplifies OpenAI integration in NodeBoot applications. By following this guide, you can configure and use OpenAI effectively within your services.
For more details, refer to the official OpenAI API documentation.