Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/server/modules/openAi/openai.entity.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 19 additions & 0 deletions src/server/modules/openAi/openai.router.ts
Original file line number Diff line number Diff line change
@@ -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;
}),
}),
};
12 changes: 6 additions & 6 deletions src/server/modules/user/user.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}