-
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 new endpoint for correcting product titles and descriptions
- Implement new OpenAI endpoint `/openai/products-title` with associated request and response schemas - Add `OpenAIControllerProductTitle` and `OpenAIProductTitleService` for title correction functionality - Remove unused `CustomerProductServiceGet` import in `update-product.ts` - Refactor `updateProduct` function to use lean queries and simplify product update logic
- Loading branch information
1 parent
6b40825
commit a4d0e36
Showing
9 changed files
with
143 additions
and
19 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,8 @@ | ||
type: object | ||
properties: | ||
title: | ||
type: string | ||
description: | ||
type: string | ||
required: | ||
- title |
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: openAIProductTitle | ||
summary: POST Correct any grammatical errors in title and description | ||
description: This endpoint correct any grammatical errors in title and description | ||
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,19 @@ | ||
type: object | ||
properties: | ||
success: | ||
type: boolean | ||
example: true | ||
payload: | ||
type: object | ||
properties: | ||
title: | ||
type: string | ||
description: | ||
type: string | ||
required: | ||
- title | ||
- description | ||
|
||
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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import { app } from "@azure/functions"; | ||
import "module-alias/register"; | ||
import { OpenAIControllerProductCategorize } from "./openai/controllers/product"; | ||
import { OpenAIControllerProductCategorize } from "./openai/controllers/product-categorize"; | ||
import { OpenAIControllerProductTitle } from "./openai/controllers/product-title"; | ||
|
||
app.http("openaiProductCategorize", { | ||
methods: ["POST"], | ||
authLevel: "anonymous", | ||
route: "openai/products-categorize", | ||
handler: OpenAIControllerProductCategorize, | ||
}); | ||
|
||
app.http("openaiProductTitle", { | ||
methods: ["POST"], | ||
authLevel: "anonymous", | ||
route: "openai/products-title", | ||
handler: OpenAIControllerProductTitle, | ||
}); |
File renamed without changes.
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,23 @@ | ||
import { z } from "zod"; | ||
import { _ } from "~/library/handler"; | ||
import { OpenAIServiceProductTitle } from "../services/product-title"; | ||
|
||
export type OpenAIControllerProductTitleRequest = { | ||
body: z.infer<typeof OpenAIControllerProductTitleSchema>; | ||
}; | ||
|
||
export const OpenAIControllerProductTitleSchema = z.object({ | ||
title: z.string(), | ||
description: z.string().optional(), | ||
}); | ||
|
||
export type OpenaiProductCategorizeResponse = Awaited< | ||
ReturnType<typeof OpenAIControllerProductTitle> | ||
>; | ||
|
||
export const OpenAIControllerProductTitle = _( | ||
({ body }: OpenAIControllerProductTitleRequest) => { | ||
const validateBody = OpenAIControllerProductTitleSchema.parse(body); | ||
return OpenAIServiceProductTitle(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,40 @@ | ||
import OpenAI from "openai"; | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
|
||
type OpenAIServiceProductTitleReturn = { | ||
title: string; | ||
description: string; | ||
}; | ||
|
||
export const OpenAIServiceProductTitle = async ({ | ||
title, | ||
description, | ||
}: { | ||
title: string; | ||
description?: string; | ||
}) => { | ||
const response = await openai.chat.completions.create({ | ||
model: "gpt-4o-2024-05-13", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: `You are an expert in correcting treatment titles and descriptions. Please correct any grammatical errors in title and description. If the description is missing, add a short sentence that encourages customers to buy the product. Please responds with title, description json format and keep the language in danish.`, | ||
}, | ||
{ | ||
role: "user", | ||
content: `Title: ${title}\nDescription: ${description || ""}`, | ||
}, | ||
], | ||
max_tokens: 100, | ||
response_format: { | ||
type: "json_object", | ||
}, | ||
}); | ||
|
||
return JSON.parse( | ||
response.choices[0].message.content as any | ||
) as OpenAIServiceProductTitleReturn; | ||
}; |