LLMs will Transform Data Science - Here's How

Rabbitmetrics · Intermediate ·🧠 Large Language Models ·2y ago

Key Takeaways

This video teaches how to leverage OpenAI and LLM Function Calling to create a targeted communication pipeline for email marketers using Klaviyo

Full Transcript

function calling is one of the coolest features of openi language models by enabling these models to Output Json we can turn them into autonomous agents capable of accessing a range of useful tools but there is more to this feature than just creating agents what if I told you that the traditional tools you're using as a data scientist are about to be transformed in ways you didn't expect in this video I'm going to give you two specific examples from the world of customer analytics first I'll demonstrate how llms could replace traditional machine learning in specific scenarios offering a more effective solution then we'll see how leveraging llm function calling can dramatically accelerate data analysis and insight generation I'm going to be using the open AI API and gbt 4 but keep in mind that what we're doing here can likely be achieved with the right open source model as well it just requires a bit more work by the end of this video you'll have an overview of how you can leverage function calling and the code that lets you work and specific data science use cases more efficiently if you new here like And subscribe I'll put a link to the code below this video so what exactly is function calling in the context of om and llms function calling enables developers to retrieve structured data from omi's gbt Models consistently allowing the gbt to take userdefined functions as input and generate Json output the opening I API will not execute the function but will generate the arguments needed to call the function and the way it works in a not shell is that a user will send a request formulated in natural language the language model is provided with a list of function descriptions that allows the language model to return the Json data needed to make the correct function call this is possible because gbt 4 is specifically fine-tuned to increase function coding accuracy and reliability let's have a look at how this works first by looking at the documentation and then by writing some code all right so here I am in the Omi API reference documentation specifically looking at the chat completion endpoint and as you can see here the most recent version of gb4 allows you to pass in a response format and this allows you to tell the model that you want a Jason object as an output and note that in order for this to be robust and reliable you also need to instruct the model to produce Json either in a system prompt or a user prompt we can also pass in a list of tools right now it's only functions that you can pass to the model and the description name parameters is used by the model to generate Json inputs for those functions so in this video I'm going to focus on the Json part because that is the most fundamental feature we need to understand the implications of so let's head over to a collab notebook and start exploring I'm going to start off with a simple example of an API call that returns Json and then build on that I have my open API key in an environment file and then I'm going to load that using python. EnV then I'm going to import om Ai and instantiate the client and then I'm ready to make a function call I'm not actually going to call a function I'm just going to have the language model return Json and the example we're going to look at is extracting the age and the gender from a customer name this is a classical exercise in customer analytics because it helps marketers Target the messaging to the specific customer there's actually an old article on this in 538 that goes into how this can be done using probabilistic modeling I'll put a link to this article below the video but here we're going to focus on using gbt 4 for this so in this example I have a customer named Karen Backstrom and I'm simply prompting gbt 4 to give me a guess of the age and gender of the customer based on the name I know that I'm specifically asking for adjacent object and then I'm simply passing this prompt to the chat completions endpoint using the gbt 4 1106 preview model and of course I'm specifying that I want the response format to be ajacent object if we look at the content of The Returned message you can see that we get a name because I specified that I wanted a name back we get an estimated age and we get a gender now this is already useful and you can probably see where I'm going with this because we can pass a lot more than the name of the customer to the language model and we can have the language model model tell us what type of buyer Persona Karen is so what is a buyer Persona a buyer Persona is a fictional representation of an ideal customer based on market research and data on existing customers it typically includes details like demographics Behavior patterns motivations and goals any brand should have a clear idea of the target buyer personas based on the product selection alone and if these personas are really representing the customer base then any c customer with a purchase history should somehow fit into one of these fictional representations and as a customer increases engagement with a brand over time more and more data will be available and the best guess of a Persona type will typically change based on the order data and the behavior data the main reason we want to understand the Persona type of a given customer is because it helps the brand communicate with that customer and this is important because this is why language modeling is a useful tool for this so I asked chat dutt about personas for a fashion brand operating in London chat DT has no problem coming up with the different Persona types it'll give you age groups gender interests and behavior and what else you might want and you can ask chat to give you more information if you'd like to refine the groups or you can upload a file with the product catalog which will make the groups even better given these personas I then ask chat dut to place a given customer in one of those categories and here I have have a Karen Backstrom that bought a Fossil women pink dial chronograph watch and as you can see chat can reason about the customer and places Karen in the professional women's category that means we can feed gbt 4 with upto-date information about a customer and have the language model make a best guess about the Persona type now let's write some code for a pipeline that will dynamically update Persona information based on customer data so so I have a personas TT file with 20 personas for a fashion brand similar to what I just showed you in chat DBT now what I want to do I want to use these 20 personas as a basis for making the best guess about the Persona type of a given customer so I'm just going to load the 20 base personas into a python variable and then let's have a look at some specific examples of customer data here I have a development Shopify store with some sample products we have some backpack some shoes and some kits items this Shopify store is linked to the email service provided clavio so that every time an order is made it's going to appear in the clavio dashboard and clavio not only tracks orders but also events on the web page so we can head over to clay.com and log in this is the main dashboard all see and I can fetch profiles of my customers by clicking profiles and then select one of the customers and here I have a John Doe that ordered a Addidas classic backpack and every customer in here has a unique ID that we can use to fetch the customer data that is collected besides John in here I also have Karen Backstrom and she ordered a different backpack the hersel Iona backpack and the unique ID for every customer can be found in the URL or under profile details for every customer in here clavio is collecting events tied to specific metrics so if we click metrics under analytics we can see a list of all the metrics and of course all the metric or the events tied to the metrix contain information that can be used to take the marketing towards a specific customer so I'm going to focus on ordered product and I'm going to fetch all the events tied to this metric for specific customer using the ID of the metric that can be found in the URL as well now let's fetch some data for John and Karen I'm going to use the python rabber around the clavio API you can install that by running pip install clavio API then I'm going to instantiate the API using the clavio API key that I've stored in the environment file and here I have the IDS for John and Karen and then I have a simple python function that allows me to extract all the attributes that are linked to the profiles of the customer as well as the events that are linked to the metric ordered product and the function is just going to return everything as a string so I can feed it to the language model so let me just extract the data for John and as you can see here the data is returned as a string in a raw format there's no feat engineering going on no nothing we're just extracting raw data from the API let me just extract the data for Karen as well and that also works we see that we have the purchase of the heral AO oner back so I'm just going to extract the data for John again and now we want to feed this to the language model to do that I have a Persona prompt that is similar to the one I use to extract the age and gender from the name I will inject the customer data the 20 based personas and then I'll ask the language model to give me the Persona category name the interests the behavior and the age and gender of the customer and let me start by doing this for John I'll call the chat completions endpoint with the Persona prompt asking for Jason in return and here we have the Persona category information for John he's a young professional male he's interested in quality brand reputation maintaining a professional image and expressing personal style and let me do the same thing for Karen I'm just going to fetch Karen's data and then I'm going to prompt gbt 4 with Karen's data and here we see that gbg4 places Karen in the corporate female category which is based on the name and the bag that she bought now the cool thing about this is that whenever something changes in the data the Persona category is going to be updated and to illustrate this I've made an additional order in Karen's name now she's also purchased the converse top Taylor Shoes of course this signals that Karen might be a mom and it might be a better way to communicate to Karen as a mom instead of a corporate female so if I head back to the notebook and I update Ken data we can see that the data now contains an additional order the toddler Chuck Taylor shoes and if I ask for the Persona category based on this updated dat data I'm going to get an updated guess from the language model so I'll hit the chat completions endpoint again with the updated prompt and here we see that gbt 4 places Karen in the category stylish mom and of course we don't really know what the true Persona is we don't have a Target Persona category we can use for modeling purposes and that's the whole point traditional machine learning might actually have been the wrong tool for this let's have a look at a workflow like this using a more traditional approach like standard machine learning so first you'd have to transform the raw data into feature data that you could feed the machine learning model then you would have to do the modeling in this case it would be some sort of propensity model or customer Affinity model and as I mentioned you don't know the true Persona type so you don't have a Target variable complicating the problem at least making the task non-trivial then you would put rules on on top of the output of the models depending on what type of model You're Building you probably need to map the output or the score of the model to a Persona category then you would need to tag the customer with a suggested messaging and finally marketus would need to formulate the copy for the emails or the advertising now compare this to what we just did we take the raw data pass it to the llm and we get a Persona category and we're not done yet in a second I'm going to take this all the way to the right to serve the business but you already get the point not only is Persona prediction easy with llms but because we ultimately care about taking RI maximizing actions in the form of text the llm is a superior tool for this now let's finish this so we create something that the marketers can actually use if we head over to the customer profile of Karen Backstrom in clavio we can see that we have a section called custom properties and we can enrich Karen's profile with our own scores and predictions so I'm going to tag Karen with a Persona type we can then use those tags to create specific Persona segments that can be targeted in floing campaigns and I can easily do that with the python wber around the Cav API so I'm going to formulate a payload where I add the Persona attributes as properties and of course I'm going to need Ken ID and then I'm going to to use the update profile method so now Karen is tagged with her Persona category so let's head over to clavier again and refresh the customer profile page of Karen Backstrom to see that this is actually the case and here we have the Persona attributes so now we can create segments in clavio based on these Persona attributes like age Behavior gender interest and so on but because we're used using a language model for this we can go even further than that and to do that we'll have a look at how an email marketer formulates a campaign so this is the main web page of Chase Diamond who's running an email marketing agency he's very active on Twitter I'll put a link to his Twitter profile below the video and the reason I'm using Chase Diamond as an example is because he has this beautiful article on using chat gbt for writing the copy for email marketing campaigns and as you can see here he's listing 25 copyrighting Frameworks that you can use used to hook the customer for instance we have the attention interest desire action framework and as you can see here these Frameworks take a customer Persona as input now what I'm going to do is I'm going to take one of these Frameworks and I'm going to inject a Persona type and formulate an email template for a marketing campaign and to do that I'll need a Persona category name I'll prompt the system telling the model that it's a worldclass marketing algorithm that writes personalized emails for fashion brands and then I'm going to ask it to write an email using the attention interest desire action framework and grab the attention of the specific Persona name we're targeting I'm also going to mention a specific product in this case the added Des classic sneakers and I wanted to use the angle increase comfort and then I'll hit the chat completions endpoint with the system prompt and the user prompt and this should give us an email template that we can use for personalization so this is the response from the chat model and here we have a nice email template that can be used to Target the Persona type of Karen which is stylish mom and of course we can just switch out the framework of the product as we see fit so this is nice but we don't want this template in a notebook we want it inside of cavio so I'm going to push it using the API so I'm just going to wrap it in simple HTML and then I'm going to push this using the create template method and of course you can also build a lot more advanced HTML templates so now we have this template inside of clavio and it can be used to Target the messaging to stylish Ms so let's just check that it's in our templates we'll click templates and here we have the top one that was just created so now we have the tax and the customers and we have the templat in the system which is what we need to run targeted communication towards the customers and it's safe to say that if you wanted to automate this using traditional machine learning it would require a lot more work than what we just did so now we have this simple plain template here in glavo we also have some pre-built HTML templates in here so let's continue building on this use case and and move on to the data analysis part so imagine you are a data analyst working for a consumer facing company that uses clayo for email marketing and one day your boss comes into your office and wants to know why some email marketing campaigns are converting really well While others are not performing now this is a big question and it's obviously valuable to figure this out and probably the reason that you were hired as a data analyst in the first place but answering a question like this is like looking for a needle in a hyack there are so many different factors that can affect the performance of an email marketing campaign the HTML templates that are being used in the campaign can affect the conversion rates it could be the font size the font color the text the images the messaging the marketing angle the call to action and so on you really want to AB test everything but where do you start to find the needle in the ha stack you need some sort of magnet but luckily an llm like gpc4 is the magnet you need and to show you what I mean by this let's head back to the collab notebook and extract the data for analysis for this part of the video I'm going to use Lang chain as a wrapper around Omi because Lang chain has a really nice tagging functionality that I'm going to use so here I have a campaign performance data set in adjacent file and you can create a file like this by extracting performance data from the cavio API but I'm not going to burn your time with this as it's it's pretty tedious instead I'm going to use this file and I'm going to make sure that this file is available with the code so I'm loading the campaign performance data into a pandas data frame and here we have the data we have a campaign ID template ID a customer ID the name email the stage of the customer journey and then we have the indicators of whether the email was open clicked and whether or not there was a conversion so with a data set like this you will typically Group by campaign ID and then sum the indicator variables so that you get an idea of the overall performance for each campaign and let me just do that here so we see what that looks like so here we have the aggregated data grouped by campaign ID that allows us to get an overview of what campaigns are performing well and what campaigns are not performing and this is really nice for building dashboards to give you an overview of how your marketing is performing but in order to proove the business we need to go deeper than this we need to understand what it is about the campaign that makes it perform so I'm just going to delete this aggregation again and then let's try to dig deeper using language modeling in order to dig deeper I first need to fetch the templates that we used in the campaigns I'll do that using the clavier API this time I'll just use requests and note that I'm sorting in the URL so I'm getting the last templates created and if I just fetch the first one here we'll see that we get the template that we just created before and I think template number five or number six in the array is one of the pre-built HTML templates and as you can see this one has a lot more going on HTML wise than the one we created before now the reason I wanted to use Lang chain as a WRA around open AI is because Lang chain has a chain type called create tagging chain that is very useful for what we're going to do and Lang chain tagging uses Omar function calling under the hood and allows us to pass a schema and a language model to the chain and then have the chain tag a given document with the features of that document and the document can be in plain text or it can be code like HTML and in order to do the tagging I'll import chat Omi from linkchain chat models and I'll import create tagging chain from Lang chain chains then I'm going to define a schema I'm going to start with the pre-built HTML templates and let's say I wanted to understand how the different features of the template will affect conversion rates such as the font color the background color of the template the call to action that's being used and I can simply specify what features I want to extract from the template in the schema and then the language model will extract those features so if you run the chain on template number five the one we had to look at before see that the chain will extract and return the features in Json format and this is very useful once we add this to the performance aggregation we did before but before I do that let me go ahead and do something similar for the plain text templates we can also extract other types of features such as marketing angles or persuasiveness of the copy and we can extract the underlying copywriting framework the ones we use to generate the email template and note that we can specifically control the values for each property in the schema so I'm going to create a new chain out of this new schema and then I'm going to run this on the first four templates in the template array because these are plain text templates and these are the templates that we used in the campaigns in the performance data set we had in the data frame so I'm just going to create a template dictionary where the keys are the template IDs and the values the features of the template and that allows me to to attach the features of the template to each row in the campaign frame and if we have a look at the final frame here we can see that we now have the features of the template that was used in the specific campaign attached to each row and this allows us to dig deeper into what is really driving the performance of a given campaign so before we could Group by a campaign ID or a template ID but now we can Group by a marketing angle for instance and understand how the different marketing angles affect the performance of The Campaign and here we see that the marketing angle improved status and luxury seems to be an angle that's working really well and of course we can go one step deeper than this we can Group by marketing angle and the customer Journey stage and this breakdown shows us that the marketing angle improved status and luxury Works particularly well for customers in the welcome stage in the welcome flow and this is just one example showing how you combine features extracted using llm function coding with your email marketing performance data to generate actionable insights I hope this video gave you an idea of how om function calling and function calling in general can be used as a powerful data science tool tool if you enjoyed the video like And subscribe thanks for watching

