OpenAI
Integrating OpenAI for AI-powered features with JSON/Text parsing and credit tracking.
Optional Feature
OpenAI integration is not included by default to keep the core lean. If your project requires AI features, you can add this functionality in minutes.
This guide explains how to integrate OpenAI into your application, enabling features like:
- Generating plain Text responses.
- Receiving structured JSON responses using function calling.
- Implementing a basic Credits system to track usage.
1. Setup
Follow these steps to prepare your project for OpenAI integration.
Installation
First, install the official OpenAI Node.js library:
Environment Variable
Add your OpenAI API key to your environment variables. Create or update your .env.local
file:
Model Configuration
Define the OpenAI model you want to use within your application configuration. It's recommended to store this in a central configuration file like lib/app-config.ts
:
2. Create OpenAI Client
Create a reusable client to interact with the OpenAI API. This client handles API calls, response parsing (text or JSON), and basic credit tracking.
Create the file lib/openai/client.ts
with the following code:
3. Define Response Schemas
To receive structured JSON output from OpenAI, you need to define schemas using the JSON Schema format. OpenAI uses these schemas via its function calling feature.
Create the file lib/openai/openai-schemas.ts
to store your schemas:
4. Implement Credit System (Optional)
The client includes a basic mechanism to track API usage cost using a custom credits
value per request. This allows you to assign different costs to different types of AI operations.
- Logging: The
updateCreditUsage
function logs the token count and assigned credit cost to the server console for monitoring purposes. - Deduction (Implementation Required): The function includes a placeholder (
// TODO:
) where you must add your own logic to deduct credits from a user's account in your database (e.g., using Prisma).
Adding Credits Field to User Model
To implement the credit deduction, first add a credits
field (or similar) to your User
model in prisma/schema.prisma
:
Update Database Schema
After modifying prisma/schema.prisma
, update your database schema by running:
npx prisma db push
(for MongoDB)npx prisma migrate dev --name added_user_credits
(for PostgreSQL or other relational databases)
Next, uncomment and adapt the database update logic within the updateCreditUsage
function in lib/openai/client.ts
to connect to your database and decrement the user's credits.
5. Usage Examples
You can use the generateResponse
function from lib/openai/client.ts
directly within your Server Actions or API Routes to interact with OpenAI.
Credit System Flexibility
The credit system is optional. If you don't pass the credits
parameter to generateResponse
, it defaults to 1
. You can assign different costs for various operations, e.g., generating a title might cost 1 credit, while generating a full blog post might cost 3 credits.
Generating Plain Text Responses
To get a simple text response, call generateResponse
with just a prompt
:
Generating Structured JSON Responses
To get a structured JSON response, provide a schema
(defined in lib/openai/openai-schemas.ts
) along with the prompt
:
This provides a solid foundation for integrating OpenAI into your SaaS application.