-
-
Save jadenguitarman/e883285b0aeace386d1fe8469c947beb to your computer and use it in GitHub Desktop.
Personalizing Product Descriptions with Algolia + GPT-5
This file contains hidden or 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
| import OpenAI from 'openai'; | |
| const fetchUserProfile = async (userID, ALGOLIA_APPLICATION_ID, ALGOLIA_API_KEY, ALGOLIA_REGION="eu") => { | |
| const fallbackUserProfile = { | |
| userID, | |
| type: "basic", | |
| affinities: [], | |
| lastUpdatedAt: new Date().toISOString(), | |
| }; | |
| try { | |
| const response = await fetch( | |
| `https://ai-personalization.${ALGOLIA_REGION}.algolia.com/2/users/${encodeURIComponent(userID)}`, | |
| { | |
| headers: { | |
| "X-Algolia-Application-Id": ALGOLIA_APPLICATION_ID, | |
| "X-Algolia-API-Key": ALGOLIA_API_KEY, | |
| }, | |
| } | |
| ); | |
| if (!response.ok) { | |
| console.error(await response.text()); | |
| return fallbackUserProfile; | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| console.error(error) | |
| return fallbackUserProfile; | |
| } | |
| }; | |
| const sendToGPT5 = (instructions, input, OPENAI_API_KEY) => // Returns a promise | |
| (new OpenAI({ apiKey: OPENAI_API_KEY })).responses.create({ | |
| model: "gpt-5-nano", | |
| reasoning: { effort: "low" }, | |
| instructions, | |
| input | |
| }); | |
| const convertProfileToMarkdown = ({ userID, affinities }) => [ | |
| `Affinities for user "${userID}":`, | |
| ...affinities.map(affinity => | |
| `- ${affinity.name}: ${affinity.value} (score: ${affinity.score})` | |
| ) | |
| ].join("\n"); | |
| export const personalizeCopy = async (userID, copy, credentials) => { | |
| const userProfile = await fetchUserProfile(userID, credentials.ALGOLIA_APPLICATION_ID, credentials.ALGOLIA_API_KEY, credentials.ALGOLIA_REGION); | |
| if (userProfile.affinities.length == 0) return; | |
| const markdownProfile = convertProfileToMarkdown(userProfile); | |
| const instructions = `You're a marketing copywriter personalizing ecommerce copy, such as product descriptions. Given a user's preference profile (facet scores) and a generic piece of copy, rewrite that content to emphasize what the user cares about most. Be subtle but relevant, maintaining the general tone and length of the original. Do not respond with anything other than the rewritten copy.`; | |
| const prompt = [ | |
| markdownProfile, | |
| "", | |
| "Original copy:", | |
| copy, | |
| "", | |
| "Rewrite this copy based on the user's preferences." | |
| ].join("\n"); | |
| const response = await sendToGPT5(instructions, prompt, credentials.OPENAI_API_KEY); | |
| return { markdownProfile, output: response.output_text }; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
credentialsobject that gets passed into thepersonalizeCopyfunction is this format:To run this on the backend, you'll first need to
npm install openai. If you're trying to run it from the frontend (which isn't recommended since that involves exposing your OpenAI secret key), you can use link to the OpenAI npm module on jsDelivr:import openai from 'https://cdn.jsdelivr.net/npm/openai@5.12.2/+esm'. You would also need to explicitly authorize dangerous frontend usage - an error message will explain how to do that.