Original Description

LLM Function Calling will 10x your Data Science Efficiency. I'll show you how to leverage OpenAI to create a targeted communication pipeline for email marketers using the email service provider Klaviyo as a demonstration. Code and data used in the video: https://github.com/rabbitmetrics/openai-datascience Estimating age from name: https://fivethirtyeight.com/features/how-to-tell-someones-age-when-all-you-know-is-her-name/ Copywriting frameworks for email marketing: https://www.chasedimond.com/25-chatgpt-copywriting-prompts-1 https://twitter.com/ecomchasedimond ▬▬▬▬▬▬ V I D E O C H A P T E R S & T I M E S T A M P S ▬▬▬▬▬▬ 0:00 Intro: LLM Function Calling 3:00 Extracting Structured Data From Text 4:30 Predicting Buyer Personas 13:48 Tagging Customers 15:00 Engineering Email Marketing Campaigns with LLMs 18:00 Analyzing Email Marketing Data 21:20 LangChain Tagging as a Data Analysis Tool 22:40 Generating Actionable Insights with LLMs
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Chapters (8)

Intro: LLM Function Calling
3:00 Extracting Structured Data From Text
4:30 Predicting Buyer Personas
13:48 Tagging Customers
15:00 Engineering Email Marketing Campaigns with LLMs
18:00 Analyzing Email Marketing Data
21:20 LangChain Tagging as a Data Analysis Tool
22:40 Generating Actionable Insights with LLMs
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →