Skip to content

Commit

Permalink
Add profile endpoint and service for autofill in OpenAI API
Browse files Browse the repository at this point in the history
 • Implement new /openai/profile endpoint in OpenAPI spec
 • Create profile service and controller in OpenAI functions
 • Define request and response schemas for profile data handling
  • Loading branch information
jamalsoueidan committed Jul 11, 2024
1 parent 5d144ff commit fdcabd5
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,5 @@ paths:

/openai/products-title:
$ref: "./paths/openai/product-title/index.yaml"
/openai/profile:
$ref: "./paths/openai/profile/index.yaml"
16 changes: 16 additions & 0 deletions openapi/paths/openai/profile/body.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: object
properties:
professions:
type: array
items:
type: string
skills:
type: array
items:
type: string
userDetails:
type: object
required:
- professions
- skills
- userDetails
28 changes: 28 additions & 0 deletions openapi/paths/openai/profile/index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
post:
tags:
- OpenAI
operationId: openAIProfile
summary: POST autofill profile forms
description: This endpoint autofill profile forms
requestBody:
required: true
content:
application/json:
schema:
$ref: "./body.yaml"

responses:
"200":
description: Response
content:
application/json:
schema:
$ref: "./response.yaml"
"400":
$ref: "../../../responses/bad.yaml"
"401":
$ref: "../../../responses/unauthorized.yaml"
"403":
$ref: "../../../responses/forbidden.yaml"

security: []
29 changes: 29 additions & 0 deletions openapi/paths/openai/profile/response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type: object
properties:
success:
type: boolean
example: true
payload:
type: object
properties:
professions:
type: array
items:
type: string
skills:
type: array
items:
type: string
aboutMe:
type: string
shortDescription:
type: string
required:
- professions
- skills
- aboutMe
- shortDescription

required:
- success
- payload
8 changes: 8 additions & 0 deletions src/functions/openai-product.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { app } from "@azure/functions";
import "module-alias/register";
import { OpenAIControllerProductCategorize } from "./openai/controllers/product-categorize";
import { OpenAIControllerProductTitle } from "./openai/controllers/product-title";
import { OpenAIControllerProfile } from "./openai/controllers/profile";

app.http("openaiProductCategorize", {
methods: ["POST"],
Expand All @@ -16,3 +17,10 @@ app.http("openaiProductTitle", {
route: "openai/products-title",
handler: OpenAIControllerProductTitle,
});

app.http("openaiProfile", {
methods: ["POST"],
authLevel: "anonymous",
route: "openai/profile",
handler: OpenAIControllerProfile,
});
24 changes: 24 additions & 0 deletions src/functions/openai/controllers/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { z } from "zod";
import { _ } from "~/library/handler";
import { OpenAIServiceProfile } from "../services/profile";

export type OpenAIControllerProfileRequest = {
body: z.infer<typeof OpenAIControllerProfileSchema>;
};

export const OpenAIControllerProfileSchema = z.object({
professions: z.array(z.string()),
skills: z.array(z.string()),
userDetails: z.object({}),
});

export type OpenaiProductCategorizeResponse = Awaited<
ReturnType<typeof OpenAIControllerProfile>
>;

export const OpenAIControllerProfile = _(
({ body }: OpenAIControllerProfileRequest) => {
const validateBody = OpenAIControllerProfileSchema.parse(body);
return OpenAIServiceProfile(validateBody);
}
);
70 changes: 70 additions & 0 deletions src/functions/openai/services/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import OpenAI from "openai";

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});

type OpenAIServiceProfile = {
professions: string[];
skills: string[];
aboutMe: string;
shortDescription: string;
};

export const OpenAIServiceProfile = async ({
professions,
skills,
userDetails,
}: {
professions: any;
skills: any;
userDetails: any;
}) => {
try {
const content = `
You are an assistant helping to create a user profile for a beauty professional. Use the provided information about the user's profession, skills, and other details to fill out the profile in the specified JSON format. Ensure the information is concise, accurate, and professional.
### Professions to choose from:
${JSON.stringify(professions, null, 2)}
### Skills to choose from:
${JSON.stringify(skills, null, 2)}
### User Details:
${JSON.stringify(userDetails, null, 2)}
The profession and skills should be determined based on the products offered. For example:
- If the products are related to hair styling, hair coloring, or bridal hair, select the profession as "hair_stylist" and relevant skills such as "balayage_specialist", "hair_coloring", and "bridal_makeup".
Write a short description and an about me section in Danish.
Respond with this JSON structure:
{
"professions": [...],
"skills": [...],
"shortDescription": '',
"aboutMe": ''
}`;

const response = await openai.chat.completions.create({
model: "gpt-4o-2024-05-13",
messages: [
{
role: "system",
content,
},
],
max_tokens: 1000,
response_format: {
type: "json_object",
},
});

return JSON.parse(
response.choices[0].message.content as any
) as OpenAIServiceProfile;
} catch (error) {
console.error("Error:", error);
}
};

0 comments on commit fdcabd5

Please sign in to comment.