-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profile endpoint and service for autofill in OpenAI API
• 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
1 parent
5d144ff
commit fdcabd5
Showing
7 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |