Using OpenAI Embeddings API to implement Semantic Search into a HubSpot Workflow

Roman from Cohorto walks through building a HubSpot workflow that uses OpenAI embeddings to search tickets semantically. The setup uses HubSpot's tickets API and custom code actions to match support requests to similar past cases, automating ticket assignment and faster resolution.
Hi, my name is Roman. I am a managing partner at Cohorto.
In this tutorial, I will create a workflow that helps customer support team to respond quickly and accurately to client issues. Specifically, I will show you how to use external semantic Search API in the workflow.
Semantic search is a key feature here.
It allows us to compare incoming tickets to the database of previously resolved. This way we can quickly assign a ticket to the right support agent and provide them with necessary information to resolve the issue. The workflow is relatively simple. It starts when a new ticket is created and runs a custom code action. The action is a bit more complex. It consists of three operations. At first it will find similar tickets in the system. Then it will assign the new ticket to the same support agent who has previously resolved the closest similar ticket. And lastly, we'll update the ticket with links to similar tickets. To implement this custom code action, we will use a combination of HubSpot tickets API and OpenAI embeddings API. The main idea of using these APIs is to get a title and description of each ticket and convert it into a unique numerical vector representation. Then compare pairs of those vectors to identify the most similar. To set up this workflow in my household portal, I will perform a series of steps. First, I will create a workflow for incoming tickets. Then I will set up a trigger that will start the workflow when a new ticket is created. And finally, I will add a custom code action and configure it. So let's start.
Alright, I'll log into my developer portal. It's a new portal with all default settings. The only thing I have added here is it's a few dummy tickets. Let me show you them first. So here I have a table with 10 records. As you probably noticed, these tickets were assigned to different owners. The workflow will use these owners to assign a new ticket to one of them. Beside that, the workflow will need that each ticket has a name and description. Here we have ticket names and we don't see it in the table. Here we have a ticket description. All these tickets have a description as well. And I believe that's all preparation that I made in this portal. Now let's go and create the workflow. So I'm on the workflows page and as you can see I don't have any workflows in my portal. I can create a new one by clicking the Create Workflow button. There's two options from scratch and from template. I need from scratch to proceed, I need to choose a CRM object type here in the left panel, a chosen type will define the data that will be enrolled in the workflow. I need workflow for tickets so I'll select ticket based workflow. Also there is a question about how I want to start the workflow. The blank workflow option works well for me. I don't need to choose anything here and I can proceed by clicking the next button. Now I am on the workflow editor page. Here I can set up the workflow logic. As you probably know, each workflow starts with a trigger. So I need to set up a trigger first. I have a box here that can help me to do this. Here in the right panel there is a list of CRM objects. The first is a ticket object that can be enrolled in the workflow. The rest are objects that are associated with the ticket. So I can filter based on ticket properties or other object properties. I want to enroll the workflow only for new tickets, so I choose ticket object and its property status. Whenever ticket has status new, it's good to be enrolled so I can apply the filter and save the trigger. Now when I have the trigger I need linked action for it. Here I have a button with plus sign. By clicking this button I can get the list of allowed actions I need. Custom code action the custom code action is probably the most complex action in this list. There are a lot of happening here. We have language select we need node JS secrets which also called environment variables, Incoming data structure the code I'll delete this one, the output data structure and the section where we can test this custom code action. I would encourage you to go to official documentation page about custom code workflow actions and read about it if you want to do beyond the scope of this tutorial. Okay, let's start with the code section. I have prepared code a GitHub repository. It's public repository and you will get the link to this repository in the description to this video. So here I can copy the code and paste it into the code section. Now whenever we have the code section we need to provide the incoming data to this code. For this we will use this section property. To include in the code we will need four properties Ticket name, Ticket description, Ticket owner and the Last1ticket ID. We don't need to change name of these variables because the code uses the same names by default. Let me show you line 11, subject content, HubSpot, owner ID and ticket ID. I also want to show you two more variables that need to be assigned upfront. The first one HubSpot token and the second OpenAI token. These two variables we can assign in a secret section by clicking this add secret. The name has to be the same as in the code. So I'll go and copy the first name Household token, paste it. So we have name and we need to get some more value. We can do this by going to the same portal settings private apps and create private app. We don't need change basic info here. We just only need to add one scope which is tickets and we can create the app and now we can get token and go back to our workflow. That's how we get secret value for secret HubSpot token. Okay, the second variable OpenAI token it will be a little more harder. We have OpenAI token. I have opened OpenAI account in another tab and UAP keys generate new key copy OK and go to back to our workflow. Okay, that was the last step that is needed to set up custom code action. Let's go through all section of custom code again. So we have to have language not JS2 environment variables. HubSpot token, OpenAI token 4 incoming data fields. Ticket name, description of ticket ID the code from my GitHub repository and that's all we don't need to change data outputs because we don't use data from this custom code action. And we can save it now. Okay, it seems now like testing time. We can test the whole workflow or we can just test solely custom code action. Let's just test custom code action and then test the whole workflow. Here we have test action section and here we can choose etiquette that we will test on. I will tell you up front it will not work. It will spit out error. Let's see the error and based on the message take next steps. Okay, status success. But there is error. Okay, it says property value not valid. What is property ticket AI embedded. What does it mean? This code stores custom data in the ticket object. And for that we need to set up ticket AI embeddings fields, a custom kit and one more. Let me show you that. I'll open the code section full screen again. Here we are. So we have function, it's called update ticket and this function update ticket with two custom custom fields Ticket AI embedded the first one from the error message and second one Similar tickets. Similar tickets is a text that will help us to navigate to similar tickets by clicking links in this text. Okay, how we can set up this custom fields in ticket object we can go another tab where I created product tab and here we have data management section. In data management we have Objects ticket object. Here we need to click manage ticket properties and create new property label was copied from the code. I don't need description, just need to check to select group will be ticket information. Okay, this will be a multiline text and create. I don't know, I don't need any checks here. Okay. Okay. And the second field was similar tickets. Okay, again create property ticket information. Next this will be rich text because code stores it in HTML format so we can click on links when we have similar tickets create. Now we have added two custom properties to the ticket object and now we can test again. Let's be the same test and we don't have errors and we have also success status. Okay, save it so it's ready and we can review and publish the whole workflow. As all my dummy tickets has status new, I can enroll existing tickets in this workflow and tickets will be populated with ticket AI invariance data for semantic search. Hey, to make it faster, I will enroll it manually. Good. Now let's test the whole workflow. For that I'll go to tickets section and create new ticket. I will use GPT3 to generate some new dummy ticket. This will be the ticket name and ticket description. I can set up source email and I don't need ticket owner because our workflow will assign the ticket owner. Great. Okay, on let's see all properties of our ticket. There is no other and there is no similar tickets and no ticket AI embeddings. Let's go back, let's refresh and now we see the login issue has owner. Let's check again properties and there is there is similar tickets and also ticket AI embeddings. Let's see. So we have four links to the similar tickets and we have similarity score number one the most similar identical tickets. The closer to one, the more similar tickets are and we can check also similar tickets unable to login into account. And here we have enable into login my account pretty similar. That's all. Thanks for being with me in this tutorial. There's also an article on HubSpot developer blog where you can check more details about how it works, what why it works and how you can create it for yourself. Thanks. Bye.










