Build a storefront chatbot with OpenAI
Topics covered: Shopify connections, AI + vector embeddings, HTTP routes
Time to build: ~30 minutes
Large Language Model (LLM) APIs allow developers to build apps that can understand and generate text. We can use OpenAI's APIs to build a chatbot that can understand a shopper's question and respond with product recommendations.
In this tutorial, you will learn how to:
- set up a Shopify and OpenAI connection in Gadget
- add custom data models and fields
- use OpenAI's text embedding API to generate vector embeddings for product descriptions
- use OpenAI's chat API to generate and stream a response to a shopper's question
- use a Shopify theme app extension to embed the chatbot into a storefront
Prerequisites
To get the most out of this tutorial, you will need:
- A Shopify Partners account
- A development store
- The Shopify CLI installed on your local machine
- Gadget's CLI: ggt, installed locally
Step 1: Create a Gadget app and connect to Shopify
Your first step will be to set up a Gadget project and connect to a Shopify store via the Shopify connection. Create a new Gadget application at gadget.new and select the Shopify app template.
Now we will set up a custom Shopify application in the Partners dashboard.
- Go to the Shopify Partner dashboard
- Click on the link to the Apps page
Both the Shopify store Admin and the Shopify Partner Dashboard have an Apps section. Ensure that you are on the Shopify Partner Dashboard before continuing.
- Click the Create App button
- Click the Create app manually button and enter a name for your Shopify app
- Click on Settings in the side nav bar
- Click on Plugins in the modal that opens
- Select Shopify from the list of plugins and connections
- Copy the Client ID and Client secret from your newly created Shopify app and paste the values into the Gadget Connections page
- Click Connect on the Gadget Connections page to move to scope and model selection
Now we get to select what Shopify scopes we give our application access to, while also picking what Shopify data models we want to import into our Gadget app.
- Select the scopes and models listed below and click Confirm to connect to the custom Shopify app
- Enable the Products Read API scope, and select the underlying Product and Product media models that we want to import into Gadget
Now we want to connect our Gadget app to our custom app in the Partner dashboard.
- In your Shopify app in the Partner dashboard, click on Configuration in the side nav bar so you can edit the App URL and Allowed redirection URL(s) fields
- Copy the App URL and Allowed redirection URL from the Gadget Connections page and paste them into your custom Shopify App
Now we need to install our Shopify app on a store.
- Go back to the Shopify Partner dashboard
- Click on Apps to go to the Apps page again
- Click on your custom app
- Click on Select store
- Click on the store we want to use to develop our app
- You may be prompted about Store transfer being disabled. This is okay, click Install anyway
- Click Install app to install your Gadget app on your Shopify store
If you are getting a permissions denied error when installing your app, try logging in to the Shopify store Admin!
You will be redirected to an embedded admin app that has been generated for you. The code for this app template can be found in web/routes/index.jsx
.
Now you can install your app on a store from the Partners dashboard. Do not sync data yet! You're going to add some code to generate vector embeddings for your products before the sync is run.
Step 2: Set up OpenAI connection
Now that you are connected to Shopify, you can also set up the OpenAI connection that will be used to fetch embeddings for product descriptions. Gadget provides OpenAI credits for testing while building your app, so you don't need a personal OpenAI API key to get started.
- Click on Settings in the nav bar
- Click on Plugins
- Click on the OpenAI connection
- Select the Use Gadget's API keys option in the modal that appears OR enter your own OpenAI API key
- Click Add connection at the bottom of the page
Your OpenAI connection is now ready to be used!
Step 3: Add data models for chat responses
Now we need a new data model to capture the chat responses from OpenAI, and the products recommended to shoppers. We need to use a has many through relationship to relate the chat response to the recommended products.
- Click + next to the
api/models
folder to add a new model - Name the model
chatLog
- On
api/models/chatLog/schema
, click + next to FIELDS to add a new field - Name the field
response
and leave it as a string field
This string field will record the response coming back from OpenAI. We also want to keep track of the products that were recommended, so we need to add a has many through relationship to the shopifyProduct
model.
- Add another field to
chatLog
calledrecommendedProducts
- Set the field type to a has many through relationship
- Set the
sibling
toshopifyProduct
and thejoinModel
torecommendedProduct
, this will create a newrecommendedProduct
model - Name the field in
recommendedProduct
that relates to thechatLog
model,chatLog
- Name the field in
recommendedProduct
that relates to theshopifyProduct
model,product
- Name the field in the
shopifyProduct
model that relates to therecommendedProduct
model,chatRecommendations
Step 4: Add vector field to shopifyProduct
model
Before you add code to create the embeddings from product descriptions, you need a place to store the generated embeddings. You can add a vector field to the shopifyProduct model to store the embeddings.
The vector field types store a vector, or array, of floats. It is useful for storing embeddings and will allow you to perform vector operations like cosine similarity, which helps you find the most similar products to a given chat message.
You are going to use Gadget's built-in OpenAI connection to generate vector embeddings for product descriptions. These embeddings will be used to perform a semantic search to find the products that best match a shopper's chat message.
LLMs and vector embeddings are relatively new technologies, and there are many resources available to learn more about them. Here are some resources to get you started:
- Building AI apps in Gadget
- Sorting by vector fields Gadget API docs
- OpenAI docs
To add a vector field to the shopifyProduct model:
- Go to
api/models/shopifyProduct/schema
- Click on + in the FIELDS section to add a new field
- Name the field
descriptionEmbedding
- Set the field type to vector
Now you are set up to store embeddings for products! The next step is adding code to generate these embeddings.
Step 5: Write code effect to create vector embedding
Now you can add some code to create vector embeddings for all products in your store. You will want to run this code when Shopify fires a products/create
or products/update
webhook. To do this, you will create a code effect that runs when a Shopify Product is created or updated.
- Go to the
api/models/shopifyProduct
folder and click on + to create a new file - Name the file
createEmbedding.js
- Paste the following code into
api/models/shopifyProduct/createEmbedding.js
:
api/models/shopifyProduct/createEmbedding.jsJavaScript1export const createProductEmbedding = async ({ record, api, logger, connections }) => {2 // only run if the product does not have an embedding, or if the title or body have changed3 if (!record.descriptionEmbedding || record.changed("title") || record.changed("body")) {4 try {5 // get an embedding for the product title + description using the OpenAI connection6 const response = await connections.openai.embeddings.create({7 input: `${record.title}: ${record.body}`,8 model: "text-embedding-ada-002",9 });10 const embedding = response.data[0].embedding;1112 // write to the Gadget Logs13 logger.info({ id: record.id }, "got product embedding");1415 // use the internal API to store vector embedding in Gadget database, on shopifyProduct model16 await api.internal.shopifyProduct.update(record.id, { shopifyProduct: { descriptionEmbedding: embedding } });17 } catch (error) {18 logger.error({ error }, "error creating embedding");19 }20 }21};
In this snippet:
- the OpenAI connection is accessed through
connections.openai
and theembeddings.create()
API is called - the internal API is used in the
onSuccess
function to update the shopifyProduct model and set thedescriptionEmbedding
field
The internal API needs to be used because the shopifyProduct
model does not have a Gadget API trigger on this action by default. You can read more about the internal API in the Gadget docs.
Now call this function from your api/models/shopifyProduct/actions/create.js
and api/models/shopifyProduct/actions/update.js
actions:
- Open
api/models/shopifyProduct/actions/create.js
and paste the following code:
api/models/shopifyProduct/actions/create.jsJavaScript1import { applyParams, preventCrossShopDataAccess, save, ActionOptions, CreateShopifyProductActionContext } from "gadget-server";2import { createProductEmbedding } from "../createEmbedding";34/**5 * @param { CreateShopifyProductActionContext } context6 */7export async function run({ params, record, logger, api, connections }) {8 applyParams(params, record);9 await preventCrossShopDataAccess(params, record);10 await save(record);11}1213/**14 * @param { CreateShopifyProductActionContext } context15 */16export async function onSuccess({ params, record, logger, api, connections }) {17 await createProductEmbedding({ record, api, logger, connections });18}1920/** @type { ActionOptions } */21export const options = {22 actionType: "create",23};
- Open
api/models/shopifyProduct/actions/update.js
and paste the following code:
api/models/shopifyProduct/actions/update.jsJavaScript1import { applyParams, preventCrossShopDataAccess, save, ActionOptions, UpdateShopifyProductActionContext } from "gadget-server";2import { createProductEmbedding } from "../createEmbedding";34/**5 * @param { UpdateShopifyProductActionContext } context6 */7export async function run({ params, record, logger, api, connections }) {8 applyParams(params, record);9 await preventCrossShopDataAccess(params, record);10 await save(record);11}1213/**14 * @param { UpdateShopifyProductActionContext } context15 */16export async function onSuccess({ params, record, logger, api, connections }) {17 await createProductEmbedding({ record, api, logger, connections });18}1920/** @type { ActionOptions } */21export const options = {22 actionType: "update",23};
Now vector embeddings will be generated when a new product is created, or an existing product is updated.
Generate embeddings for existing products
Now that the code is in place to generate vector embeddings for products, you can sync existing Shopify products into your Gadget app's database. To do this:
- Click on Settings in the nav bar
- Click on the Plugins page
- Select the Shopify connection
- Click on Installs for the connected Development app
- Click on the Sync button for the store you want to sync products from
Product and product media data will be synced from Shopify to your Gadget app's database. The code effect you added will run for each product and generate a vector embedding for the product. You can see these vector embeddings by going to the Data page for the shopifyProduct
model. The vector embeddings will be stored in the descriptionEmbedding
field.
If you are running into rate limit errors when generating embeddings while syncing, try using your own OpenAI API key. You can add your own API key to the OpenAI connection by clicking on the Settings tab in the OpenAI connection and selecting the Use my own API key option.
Step 6: Add /chat
HTTP route
We will use an HTTP route to handle incoming chat messages from the storefront. The route will take a message from the shopper, use cosine similarity to determine what products to recommend, and stream a response from OpenAI back to the client.
- Hover over the
api
folder and right-click on it to create a folder namedroutes
- Click + next to
api/routes
create a new file - Name the file POST-chat.js
Your app now has a new HTTP route that will be triggered when a POST request is made to /chat
. You can add code to this file to handle incoming chat messages.
- Paste the following code in
api/routes/POST-chat.js
:
api/routes/POST-chat.jsJavaScript1import { RouteContext } from "gadget-server";2import { openAIResponseStream } from "gadget-server/ai";34/**5 * Route handler for POST chat6 *7 * @param { RouteContext } route context - see: https://docs.gadget.dev/guides/http-routes/route-configuration#route-context8 *9 */10export default async function route({ request, reply, api, logger, connections }) {11 // get input from shopper12 const { message } = request.body;1314 // embed the incoming message from the user15 const embeddingResponse = await connections.openai.embeddings.create({ input: message, model: "text-embedding-ada-002" });1617 // find similar product descriptions18 const products = await api.shopifyProduct.findMany({19 sort: {20 descriptionEmbedding: {21 cosineSimilarityTo: embeddingResponse.data[0].embedding,22 },23 },24 first: 2,25 filter: {26 status: {27 equals: "active",28 },29 },30 select: {31 id: true,32 title: true,33 body: true,34 handle: true,35 shop: {36 domain: true,37 },38 featuredMedia: {39 file: {40 image: true,41 },42 },43 },44 });4546 // capture products in Gadget's Logs47 logger.info({ products, message: request.body.message }, "found products most similar to user input");4849 const prompt = `You are a helpful shopping assistant trying to match customers with the right product. You will be given a question from a customer and some JSON objects with the id, title, handle, and description (body) of products available for sale that roughly match the customer's question, as well as the store domain. Respond in HTML markup, with an anchor tag at the end with images that link to the product pages and <br /> tags between your text response and product recommendations. The anchor should be of the format: <a href={"https://" + {domain} + "/products/" + {handle}} target="_blank">{title}<img style={border: "1px black solid"} width="200px" src={product.featuredMedia.file.image.url} /></a> but with the domain, handle, and title replaced with passed-in variables. If you have recommended products, end your response with "Click on a product to learn more!" If you are unsure or if the question seems unrelated to shopping, say "Sorry, I don't know how to help with that", and include some suggestions for better questions to ask. Here are the json products you can use to generate a response: ${JSON.stringify(50 products51 )}`;5253 // send prompt and similar products to OpenAI to generate a response54 // using GPT-4 Turbo model55 const chatResponse = await connections.openai.chat.completions.create({56 model: "gpt-4-1106-preview",57 messages: [58 {59 role: "system",60 content: prompt,61 },62 { role: "user", content: message },63 ],64 stream: true,65 });6667 // function fired after the steam is finished68 const onComplete = (content) => {69 // store the response from OpenAI, and the products that were recommended70 const recommendedProducts = products.map((product) => ({71 create: {72 product: {73 _link: product.id,74 },75 },76 }));77 void api.chatLog.create({78 response: content,79 recommendedProducts,80 });81 };8283 await reply.send(openAIResponseStream(chatResponse, { onComplete }));84}
This code will:
- create a vector embedding from the shopper's message using the OpenAI connection using
connections.openai.embeddings.create()
- use cosine similarity to find the 2 most similar products to the shopper's message using the Gadget API:
cosine similarity in api/routes/POST-chat.jsJavaScript1const products = await api.shopifyProduct.findMany({2 sort: {3 descriptionEmbedding: {4 // cosine similarity to the embedding of the shopper's message5 cosineSimilarityTo: embeddingResponse.data[0].embedding,6 },7 },8 first: 2,9 select: {...}10});
- use the OpenAI chat API to generate a response using the
gpt-4-1106-preview
model
stream chat completion in api/routes/POST-chat.jsJavaScript1const chatResponse = await connections.openai.chat.completions.create({2 model: "gpt-4-1106-preview",3 messages: [4 {5 role: "system",6 content: prompt,7 },8 { role: "user", content: message },9 ],10 stream: true,11});
- stream the response back to the client and save the records to the database using
reply.send(openAIResponseStream(chatResponse, { onComplete }))
onComplete
is a callback function that is called after the stream is finished
Set up CORS handling
Before we can call our /chat
route from a theme extension, we need to enable CORS handling. To do this we will use the @fastify/cors
plugin.
- Open the Gadget command palette using P or Ctrl P
- Enter
>
in the palette to change to command-mode - Run the following command to install the
@fastify/cors
plugin:
Run in the Gadget command paletteyarn add @fastify/cors
Once the plugin is installed, you can add it to your app's configuration:
- Add a new file in the
api/routes
folder called+scope.js
- Paste the following code into
api/routes/+scope.js
:
api/routes/+scope.jsJavaScript1import { Server } from "gadget-server";2import cors from "@fastify/cors";34/**5 * Route plugin for *6 *7 * @param { Server } server - server instance to customize, with customizations scoped to descendant paths8 *9 * @see {@link https://www.fastify.dev/docs/latest/Reference/Server}10 */11export default async function (server) {12 await server.register(cors, {13 origin: true, // allow requests from any domain14 });15}
This will allow requests from ANY domain! For your actual, production app, you probably want to set the origin
option to a specific domain - the domain of your store. Read more about CORS in Gadget in our documentation.
Your route is now complete! Now all that is needed is a frontend app that allows shoppers to ask a question and displays the response along with product recommendations.
Step 7: Use Shopify theme app extension to embed chatbot
We make use of an app embed block for our storefront chatbot theme extension. This means that our extension works with any Shopify theme, both vintage and Online Store 2.0 themes.
- Use git to clone the Shopify CLI app
You can also build Shopify extensions inside your Gadget project directory so you can manage your entire app with a single Git repo. This tutorial skips this step due to all the assets that need to be copied for the chatbot!
cd
to the cloned directory- Update the direct script tag in
extensions/theme-extension/blocks/chatbot.liquid
to include your app's script tag URL
Your script tag needs --development added to your app-specific subdomain when working on your Development environment:
<script src="https://example-app--development.gadget.appapi/client/web.min.js" defer="defer"></script>
- Run
shopify app dev
to start your app and connect to your existing Partners app and development store
You should now be set up and ready to add the chatbot theme app extension to the storefront theme.
- Navigate to your storefront theme editor and click on App embeds in the left sidebar
- Enable your app - the chatbot should appear in your storefront preview
- Click Save in the top right corner of the page
Theme extensions allow you to create custom blocks that merchants can place in the storefront theme editor. You can read more about theme extensions in the Shopify docs. This chatbot extension is an app embed block, so it works with both vintage and Online Store 2.0 themes.
In this theme there are 3 main files:
blocks/chatbot.liquid
- the main file that renders the chatbotassets/chatbot.js
- handles the chatbot logic and calls your Gadget app's APIassets/chatbot.css
- the CSS file that styles the chatbot
Your chatbot should now be available in your storefront - try it out!
Next steps
You now have a chatbot that can respond to shopper questions with product recommendations! You can continue to build on this app by:
- customizing the embedded React frontend, found in the
frontend
folder, to give merchants installation instructions - editing the prompt in
routes/POST-chat.js
to customize the chatbot's response - change the look, feel, and merchant customization options for the chatbot by editing the theme extension files
- maintain full chat context by passing
messages
back and forth between the client and server- also maintain chat context between browser sessions and/or windows by storing the full chat context in the database
Have questions about the tutorial? Join Gadget's developer Discord to ask Gadget employees and join the Gadget developer community!
Want to learn more about building AI apps in Gadget? Check out our building AI apps documentation.