-
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.
- Loading branch information
1 parent
1d18888
commit 9ae0afc
Showing
8 changed files
with
226 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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, | ||
}); |
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 { 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); | ||
} | ||
); |
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,94 @@ | ||
import OpenAI from "openai"; | ||
import { shopifyAdmin } from "~/library/shopify"; | ||
import { CollectionsQuery } from "~/types/admin.generated"; | ||
|
||
const openai = new OpenAI({ | ||
apiKey: process.env.OPENAI_API_KEY, | ||
}); | ||
|
||
export const OpenAIServiceProductCategorize = async ({ | ||
title, | ||
description, | ||
}: { | ||
title: string; | ||
description?: string; | ||
}) => { | ||
try { | ||
const { data } = await shopifyAdmin().request(COLLECTIONS); | ||
|
||
const collections = data?.collections.nodes; | ||
|
||
// Prepare collections data as context | ||
const collectionsContext = JSON.stringify(collections, 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) | ||
.collections as CollectionsQuery["collections"]["nodes"]; | ||
} catch (error) { | ||
console.error("Error:", error); | ||
} | ||
}; | ||
|
||
const COLLECTION_FRAGMENT = `#graphql | ||
fragment CollectionFragment on Collection { | ||
id | ||
title | ||
description | ||
ruleSet { | ||
rules { | ||
column | ||
condition | ||
} | ||
} | ||
} | ||
` as const; | ||
|
||
export const COLLECTIONS = `#graphql | ||
${COLLECTION_FRAGMENT} | ||
query collections { | ||
collections(first: 250, query: "-Alle AND -Subcategory AND -User") { | ||
nodes { | ||
...CollectionFragment | ||
} | ||
} | ||
} | ||
` as const; |
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