From e9ec192fbf5d296198bc269bc62d59ecbc38f27f Mon Sep 17 00:00:00 2001 From: pbairol Date: Tue, 13 Aug 2024 21:55:48 -0400 Subject: [PATCH] Fixed secret key --- src/server/modules/openAi/openai.entity.ts | 39 ++++++++++++++++++++++ src/server/modules/openAi/openai.router.ts | 19 +++++++++++ src/server/modules/user/user.entity.ts | 12 +++---- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/server/modules/openAi/openai.entity.ts create mode 100644 src/server/modules/openAi/openai.router.ts diff --git a/src/server/modules/openAi/openai.entity.ts b/src/server/modules/openAi/openai.entity.ts new file mode 100644 index 0000000..3ac334a --- /dev/null +++ b/src/server/modules/openAi/openai.entity.ts @@ -0,0 +1,39 @@ +import dotenv from "dotenv"; +import { OpenAI } from "openai"; +import { zodResponseFormat } from "openai/helpers/zod"; +import { z } from "zod"; + +dotenv.config(); + +const ParsedEmail = z.object({ + category: z.string(), + details: z.string(), +}); +const openai = new OpenAI({ + apiKey: "enter key", +}); + +export async function getGPTCategory(subject: string, body: string) { + const response = await openai.chat.completions.create({ + model: "gpt-4o", + messages: [ + { + role: "system", + content: + "You are an assistant taht categorizes emails into a strcutured JSON format.", + }, + { + role: "user", + content: `Please categorize the following email into a JSON format with the keys category, and details: \n\n Email Subject: ${subject}\n Email Body: ${body}`, + }, + ], + response_format: zodResponseFormat(ParsedEmail, "parsed_email"), + }); + const parsed_email = response.choices[0].message; + + if (parsed_email.refusal) { + console.log(parsed_email.refusal); + } else { + console.log(parsed_email.content); + } +} diff --git a/src/server/modules/openAi/openai.router.ts b/src/server/modules/openAi/openai.router.ts new file mode 100644 index 0000000..e64423c --- /dev/null +++ b/src/server/modules/openAi/openai.router.ts @@ -0,0 +1,19 @@ +import { z } from "zod"; +import { publicProcedure, router } from "../../trpc"; +import { getGPTCategory } from "./openai.entity"; +export default { + openai: router({ + categorizeEmail: publicProcedure + .input( + z.object({ + subject: z.string(), + body: z.string(), + }) + ) + .mutation(async ({ input }) => { + const { subject, body } = input; + const categoryInfo = await getGPTCategory(subject, body); + return categoryInfo; + }), + }), +}; diff --git a/src/server/modules/user/user.entity.ts b/src/server/modules/user/user.entity.ts index f8e0bbf..3905d17 100644 --- a/src/server/modules/user/user.entity.ts +++ b/src/server/modules/user/user.entity.ts @@ -1,11 +1,11 @@ -import { Entity, Property } from "@mikro-orm/postgresql" -import { CoreEntity } from "../../lib/helpers/coreEntity" +import { Entity, Property } from "@mikro-orm/postgresql"; +import { CoreEntity } from "../../lib/helpers/coreEntity"; @Entity() export class User extends CoreEntity { - @Property({ unique: true }) - email!: string + @Property({ unique: true }) + email!: string; - @Property() - password!: string + @Property() + password!: string; }