Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add openapi-categorize-endpoint #165

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/deploy-azure-functions-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
npm install
npm run graphql:codegen --if-present
npm run codegen --if-present
npm run build --if-present
popd

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jobs:
- name: Install Dependencies
run: npm ci
- name: Generate GraphQL types
run: npm run graphql:codegen
run: npm run codegen
- name: Run Tests
run: npm run test
84 changes: 81 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"generate-docs": "npx @redocly/cli build-docs docs/openapi.yaml -o docs/index.html --title 'Booking Api Documentation'",
"generate-ts": "npm run bundle && npx orval --config ./orval.config.js",
"postprocess": "node postprocess.js",
"graphql:codegen": "npx graphql-codegen -- --watch && npm run postprocess"
"codegen": "npx graphql-codegen -- --watch && npm run postprocess"
},
"dependencies": {
"@azure/functions": "^4.3.0",
Expand All @@ -34,6 +34,7 @@
"jsonwebtoken": "^9.0.2",
"module-alias": "^2.2.3",
"mongoose": "^7.6.10",
"openai": "^4.52.1",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions src/functions/openai.function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { app } from "@azure/functions";
import "module-alias/register";
import { OpenAIControllerProductCategorize } from "./openai/controllers/product-categorize";

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

export type OpenAIControllerProductCategorizeRequest = {
body: z.infer<typeof OpenAIServiceProductCategorizeSchema>;
};

export const OpenAIServiceProductCategorizeSchema = z.object({
title: z.string(),
description: z.string(),
});

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

export const OpenAIControllerProductCategorize = _(
({ body }: OpenAIControllerProductCategorizeRequest) => {
const validateBody = OpenAIServiceProductCategorizeSchema.parse(body);
return OpenAIServiceProductCategorize(validateBody);
}
);
62 changes: 62 additions & 0 deletions src/functions/openai/services/product-categorize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import OpenAI from "openai";

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

export const OpenAIServiceProductCategorize = async ({
title,
description,
}: {
title: string;
description?: string;
}) => {
try {
// Prepare collections data as context
const collectionsContext = JSON.stringify([], null, 2);

const content = `
Given the following product title and description, response with the collection titles that this product fits into. The JSON structure should be:
{
"collections": [
{
id: "gid://shopify/Collection/1111",
title: "example",
ruleSet: {
rules: [{
column
condition
}],
},
},
],
}
Where:
- "collections" includes the existing collections that the product fits into based on the given list of collections.
### Existing Collections:
${collectionsContext}
### Product Details:
Product Title: ${title}
Product Description: ${description}
If you think the product fits multiply collections, it's fine, include them all in the response.
`;

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

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