friends creating knowledge graphs is becoming easier and easier with the advancements in llms and llm Frameworks a couple of weeks ago Lang chain has introduced new functionality to create knowledge graphs from raw text so in this video we will see how to do that all right so let's do a bunch of installs uh related to Lang chain Lang chain Community experimental and and also open AI support uh we also need neo4j desktop uh to for graph database and neo4j uh SDK uh client all right so the main modules we are going to use are this llm graph Transformer uh this is a new functionality introduced just a couple of weeks ago by Lang chain community so whenever Lang chain in community developed new features they will be part of the Lang chain experimental okay and and we will be using neo4j graph uh for loading the knowledge graph into the graph database and open AI uh but we can use any uh open source llms uh for example Lama to mistal Etc again using uh this olama wrapper from Lang chain okay I already have my open a API key in my environmental variables so I don't need to set it explicitly but here I'm defining my llm so it's gp4 now temperature zero uh this is very important because here we are extracting factual information from a given text we are not doing any creative writing uh uh so that's why we need to set the temperature to zero whenever we create any knowledge graphs using any llms okay all right so instantiate an llm and then uh provide it to this llm graph Transformer okay so as I mentioned uh this is the functionality we are going to use uh to create knowledge graphs and then uh these are neo4j uh local uh database credentials these are the default ones you don't need to change anything uh but to be able to use this uh you need to install neo4j desktop client and also create a database which I will show you in a minute and you have to run the database so that it's active uh so that we can uh add the graph data into the graph database okay so here we have a small piece of text uh it's about uh the physicist or scientist Mary cury uh it's about uh the areas she worked on the prizes she has won and uh it also talks about her husband uh who also won uh uh or shared a Nobel Prize with her and where she worked it okay uh some it's a text but with very rich information all right then uh so using documents uh again Lang chain document uh we create the document we create the document and simply call this llm Transformer which is coming from this llm graph Transformer okay and just simply Supply the documents okay uh this Transformer it already connected to the uh llm right so that's all we need to do so it will return uh uh it will return return a result which contain both the notes and the relationships so because we have used only one document so this is our first document and these are all the notes from the first document so we are going over all the nodes and printing the node information so this will have the ID which is actually the node uh content itself and it will also uh uh have the type right for example here Mary cury it's of type person and then uh she is Polish uh this is of type nationality right and similarly here uh the chemist uh this is an occupation and Nobel Prize this is an award and uh per cury uh it's her husband and this is also person and then here we have a university name which is a organization type right so we simply need to Define our text documents and we'll use this llm graph Transformer to which we need to supply an llm it can be open AI or it can be any open source llm and that's it really so just Supply your documents and we can get the notes and then we are doing the same thing again we have only one document that's why this index is zero and then here we are printing the relationships now the relationships it connect two nodes right a source node and a Target node so that's why for every relationship we see three pieces of information so the first one it is connecting the source note Mery cury and it again display the type but the source node ID is enough so it's person maryury and then uh this target is uh Target value is Polish and the relationship is nationality so Mary cury nationality is Polish right that's how uh uh we see it and for example let's look at one more so this is her husband and here uh the target is uh Nobel Prize and the relationship is award one okay so we have extracted uh the nodes and the relationship and now we simply load them into uh neo4j database simply using this uh ad graph documents okay now um so when you install Neo 4J client uh this is how it looks like so first you need to create a new project just give it a name uh uh uh standard information and then I'm going back uh one second so yeah so these are all the projects I have created and I'm Lo and make one of the projects active meaning for example currently I have created this new database called uh new project called Lang chain and then it is act AC so you see this active here right so when you have a database you need to make it active so that it can start accepting the data and you can visualize it locally right once you start the database then click on this open and neo4j browser okay so Neo 4J browser is uh is this okay so from Neo 4J client we are opening NE 4J browser where we can visualize the data right so this is what we have uh uh uh the graph Transformer it has uh extracted these nine notes and here you can see those notes and also the relationship so the central one is this uh merury now again uh it should have displayed here but because uh it could not fit into it uh it's not there but the central note this is maryury and then here you can see all the relationships right for example award one um uh she's a professor at University of Paris her nationality uh occupation uh so uh occupation so she is both a chemist and physicist right uh and then here uh we have her husband and they both share uh one of the Nobel Prize she won another Nobel Prize herself but they both share uh this uh uh one Nobel Prize now as you can see even though these two people are connected uh because they are spous to each other thus relationship is not being uh automatically uh extracted by the uh graph Transformer right so in such a case what we can do is so when we are defining this graph Transformer we can Define what sort of nodes we would like to have and what sort of relationships we would like to have so maybe when you start a new project we can leave it this open ended so that we can see what sort of notes and relation ships are coming up and then we can decide which ones are important to us which one we can ignore and which ones are missing so that we can uh add them to the list uh when we create uh the second version of uh graph uh entities right so here we are explicitly saying hey I am interested in only these three types of notes right person country and organization so whereas if you look at here we have person uh um uh the name nationality the research area the awards uh quite a few right so here we are saying the note type must be one of these three only and then the allowed relationships are nationality located in work Deb and spouse so this time we are explicitly asking if there are any spouse relationships in the given text do extract those relationships okay and this time we are storing the results into this llm uh Transformer filter sorry we are uh instantiating this llm Transformer and then we are uh supplying the same documents uh and if you look at the notes this time we have fewer notes right and all these notes must belong into one of these three types so the person country organization only these three now if you look at the relationship we should have a new relationship which is this spouse um where is it yeah so here we can see the source is merury the target is peric cury and the relationship is a spouse okay and we can load this data so let's do one thing so we'll delete all these data um have here yeah so here we are matching uh so this is a a cipher syntax uh which is somewhat similar to uh SQL but for graph databases so here match me is something like select all so we are selecting all the notes all the nine notes and we are deleting all of them so let me delete I have deleted all of them so if I do this one second yeah no records and then if I run this query so it should not return anything so this time here we are returning maximum 25 notes okay if there was anything it should have been displayed here okay so we have deleted it now let's add this data to the database so we have added and let's rerun this query uh so this time as you can see we have fewer notes and uh fewer relationships one of them is this spouse so this is maryury and this is peric cury and the relationship is p so this way uh we can control what sort of nodes we would like to have and what sort of relationships we would like to have so as I mentioned uh it's becoming easier and easier uh earlier whenever we have a piece of text we had to do lot of text processing right we used to extract uh the entities uh the the relationship text cleaning uh and D duplication uh so so and so forth right but it is becoming easier both with Frameworks like Lang chain as well as the advanced in llms uh that's pretty much for this video uh thank you very much