Create your custom function to interact with the AI service.
packages/api/routes/ai.ts
const openai = new OpenAIChatApi( { apiKey: env.OPENAI_API_KEY }, { model: env.OPENAI_MODEL, maxTokens: env.MAX_TOKENS },);const response = await completion(openai, prompt, { systemMessage : "As FlashCards AI, your primary role is to transform educational material into flashcards...", schema: z.object({ error: z.string().describe("Error description").optional(), title: z .string() .describe("The name of deck, should be short") .max(60), description: z .string() .describe("Short description of the desk") .max(500), cards: z.array( z.object({ question: z .string() .max(500) .describe("The question of the card, in short sentence"), answer: z .string() .max(500) .describe("The answer of the card, in short sentence"), }), ), }),});