Using AI to Enhance Product Pages with LangChain and Python

Richie Cotton and the Storybrain team show how to use LangChain and OpenAI to automatically pull product specs and review summaries from e-commerce pages. They extract structured data like RAM and CPU from descriptions, scrape dynamic reviews with Playwright, and feed them to an LLM to generate pros, cons, and summaries.
Chapters
Sam. Foreign.
And thank you for joining today's code along. My name is Rhys and I'll be your moderator today. We're going to kick off today's session in a couple of minutes. We're just waiting so everyone has a chance to join. In the meanwhile though, we'd love to hear from you. So let us know where you're joining from using the chat, the comments depending on what platform you're watching from. And yeah, tell us something that you'd like to learn from today's webinar. We are going to be using datacamp workspace in the session today, so if you don't have an account already, please sign up for one. I will post a link to that in the chat in the comments now to make it a bit easier for you. But yeah, I'll be back to repeat these messages for any new joiner shortly. So until then, enjoy the background music. One more, one more thing to note as well. If you haven't registered for the event, please do because we will send you the any of the resources and the recording as well in the next few days. So yeah, head over to LinkedIn and you should be able to find the event on Data Camp and you should see we're live. So yeah, make sure you get registered so that you can get sent all the resources after the session.
Foreign.
And thank you for joining today's code along. My name is Reece and I'm going to be a moderator today. We're going to kick off today's session in a couple of minutes. We're just waiting so everyone has a chance to join. Let us know where you're joining from using the chat or the comments depending on what platform you're watching on and tell us something that you'd like to learn in today's code along. And just to note as well, we are using Data Workspace today, so if you don't have an account already, please make sure to go register for your free account. I've put a link to the code along in the chat in the comments, so go find that now. And we will be sending out the recording to everyone that's registered for the event and I will be posting a link so that you can register shortly. So give me one second, I'll make sure that's up before we get going. But yeah, we'll only be here for.
A couple of minutes.
Foreign.
And thank you for joining today's code along. My name is Rhys and I'm going to be your moderator for today's session. We're going to kick off in about 20 seconds or so. We've just been waiting so everyone has a chance to join. Let us know where you're joining from using the chat or the comments. And yeah, tell us something that you'd like to learn in today's webinar. We are sending out the recording and any resources to everyone that's registered for the event. I've posted a link to that in the in the chat, so go have a check of that and get registered so that we can send you all of the goodies after the session. We are going to also be using datacamp Workspace today, so if you don't have an account for Data Account Workspace, please get registered for one of those. I will be reposting the link so you can go along with us for, for today's session in the chat, in the comments shortly. But it is already in there, so go have a look for it if you haven't already.
Brilliant.
I think that's everything from me. So now I'll hand you over to your host for today's session, Richie. Richie, please take it away.
Hi there, Data Scamps and Data Champs, this is Richie. Now, one of the big promises of generative AI is that it's easier to create personalized content. And in a product context, what that means is that when you go shopping online, rather than seeing exactly the same page as everyone else for the thing that you're buying, you get a page that's tailored to you. And personalized content is something of a holy grail in retail. So every online store is either starting to think about how to adopt this or at least they ought to be thinking about it. And today we have three instructors from storybrain who have a platform for exactly this purpose, working with image and video data for products, and they're going to share some of their experience working with generative AI. So Jiku Jose is the CEO and he's a serial entrepreneur. So storybrain is his fourth startup and so he's been working with product and customer experience, the industries for the last 15 years. And Jibin Matthew is the CTO. So Jibin is the author of the book Pytorch Artificial Intelligence Fundamentals. And finally we have AMU Shehu, sorry, Amal Shehu, who is a senior engineer and also the third co founder of storybrain. And he's going to be taking you through the code along. So know that the flow today is going to be a little bit different to usual. So the second half is a bit tricky to make as A code along. So what we're going to do today is we're going to have a shorter code along to begin with and then you're going to get a bit of context and a demo after that. So with that, over to you, Jikku.
Hi, I believe everyone can hear me. Thanks a lot Richie for that nice intro. Hi. So I'm the CEO of storybrain and we're here today with some interesting content and this is something very in parallel to what we have been developing in storybrain for the last three years. When we started storybrain, generative AI wasn't even a term that was coined. And we started off to provide a solution that will enhance product pages. And I'm very happy to know that there is a lot of interest in this space right now. And we are taking one slice of what we are actually doing and trying to show that to the world so that if people are interested in something similar, there is a huge number of problems that can be solved using the current LLMs and all the data technologies that are happening right now, especially in regard to the LLMs that is powering solutions like ChatGPT and Verbal City and so on and so forth. So today, just to explain what we are going to do, we are taking a product detail page, that is the page in Big E commerce is like Flipkart, JCPenney, Amazon or any of them, and taking or extracting the entire reviews of the specific page. And then we are trying to figure out what exactly is the sentiment or what exactly is the pros and cons of that product from the perspective of real users posting those comments rather than the description that is provided. We are trying to actually break down exactly what people think of. So we are going to extract the page. The flow starts here. Amal and Jeevan is here to explain each step and why a framework like LangChain helps this process and makes it super easy so that anybody can who has a rough understanding of Python can actually jump in and try it out. And I'm very happy to say that the entire code and everything that will be shown will be shared to you. I believe you will all get in an email or after the session, you should be actually getting the entire thing. But this code along we'll try to make sure that you will be able to follow each step. Amal and Jibin will be able to explain each and every part of it. Feel free to ask us any questions. We would love to explain each step, but since it's a code along, I'm Telling you there could be some.
You.
Know, some issues in between, but forgive us for that. But anyway, we are trying our best to make it as smooth as possible. So with this I'm going to give the stage to Jibin and Abal. Please take over. Thank you.
Hi everyone.
Hi.
So this particular session, the entire webinar is being structured into two parts. So the first part we'll actually take a piece of text from a product detail page and try to extract critical information from that particular. So we'll kind of take in an unstructured piece of data and try to structure it into a JSON. So that's going to be the first part. That's where you people would be coding along with Amar and me. And the second part is an extension of this wherein it's a little more intense and we want to cover more topics so it won't be as easy for you to like go code along. So you'll be able to like follow the code, understand and try it for yourself. So with this I think we're good to start. Maybe Amal can start sharing his screen. I'll maybe give a brief overview about LangChain and why this is critical for what we are doing. So LLMs have been here for us, we've been interacting with ChatGPTs and other open source systems and systems that have been brought out by other platforms. So LLMs as such require a lot of fine tuned queries being made to it. It's like a humongous beast. For a normal engineer or programmer to deal with an LLM he needs to understand lot of different prompting techniques, lot of different ways to actually give an input to it, get out a particular input that is structured in a way that can be used in an application that in and itself relies on LLMs. So this has been very tricky for people. And also there has been multiple models that have been coming out lately and say you start with particular LLM and eventually you find out that there is a better LLM. Do you throw away the entire LLM and build a new system around it? That's not possible every time. And the third reason being that these LLMs, the sort of output that you might get many a times, are not predictable. So is there a way that we could structure it in such a way that the outputs from an LLMs can be guaranteed and be dealt with in the subsequent steps? There is also one more reason that with LangChain you are given the choice to actually use multiple LLMs in conjunction with one another. So there might be an LLM large language model that is very good at say writing an essay and a different LLM which is very good at writing code, and another LLM that is very good at math. So LangChain kind of gives you a piece of tool or an excellent tool to coordinate between all of these. Sorry, LangChain in itself is a marvelous combination of prompt engineering bundled into with excellent software engineering tactics. So I think this gives a brief for us to understand what LangChain is. And from here we'll start coding and explain more about the platform, about the framework as we go. So Amal. Yes.
Okay, so what we are going to do today is like we are going to extract some information from a product specification. See here we have a specification like some, some kind of a Description Text from MacBook Pro M2, how it works and all. It's a text can be reviewed, it can be description, anything. So our main point is the first we will set up our API key and all in Data Camp.
Maybe I'll run through the like once everybody has a sort of took the particular notebook at hand. So this is a notebook that you will be seeing. You should be seeing products spec extractions, notebook file pynv file which has. We have laid out the structure for this particular piece of code that we are going to tackle. So we'll start with firstly installing the libraries that is required. So first and foremost we'll install LangChain and OpenAI library that are within Python. So you could run the cell and then show them the output.
Okay, so here it's like now we have to add this OpenAI key. For that we have to use this python.env package because you guys can set it up, create a ENV file in the root and you can upload it. Then we can start quick addition.
If you are using the workspace within datacamp, you will be able to add that option from the left side panel that there is an environment option there. So you would be able to do that easily from there. Right now in this workspace we have installed LangChain, OpenAI and ENV just so that we able to read the ENV file. Environment file basically holds an OpenAI key which you can take out from by logging into the platform that OpenAI provides. So you get that key, make sure that you keep that key safe in an environment file and then from there you start the coding aspects of this.
Okay, Next thing we have to import required libraries for extracting text and all. LangChain has this specific function you can use, it's called a create Extraction chain. We use this for multiple purposes, like from extracting information from PDF, any kind of documents, any kind of text. We can use this predefined function like Create extraction chain. Then os we need to maybe.
I'll maybe give them a bit more context here. So LangChain has been structured into various. Several sub modules that cater to various requirements. So there is a module that deals with loading a lot of data documents that are out there in the world. So those are called document loaders. Basically that gives you an ability to kind of fetch data from varying different sources. This could be like starting from a simple text to crawling a URL, to getting a transcript from a YouTube video from Git book from n different sources. They have built a module that kind of takes in all of these adapters. They provide all these adapters so that you can load data from n different sources. So LLM as such is a system that perfectly works with a text sort of an input. So you might be having data in n different formats. You'll have to first bring it into a text sort of a format. So these document loaders kind of help us with that. There are several substructures. Another module that LangChain has is something that caters to chat, sort of an interface. So basically a back and forth much like char GPT out of the system can be built with LangChain. So that is the second aspect. So the particular module that we are importing here is from chat models. Chat OpenAI model is what we are using. So that kind of is very good at taking in an instruction and giving a structured sort of an output. For our requirement.
Here we are actually loading through this package called like env. You can also set it up because it's a shared environment in the code data camp code workspace. That's why I'm not using. You can actually go to this environment section and you can add your stuff here. Then you can directly. You just only have to use this osimport and OS get env variable, whatever you want. We will move to the next section. This section here we have a description.
This particular piece of text is provided in the document for you to actually look at. This has been kept here so that everybody gets sort of a similar response from an LLM. So we are kind of guaranteed that the output that you get is comparable to the other person that is running the same piece of code. You could actually move around or take in a different product description, plug it in here, modify it into a function if you want, and you will be able to easily get a similar sort of of structure every time you run it. So basically here we've taken a product in particular, it's the MacBook Pro M2 Max. We have an idea of the specification in that particular text. The relevant portions of what we are trying to extract is already there. So a typical use case for people would be from this piece of unstructured data we want to get say a structured JSON that we could subsequently use in the other parts of the program that we want to run. So this is where we'll first define a schema. So schema is basically we are trying to give it or we are trying to tell the LangChain that when you get an output from LLM, make sure that the particular output has these sort of keys and values within the results. So in case, for example, when the lang shape under the hood, say tries to send this data, this piece of text, work with the LLM and get back a particular result. Say the structure is not being maintained, say a required field is not being met. LangChain under the hood itself would do a retry for you and get back a response that is defined as per the schema that we have provided here. So this is where Amal is right now defining a schema for us. So basically here what he's trying to say is in this schema we want these properties, we want a name which should be of a type string. So our estimated or expected answer is something like MacBook Pro M2 or MacBook Pro M2 Mac, something of that sort. RAM is again a spec. We want to get it as a string and we're mentioning the type here.
Yeah, here we can get ROM then displays, there will be also string. Then we can expect cpu. Yeah. Okay, so I can put one more key here.
The property key has to be in quotes.
Oh, sure, yeah, yeah.
That will work.
Okay, okay, sorry.
Sorry.
Required.
So required you can. You'll be providing as an array wherein these are like the must fields within the response. So in case say one of these fields are not available, the the system should actually break so that it can or the LangChain can re prompt the LLM until it makes sure that these sort of key these keys are essentially present in the. So right now we have defined the structure. Basically there is two keys properties. These are the values that we want to extract. And required is a key where we kind of mention as an array of what all keys are like must for the output to be.
Let's move to this extraction process like here.
Now that we have defined the schema here. The next step for us is to actually prompt the LLM with LangChain under the hood. So what we would do here is we'll actually write this piece of code that will that where we will actually prompt, using the chat, sort of a system with the LLM within LangChain and then try to get a response from it. So here Amal is using chain. So chain is basically so within LangChain also there is a term chain associated with it and it's very critical in context to LangChain. So LangChain, if you just use a piece of function to provide one simple to solve one simple problem, it's going to be hard. And the sort of results or the achievements that you can obtain from such a thing is very limited. So LangChain kind of has a capability wherein you can combine these smaller piece of modules and build complex systems on top of it. So there would be certain systems that are exceptionally good at say performing a chart sort of a thing. There could be something else that could actually go and fetch data from a source place. So in this particular example that we are looking at, we want something, a particular chain or a particular utility that is exceptionally good at taking out the prompts, taking out the extracting out the information from a given piece of text. That's why we've used a create extraction chain, which is a chain that is available within LangChain, to take that particular piece of information and get the output in the schema that we have defined. It does two things. Takes in this text, extracts critical information and also make sure that it complies with the schema that we provide.
Yes, I'm going to execute.
This.
Is the output we are getting.
Yes. So here we've been able to like actually extract out the name. It has been, it has done the good job of actually taking into consideration the MacBook Pro M2 Max. It has been able to incorporate that. It has been able to take out the RAM information because we've given it as a string. It is able to fetch all those information. We could in fact add other information if we want and it will be able to get more information in line with the schema that we provide. So just if we look at the particular call that AMEL is making, you see a few parameters there, the temperature being set to zero, there is model information that is given. So basically what these things are, temperature is sort of like a dial that have with the LLM so that if it's dialed to zero, the randomness in the Output that you get is practically it's not there at all. And similarly, if you dial it to 1, you'll get completely random results every different other time. So depending on your use case, for example, say you want the model to be very creative, you kind of dial the temperature to somewhere between 0.5 to 0.7 or so. And similarly, in our case, we want the answer to be very precise. We don't want any sort of hallucinations or misrepresentation of data, because of which we have made sure that the temperature is set to zero, so there is no randomness in the output that we generate. Similarly, in the model information, you can provide the sort of model that you want to actually plug it into. For example, there are certain models that are exceptionally good at completing a given a piece of string. It is very good at completing that particular string. So if you give half a sentence, it will be able to complete the sentence. There are certain models that are very good at following instructions. There are certain models that are very tuned for back and forth sort of conversational system. So that's where you mention the model. And if that particular model is available within the offering of the underlying LLM, you'll be able to make a query and get back the data. So in this chain part, you're basically giving in the schema and the LLM respect that you have defined. And when you call the chain run with the description that is at hand, you get the output. In the second run that Amal did, you could see that Amal actually added something called review. So he wanted to extract a review about this particular piece of text in conjunction with the description given to the product. So here we see that that particular review has been added as part of the output that we.
Yeah.
I think with this we would be able to jump to the second part of the problem that we have at hand. Richie.
Actually, before we move on, there is a couple of questions from the audience and I actually have a few questions for you about this, because it does seem like this ability to take text data and extract useful bits of information into a structured format seems incredibly useful. First question for me is what happens if some of the bits of data that you want in the schema aren't present in the text?
So if it's not a required field, as such, those fields would be ignored. For example, if it's somebody ask at random about the camera and the camera information is not present in the particular description and they say it's not part of the required field, it'll be ignored.
Beautifully, we can specify that if that information is not present, just return minus one or a false value that we can specify in our prompt.
Okay, all right, that seems useful. And the second thing is I noticed that the schema is this sort of. It's all nested dictionaries, and that seems like a very sort of. It's like a web developer favorite structure, all the sort of nesting structures for data. Scientists like you quite often want things as a data frame or something. So is that an option or does it have to be.
Yes, there is something called Zod. Zode is also a structuring stuff. You can specify like GraphQL, kind of similar structures. You can specify.
If you want to get more fancy. Like if you are more from a Python background, what you could do is actually define a class and then define all these structures. So if someone is into extensive Python development, people would know something called a pydantic framework, wherein you define the structure of certain outputs. This is typically very popular with the API developers or backend system developers. So you could actually define a class and kind of move that class as part of the schema, and it will work just as fine as providing a dictionary for the schema.
Okay, so you can control the format of the output however you want in that case. All right, next. Next question comes from the audience. So Martin asks, is there a way to use the LLM? So in this case using GPT to define the schema? Because at the moment we sort of specified what the schema should be and then it extracted it. Right, but is there a way to just do a sort of a sensible option and get the LLM to tell.
Us what it should be? So if I understand the question right, it's basically, rather than me giving a schema, is there a way that we kind of get the LLM to answer us with the schema? Is that the question?
I think that is the question, yeah.
Wonderful. So basically that is something LLMs can do very well. In fact, given a data and you kind of prompt it saying that from this particular piece of text, provide me a structured way or a structured Python dictionary that I can subsequently use for a certain purpose given the prompt. And actually LangChain excels at something of this sort, wherein you could give a piece of text and say that I want you to give back a structure that can actually fit this particular data. And with that prompting technique, you'll be easily be able to get a structure out of.
Yeah, in this code we are actually giving something called schema. Schema. We are passing to this create extraction chain schema I am passing here if I'm not passing a schema instead of that I can use something called a prompt template. Prompt template. There I can define what are the variables going to get us input the type of output from the LLM. Then based on that basis you can actually query like chatgpt, like normal talking. You don't have to specify a schema here.
So actually the particular question that came up was is something of a meta to the actual processing. So you don't want to create a structure on the fly because that would break your interfaces or any subsequent application that might be using the data. So in order for you, the LLM can do it. It's just that you have to do it as a meta step before actually giving the data. So basically you get a structure first and then you use a second query or a second piece of chain to actually deal with your actual data. It's just a software engineering perspective is what I'm mentioning. Okay.
In fact I would like to add on to that question. In fact, it's a very interesting question. Jibin's last point I would like to emphasize he mentioned that it's a meta question. What that means is we asking LLM to generate a schema and then using that schema we are trying to ask the LLM to extract data that is a two step process or a second order problem. When you add in like that, the probabilities of an error can actually go up. It is better to split it into pieces in such a way that we have defined the schema very well so that we have a very clear test to validate the data that is coming out based on the structure that we have defined. So it is ideal that we define the structure, even though the other one is possible. It is better to always go this route. Just a few seconds from.
Okay, I think that's very comprehensively answered. Yeah. So probably you don't want the LLM to define a structure that's different to what's going on in the rest of your software. All right, before we move on, got another question from. This one comes from Yukian. So Yukian saying, can you just explain what the temperature variable was when you were calling the LLM? How does that affect the results?
Sure, sure, sure. So reiterating to what I mentioned prior. So basically in certain cases you want to have certain randomness in the system or sort of LLM being a bit more creative on every different run. For example, you are building a system that writes short essays or makes Short stories. So you basically want that given an input prompt, you kind of give out a piece of story that is suited to that prompt that is given and you want that system to actually give out a different or a slightly different output the next time you run it. Sort of like introducing randomness into the outputs of what the LLMs generate. But in here, this particular case, we have a given piece of data, we don't want any randomness in the output that we get. We want to be sure that if it's say 96GB of RAM, it's every time it's 96GB of RAM, it should not actually give say some other information like say the pixels of camera of this much when you're asking about ram. So these sort of creative or hallucination behaviors can be avoided by dialing the temperature down. So depending on your use case, typically from the sort of systems that we build, especially when the, the accuracy of the data is of utmost importance, you dial it down to as minimum as possible.
Excellent. So if you're having a conversation with GPT then maybe you want a bit of randomness to keep the conversation interesting, but in this context you want it completely reproducible, so temperature is zero. All right, super. So we've got one more audience question before we move on and I think this is going to be answered with the second part, but perhaps for Ranjit's sake, can you just give maybe a high level overview of how this fits into the retail context?
Yes, I'll probably take that question. So. Hi Ranjit, just to explain this, we have been actually doing a code along session that is going into a very small slice of what we are doing. But just to explain a simple example where this can be actually used, we can actually look into all the reviews that are posted in for a particular product or product category and understand what exactly is the specific segment of users looking for the product. For instance, in India, when you are actually posting an Android phone in the space from let's say $10,200 to $300, the most important aspect is probably the front facing camera because it's a selfie camera. So this sort of an insight we might know, we can actually guess, but we can actually bubble up this information precisely from the reviews. Like most people who will be actually reviewing might say, hey, the front camera is really bad or the battery is really poor and so on and so forth. Based on this we can actually create a better description that precisely answers what are the actual questions the person or the typical user is actually looking for. From the PDP and you can rehash the PDP to make sure that it is much more sharper to the specific audience group that you are actually catering to. So that's one of the things. But there are many, many layers where we can actually use this to optimize and sharpen are PDPs. Because if you think about it, it's the most valuable screen real estate in the Internet. So optimizing it so finely actually helps improve the top line. So just wanted to actually mention that it's very high level, but still I think it helps.
All right, super, thank you for that. And I think with that we can finish with the questions for now and carry on with the next step.
Wonderful. So now we'll move into the second part of the problem. Problem wherein it may. It would be actually more of a real world and actually dealing with far too complex sort of a data. So when, when you see like a description for a given product in an E commerce site, it's fairly structured, some person would have actually curated that content n different number of times and would have thought through the actual piece of text that is being put there and which is very critical for a particular product or being able to convey the information. But there are certain cases wherein you have to deal with user generated content. So that is where the sort of typical example is having to deal with reviews. Say you are E Commerce platform owner and say for a product that you have, you. You get hundreds of different reviews in a given day. This could be applicable to any product of any sort of a domain and you get say hundreds of difference of review and you want to say, make certain modifications to your product. How do you make decisions to this? So do you actually sit through, read all of these comments? Most of it might be repetitive. Many of this might have certain grammatical issues that you. You may find it hard to understand it. There may be a certain piece of code, that piece of text that is not relevant to the product at all. So basically what we are trying to do in this particular section of the webinar is we'll try to get what people say good about this particular product and what people say bad about this product. So pros and cons of a particular product, given the reviews from actual users, we could see a lot of application for this. Especially in the context of someone who's trying to buy this product could actually benefit from this. As a product owner, the person could benefit from this. And as a platform owner who is actually selling this particular product as a third party could also Benefit, whether in terms of making a decision whether to continue this product or delist it from the platform. So these sort of benefits are multifaceted. So Amal would be able to show us the code for this. So here we are taking Amazon as our E commerce example. We'll go to the review section of a product. It's a mobile product, mobile phone that we have at hand. Basically there are a lot of reviews for this particular phone. Generally Amazon does this trick of actually showing many E commerces actually show only the positive sides of the product towards the beginning and may have like a lesser popular or contradictory review somewhere in the hindsight or somewhere that people may not be. So we here have a system that could actually use lanship to actually go through, go to this particular URL, fetch the reviews from this particular page and come back with the piece of text, send it to the LLM and get the pros and cons. So maybe we could actually switch over to the code section here.
Okay, so first I will show something here. Jeevan, can you see the screen?
Yes.
It's explaining about Playwright. This is an agent toolkit from Play this launching. We have been using this scraping, all this. It's. We did industrial and scalable level of web scraping.
Right.
For that we have multiple choices before we use Request or Beautiful Soup. All these things actually all these tools were for extracting this static sites. Nowadays we have a lot of dynamic sites, server side rendered pages. So for that we need some advanced toolkits. So with this Playwright browser toolkits, it comes under Agents. Agents are like what you explain. Agents are like small, small programs which has. We can ask it to do perform some actions. Okay.
Okay, sure. So if you can show that heading, that would be helpful for. Can you scroll up a bit? Yeah, so play. Right, Browser toolkit. So this is like an offering from LangChain. So there are different parts to the title itself. There is Playwright, there is browser and toolkit component. So I'll go into each of these. The Playwright is something of a browser automation tool that we have. So people who have sort of a web background, people who have been doing scraping, would understand platforms like Selenium and Beautiful Soup and many other similar tools which actually is able to deal with the scraping problems that you typically have when building these sort of systems. So with the problem that we have at hand, we want to go to a particular URL, fetch that particular information that is in their page. So if you do a simple get request on that particular page, they'll be easily able to Identify that it's not actually a browser that is hitting their servers. It's much rather a simple query that is happening on their page to which they won't respond appropriately. So that's where we use something like a browser automation tool which actually emulates as if a browser is making the request. So here what happens is playwright would actually fetch the page, load it in the memory, and subsequently be able to show this particular or subsequently actually use this particular data to extract critical information. So Amal, if you can show us through the code. So the second aspect of it is Toolkit. So LangChain under the hood provides several of the several offerings that LangChain has, something called toolkit. Tools are very important and critical. So tools, what they do is basically these are small functions within LangChain wherein you can kind of give a definition to this particular function. Basically, in simple natural language, you could say that this particular function deals with say, getting a particular computation or say it would. The. It would be documented as if it is very good at extracting information from a web page. So this sort of meta information is given to a function such that it will be able to. The LLM would be able to actually make a decision on which tool to use depending on the task at hand. So you may have 10 different tools. One might be very good at extracting the weather, the other might be something else.
So Amal, can you share your screen.
And then we can.
Yes, you can you guys see my screen now? I already shared my screen. Is it possible?
No, we can't see your screen.
Okay, okay. Is it visible?
Yes.
Yes. All right, thank you.
Yes. Right, okay. I thought it was visible on the screen. That's why we are going to build a simple system I already made.
So basically, Playwright is basically a tool that actually a Playwright browser kit is a combination of a tool or defined as a tool which is very good at the LLM is given the information that this tool is very good at going to a page and fetching out the information from a web page. So this is what we are dealing with at the moment right now. So Amal, you could maybe go through the imports here and then.
Yes, so I added all the imports here. Then I have to use this load ENV and some other imports.
Just like the previous example, we want to load the environment files. As always, that's the first step. We start with methods just so that we are send information back in LLM which in this particular case is OpenAI.
Yeah, this we need for handling in jupyter notebooks or Whenever we need even loop in Python code. Then we will first define our this.
Particular piece of line that Amal showed here is useful for dealing with Async tasks at hand. So typically there is a lot of IO that is happening. So for blocking and you have to have this sort of loop that is running in the hindsight. So these sort of. This helps with that. Yeah.
First we will do all the setups for that. I will explain one by one. Here we will create simple browser using this function. We imported this create a sync playwright browser here. Then we will like under the hood there will be a beautiful soup things will be there. These are agents. So it will give us all the functions we need. From here we are loading the browser. Then from the toolkit we are actually extracting getting all the tools. This method will then after within the.
Main function, the first piece of line that you see an async browser, basically, like I mentioned, playwright is actually something that lets you have a headless browser. Basically you don't see the interface of the browser. It is something that is within the memory of the of the system that is running it. And basically a browser instance is created. It's as good as opening, double tapping a browser icon and opening that. So basically a memory a browser is instantiated and along with that. So people who have worked with these scraping systems, you know that systems like Beautiful Soup is required to actually fetch pieces of HTML tags or making filters from it so that you go to the particular HTML element to fetch the information. So these are all these things are abstracted within the PlayRoute browser toolkit. So with just one function call, you basically able to get all of these web page. Loading the web page using.
This navigate tool is the one I showed first here. This one here we have this cc this one. This will help us to navigate to any kind of URL. So we are loading our Amazon review. We have been checking just before this one and after that. So we will get this. Now what we have to do is we have to extract the reviews from the page. We are not we actually got the.
Full page, but we only need we have the page loaded and ready to go. Basically, you have defined all the tools that you've required. So basically the collection of tools that come along with player right to browser toolkit, you instantiated all of that. You made sure which URL has to be scraped and in this particular step the elements equal to await getelements toolkit. So that is a tool that is good at extracting a particular information from a particular HTML Tag So elements has information from the reviews that are out there.
And this is the template, chrome template I was talking about. Here we can specify what we want to do with the data extracted and the reviews or the structured data we already got from this page. Then I have to.
In detail with regards to what prompt template is. So people who have worked with ChatGPT would have an understanding that say you have have a task at hand. You'll basically ask say if you were to do the same task with Charge. So basically you have 10 different reviews and you give that to ChatGPT and you would say, can you summarize this for me? You could do this once, you could do this twice, say 10 times. But what if when you're dealing with such a thing on a day to day basis, so you want to have certain system that kind of does this prompting for you automatically. So LangChain is a tool that would let us do that. Basically what happens here is that you define a template here that for whatever comes into this particular piece of template fit in the incoming text along with the prompt template that you have at hand. So dynamically it will fit fill in the sections of this particular sentence or the prompt and make a call for you to the LLM.
Okay, now we are creating another variable called prompt here. This is the prompt template we exported from this LangChain prompt. This prompt templates are extremely useful to compose what are the variables we are going to use from the incoming data? Input variables. And whenever you're mentioning an input variable here that should be specified here, this this will be taken to the context.
Many times you will have dynamic portions of prompt within that particular template. So when you define a prompt template from the LangChain model, basically you give two things. The template on, on which the the dynamic variables have to run and the input variables itself along with that. In this example we don't have it, but you could also specify the output formats also within this particular module itself. So just like the last time, the previous example that we saw, we now create a LLM or we instantiate an instance of LLM so that we are now ready to make an API call to OpenAI. So this is particularly for OpenAI's chat sort of model. And that's why we have an LLM instantiated for that. Just like the last time, we don't want any hallucinations or any random things happening in the output that we have. So we have set the temperature to zero. We'll be using the 3.5 turbo model.
That OpenAI offers yeah, so I will give a recap. First we created, imported all the necessary things and we are using load ENV to get the env files. Then this next underscore singio will handle this event loop happening inside because this is an asynchronous function. Then we created browser. Then we loaded our URL. From that we extracted elements which contains review of with people, name, time, all those things. Then after that we created a prompt template to format whatever we want. Then we created initialize this chat, open a for that. After that.
Just go back a little. I will just back up a bit. Yeah. So on line number 41, wherein the template is prompt, underscore template template and then you define a string. At that point it's just a piece of string in itself. But at the point where we actually use prompt template module within the LangChain so it has a different representation to itself. Wherein it now knows that it is a template. This is the place where you have to fit in the dynamic pieces of the prompt. And also you define, okay, these are the things that you have has to go in within the prompt. So these are the two components to it. Along with that there is also another parameter that we have not shown here, but something that you could adjust which kind of dictates what the output also is. So basically the prompt template kind of fits in these when it's actually run, it fits in these templates, places or the placeholders, the actual values so that the the template or the prompt in the end looks something like this. So from the reviews that are in here, basically show me what all are the positive and the negative reviews or pros and cons of this particular product. So it will fit in all those pieces and send it out for you.
Yeah, yeah. Now we are creating a chain. Chain is the like fundamental stuff of like we can connect multiple things, prompts, LLMs, formatting stuffs, these kinds of things. Now we are creating an LLM. The input is like 2 PER. We have two inputs. One is the LLM. We already initialized the chat, we can change it. We can use any kind of model as LLM even. We can use llama 2 for formatting things, any kind of models we can use. And the prompt, the formatted prompt here. Then here I'm printing the reviews. The chain road run actually prints the final step.
Of actually fitting in the dynamic pieces of the prompt. And also the API call to the opening API OpenAI system happens at this stage when you make a call to the change.run method.
Okay, well, let's Try to run it. Let's see. It's taking some time. Yes, we got the output. So this is the output.
So Amal, we could actually show them the reviews also that went in so that it gives them a little more of context. Do you want to print that along?
Yeah, print the reviews.
Yeah. Basically what went in. Maybe show them the browser. Browser.
Yeah, I think I can show those browser. Because the data we have like in the reviews.
Right.
This is not structured.
We could show them the elements variable here.
I can show it side by side. Yes, this is fine, right?
Yes, yes. Actually this is the output that we got. So what you see on the left hand side, those were the actual reviews that people posted for this particular product in Amazon that was extracted and sent to the LLM. And now on the right hand side you see pros and cons of the product from the reviews itself. So you will be able to actually match which all portions kind of contributed to the pros and cons that you see from the LLM output. You can search for portions of.
We can try another product if you want to.
That also we can do. We can in fact show them the portion where the elements are fetched. Go back to the code.
Yes, yes, yes, yes. Here, this LCD screen. Let's see for search for LCD screens that will be like Screen. Yeah.
Yeah.
Here we have a review about screen. It's an LCD screen and see the. Yeah, the quality. But it's not spectacular. It's here. This is it. Yeah.
Maybe highlight it. I think this. It's a little hard for people to read because it's far zoomed out. Maybe highlight it so that it's easy for us to see.
Oh, oh, here, here. Which one is this visible? This output?
It's a bit hard to read because it's like too for us at the moment.
So I will increase the phone size a little bit. Fine.
Yes. Yeah, it's better. Maybe highlight the actual review and the. The output from the LLM so that it's comparable.
Yeah. Here we have this.
Screen.
Yes. See here an LED screen. This year we got this extraction here. Yeah. Screen is not vibrant as higher end models.
See. Yeah. Maybe highlight the same in the review also so that it seems easier for people to follow which portion.
Yeah, yeah. We actually condensed this much text into something valuable and it's easy to understand what's happening.
Exactly. So in fact if we can go back to the code, maybe we'll. We can play around a bit and actually see.
Yeah, yeah.
Can you enlarge.
Yes.
Maybe cut down on the output. Yeah. So at this stage where we have the elements. Can you. Elements. This one, I think it's a bit zoomed out.
Right, Right now. Okay, this is fine.
The line number. Yeah, after line number 42, maybe print elements so that people can see what the actual content went in.
Oh, okay, okay, okay, okay. I can. I can print it here.
Where is it?
Oh, yeah. So now I'm going to print. See, this is the input we got from. We extracted from the page.
This is a humongous piece of text from the page was extracted. If you look at it in detail, you will actually be able to see the actual reviews from the page that has been extracted here using playwright and the element. And then this long piece of text actually went into the LLM using the chain run that we called. And then LLM did its magic to kind of condense them into prompts and pros and cons. So this is essentially what we could achieve from something like a prompt template, sort of a system wherein you can define a piece of prompt. You could run the same thing for a different product detail page as well. So if you want to change, say the URL and try it with a different URL, it will just work as seamlessly as this particular page.
Yes.
Maybe scroll down and show them the pros and cons. Yeah.
Yeah.
In fact, modify the prompt itself from. Instead of saying pros and cons, you could actually say a summary of all reviews and it'll give out a summary of all reviews. So these sort of modifications are actually possible with LLMs. Maybe try it with a different URL that might help them understand some. Some more con or get. Give them more context into this.
So we will go for like asking. Summary of the product.
Sure.
Okay, so we can. Here we can check some other products. Mobile. So yeah, there is another phone. We will get the reviews from. So we changed to another mobile phone. Now I am running it. So basically here, can I hide the results? Why? Because it.
It will.
Can I hide the elements?
Sure.
Do you want to see element?
Maybe hide that too long for people to.
Okay, so I'm running it. Let's see. Yeah, now we got a summary.
So now we've actually modified the prompt. So this time we wanted a summary instead of getting a pros and cons sort of a thing. So basically here we have a summary of all the reviews that were there. It has been condensed out into a simple paragraph that anybody owning the platform, any stakeholder in this entire equation could actually look at and get a better understanding of the product as such. So I think you could play around with this particular piece of code. We'll be able to share it with you via GitHub and coordinate further on this. In fact, the beautiful part of using LangChain here is that you kind of have a way to abstract out which LLM you're using, how the prompts have to be structured. In fact, there are many systems that are inbuilt within. For example, there are modules for summarization that actually get you excellent summaries without you actually having to write a prompt for it. These sort of high level things.
You remember that, you remember that method, inbuilt method to summarize.
There is a module for summarization. So you'll be actually be able to go through the documentation and get a sense of what all LangChain offers. So there is a way to load data, there is a way to summarize it. So here is one wherein you don't actually have to write the code to summarize or the prompt to summarize it. It'll basically internally generate that prompt for you and make a call to LLM and get you a summary of the entire thing.
Yeah, can you, can you talk about the QA also?
So there is another module within LangChain that kind of deals with the QA part. So say you have all these reports, reviews that we have saw right now, basically with QA module, what you could do is you could ask questions just like ChatGPT saying that, okay, tell me the product, tell me what spoke, what people spoke about this product in regards to the camera of the particular phone or say something that people like the most about this product. These sort of question answering systems could be implemented with QA module that we have. In fact, because of the time constraints, I think we've already crossed the time for this particular webinar. But there are many other systems that we have not been in fact able to touch. Systems like using document mechanisms or there are vector databases, vector stores that we could actually cover in the coming sessions that we have. So I think this is like a good point for us to conclude this webinar right now.
Thanks Jibin. And also Amal. There are a few more questions. Ruchi, I'm not sure whether we have gone past the time, so absolutely, yeah.
Thank you to all three of you. That was a really nice demonstration. We are past time though, so we're going to run a little bit over. For anyone who can, who has to jump, please catch up the the rest of the Q and A On the recording. Before we go to audience questions, just to let you know, tomorrow we've got a webinar on data science in the energy industry. So if you're interested in learning about careers in the energy industry, then please do join us. That's Wednesday. On Thursday we've got a session on getting return on investment from AI. So if you want to know how to make sure your business is making good decisions around investing in AI, then please join us on Thursday. And then next Tuesday we've got a session on using datacamp Workspace to build a career portfolio. So please do register for that. Go to datacamp.comwebinars to join to register for any of those. All right, with that, we'll go through the last few audience questions. We'll just spend a couple of minutes on these. So this question comes from Supreet saying with the current use case, for example, if you want to identify which phone is the best by passing data to the LLM, how can it accurately come up with some sort of answer?
Okay, probably I'll. Okay, yeah, you can. It's an interesting question. We had actually pondered about this in the team and interestingly, I think Amal recently got a phone and he tried to do this also, I think. So. Interestingly. See, this is basically an aggregate question and it's also subjective. Best phone is different for different people. So the ideal question is probably to get a better variable set from a set of phones. Rather than actually going with a single answer, it's better to actually say, hey, I have a requirement for a great camera, great battery, and also great display. And my percentage of weightage for each of them is X, Y and Z. How will you rank it based out of that? Something like that might be a better way for you to analyze it rather than asking, you know, LLM, what is the best? And as you know, if you're using something like chat GPT, the information is cut off from only till September 21st. Yeah. So basically data will not be actually present. So this is not an ideal case for this. But if you want to analyze, let's say a selection of phones, let's say 15 or 20 phones, this is ideal. You can actually set up a very simple notebook and try to do this. That's awesome.
Imagine if you're kind of confused and want to understand whether it fits your need actually re engineer the prompt a bit so that you only look at. So right now in this example, you saw pros across different features of the phone. You could actually tone it down to say, get the pros and cons of just the camera of the phone, which is probably something that you. You can kind of narrow it down across multiple products and then make a decision for yourself.
And we can actually add multiple URLs and we can fetch it concurrently. We can compare 10 different phones and make a prompt which. Which actually scans all the phones and which will return the best phone according to the spec a person wants. It's easy to build just some tweaks in this example we actually should. We will share the GitHub link. You guys can try to.
As a starting point.
Since you said it's easy to build. There is a related question here that's asking you exactly that. I think so. Khandeep was asking just if you've got multiple products or multiple categories of products, then how do you go at figuring out the schema? Basically how do you expand this beyond. We're looking at a single product and just a few reviews. How do you generalize?
So if I'm understanding it right, we want to have some sort of a schema method wherein across different products it has to vary. Is that as the question?
I think so, yeah.
I think he's looking for a schema that is general enough for extracting information from different categories and different e commerces. If that is one of. I believe, if we understood the question right. It's a very interesting question. The way we actually, when we were actually trying to prototype this solution, there is a very interesting process that we did and it worked really well. What we started off with was we started created an initial version of a schema and we just kept asking the LLM, can you improve this based on let us say different. Different dimensions maybe how to make it general for n number of products, how to actually make it so that these four details can be actually extracted easily and so on and so forth. Or simplify it. Deduplication and so on and so forth. So slowly in step by step, the version actually started improving better and better. Schema became sharper and more better. It's basically a process wherein we are actually improving constantly with the LLM. That actually worked really well for us. So that is an interesting process. But as Jeevin and Amal had mentioned several times, this is basically prompting is basically something where we need to do it over and over again to get it right.
So it's basically trial and error and you have to.
It's a trial and error at the end of the day.
And especially with the sort of use case that has been asked here, you can't expect a Swiss knife to actually solve all these problems in one go. So you actually have subsystems to kind of set this in place or just.
To clarify, we may not have a Swiss knife right now. Maybe with 5 or 6 of GPT, we might have a Swiss knife. We don't know.
But then probably by then we won't be needed.
So they will not need us.
All right, so it sounds like the answer is it's a bit of trial and error or I guess if we do it software developer language, what we call it agile development. Right.
Okay.
Wonderful with that. Thank you, Jiku. Thank you, Jimin. Thank you, Amalia. Really great session. It's such an important use case. This is like, this is huge. Like, every retailer needs to be thinking about this. So this is really important stuff. Thank you for sharing your insights. Thank you to Rhys for moderating. Thank you to everyone who asked a question. Thank you to everyone who showed up today. I do hope to see you all again in future webinars. Please do, do go to datacamp.comwebinars to register and I will see you again in future sessions. All right, thank you, everyone.
Bye.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







