ChatGPT Integration Guide
ChatGPT is pre-installed in this boilerplate to help you integrate AI-powered features into your projects effortlessly.
Follow these steps to get started:
Step 1: Add Your OpenAI API Key
First, set up your OpenAI API key by adding it to your environment variables:
OPENAI_API_KEY=your-openai-api-key
Step 2: Utilize askAI
in Your Code
You can now use the askAI
function to make requests to ChatGPT anywhere in your code. Below is an example of how to use askAI
to generate a workout plan in an application.
Example: Generating a Workout Plan
import { z } from "zod";
import { askAI } from "../service/openai-service";
import { protectedProcedure, router } from "../trpc";
export const createWorkoutPlanRouter = () => {
return router({
generateWorkoutPlan: protectedProcedure
.input(
z.object({
fitnessGoal: z.string().min(1, "Fitness goal is required"),
fitnessLevel: z.enum(["beginner", "intermediate", "advanced"]),
availableDays: z.number().min(1, "At least one day is required"),
preferredExerciseTypes: z.array(z.string()).optional(),
})
)
.mutation(async (opts) => {
const {
fitnessGoal,
fitnessLevel,
availableDays,
preferredExerciseTypes,
} = opts.input;
// Construct the prompt for the AI
const prompt = `
Generate a workout plan for a ${fitnessLevel} individual whose goal is ${fitnessGoal}.
They can train ${availableDays} days per week.
${
preferredExerciseTypes
? `They prefer these types of exercises: ${preferredExerciseTypes.join(
", "
)}.`
: ""
}
`;
// Call the AI service with the constructed prompt
const result = await askAI(prompt);
// Return the result
return {
result,
};
}),
});
};
In this example:
- We define a
generateWorkoutPlan
procedure usingprotectedProcedure
. - The input schema is validated with
zod
. - A prompt is constructed based on the input data.
- The
askAI
function is called with the constructed prompt. - The AI-generated workout plan is returned as the result.
This integration allows you to easily incorporate AI-generated content into your applications.