Hello guys.
So we are going to continue our discussion with respect to graph database with Liang Chen.
Already in our previous video, we saw that how we can quickly generate a simple Q&A, uh, where we
are specifically chatting with the graph database itself.
Here, the concept was very simple.
With the help of LMS, we were creating our entire queries, right Cypher queries, and based on that,
we were executing it and we were retrieving the results from the graph database.
But understand, uh, there may be scenarios where, uh, the LM may not perform well, you know, with
respect to different, different complex kind of queries.
So it is better that, uh, we try to improve this graph database query generation mechanism.
Right.
Now here, uh, what we will be doing is that we will go with some prompting strategies, you know,
uh, which will try to improve the graph database query generation.
So we will largely focus on methods for getting relevant database specific information in your prompt.
Okay.
So for that, uh, I will just go ahead and quickly, let's say that I will go ahead and create one
more file.
And here I will just go ahead and write prompt strategies right Ipynb file.
So here we are going to specifically go over here.
Let me go ahead and select my kernel okay.
Now as usual uh this will be very much common.
So here I'm going to use the same import OS.
I will copy and paste it over here.
I could had probably done in the same notebook.
But let's go and do this.
So let's delete this.
Let's create a code over here.
Let's paste it.
And again I'll paste this one.
And similarly I will go ahead and set up my environment right over here right now.
The next step uh quickly what we are basically going to do again you remember this is the graph queries.
Uh, if you probably see we have already uploaded this entire graph.
Right.
Uh, just by executing this graph dot query.
So we know that this data is already present.
Okay.
Now, uh, what we are going to do over here is that we are going to develop some prompting strategies.
Now, before that, I go ahead and develop this prompting strategies.
Let's go ahead and quickly include all the LM models that we have.
So here are this line of code that I'm just going to copy it and paste it over here so that I will be
able to access my LM models.
Right.
So let's quickly do this okay.
So I have my LM model I'll go ahead and execute this okay.
This we really need to go ahead and do it okay.
Now uh, you will be seeing that uh, we will try to, you know, use some kind of prompting strategies.
Now, what are the kind of prompting strategies that we are going to use?
We'll just have a go.
Go ahead and have a look.
Okay.
Now, uh, let me do one thing.
Let me also use this cipher key chain.
Okay.
So I will use this control C control v okay.
And here I'm just going to create this specific chain.
Let's go ahead and create this uh generate cipher QA chain.
Uh sorry generate cipher Q and chain from underscore lm okay.
And here, uh, the first parameter is nothing but graph.
I will be using the same graph.
Um, see, I, I also have.
So here I'll also be giving my which lm model I'm actually going to use.
Uh this also has a parameter which is called uh which is called as exclude types.
Let's say you want to specifically exclude a kind of field and do not want to search based on that.
Let's say I'm going to use genre, right.
So I'll be using this and I'm telling basically do the all the searches by excluding this particular
field.
And I'll keep the verbose is equal to true okay.
So once I go ahead and execute this will basically be my chain.
So graph is not defined because I have not created the graph obviously for good.
So good reasons because I have not uh, inserted any data.
Right.
So I will try to just insert this entire data itself.
Right.
So here, uh, again, we will go ahead and use the graph.
Okay.
Perfect.
Over here.
And again if we just go ahead and write graph dot query with movie query we will be able to insert the
elements.
But it is just going to probably okay.
I have to also initialize this graph right.
So if you remember where is the graph graph graph graph graph graph dot query.
So graph should be on the top.
Here is my graph okay.
So I will just go ahead and quickly initialize this so that it will be able to communicate with the
database okay.
So this is my graph.
I'm just trying to connect it.
And then we will go ahead and execute this movie's query.
It is just trying to insert again Okay.
You can probably go ahead and see this right.
All the steps are same.
Nothing is different.
We are doing the same thing over here with respect to this okay.
Perfect.
Now let's execute this.
So this is my graph Cypher key chain.
And I have all these things right.
You can also go ahead and see the entire chain dot schema okay.
If you want if you are interested.
And you'll be able to see, hey, what kind of schema it is basically holding.
Right.
And uh, if you want the entire graph schema that also you will be able to execute, execute it.
So here you can see node properties are the following um CEO this this this are there.
And what all nodes like what all relationships everything will be able to see it over here okay.
Now let's include some of the natural language questions.
and we'll try to convert that into a valid Cypher queries.
Okay.
So obviously with respect to prompting strategies we'll give some of the examples.
Now let's take this some of the examples.
Now this is a very big list okay.
Let's say one of the question is like how many artists are there okay.
So we are matching a with person acted in movie and we are returning this count distinct a when I say
artist, I'm specifically talking about actors.
Okay then which actors played in the movie casino?
Right.
So here you if you see.
Right.
Uh, if I just go ahead and probably in our previous one, if I just go ahead and ask the same question.
Okay.
I'll say the same question over here.
How many artists are there?
Okay.
So here you'll be able to see what kind of answer we will be getting.
So if I just go ahead and execute it here you can see match p person count P as artist.
So 1240 artist.
But when we talk about artist we are specifically talking about actors.
Right.
So here we are just seeing that hey which all person has acted in the movie.
So whenever we try to give this kind of queries, obviously we are not going to get that exact answer.
What we are looking at based on the natural language processing Text.
Right.
So here what we are going to do is that we are going to define some prompting strategies where I will
be writing in the form of question and queries.
So here you can see how many artists are there.
This is the query.
Then there is another question which says which act is played in the movie casino.
Right.
So we have matched that with movie title.
Title is equal to casino acted in a return a dot name.
So we are giving some examples over here with respect to different different questions and cypher queries.
So how many movie has Tom Hanks acted in.
So let's try to experiment with this also okay so this is one I'll copy this.
We are just using the previous strategies over here directly by using the LM model.
How we will be able to get the response.
Let's see this okay.
So if I just go ahead and execute it and let's see the response.
So acted in movie it shows three.
Right.
And obviously the result may be right because the LM is able to understand.
But by using this prompting strategies, the LM model will completely get an idea like whenever we are
asking this kind of questions.
Right?
So here also it shows list all the genres of the movie Schindler's List, right.
And here you have this entire query.
Again you do not run.
You do not understand.
If you are not able to understand this particular query, it is fine.
So you can at the end of the day, your model is generating.
This is what we have actually created, uh, with respect to the sample queries.
So you can go ahead and have a look with respect to all the other queries, which directors have made
movies with at least three different actors name.
Uh, John.
Right.
So this kind of queries are complex queries.
So we are trying to match this director with this particular actor in this, and we are trying to match
with the person where a dot name starts with John, with d count distinct as a John counts, where John
counts is greater than or equal to three.
Right.
So this kind of complex queries, we are trying to just make a list of it, okay.
That is the pure idea about it right by using this prompt strategies.
So once we go ahead and execute it.
So this has got executed.
Now we are what we are going to do is that in long chain itself.
So if I go ahead and write from long chain, long chain underscore core dot prompts.
Right.
I'm just going to import few short prompt template okay.
And then we are just going to have this prompt template because I have to probably create my prompt
template also.
So here I will say hey this is my example prompt.
And let's go ahead and define my prompt template.
And here I'm just going to use dot from underscore templates.
Okay.
And here we are basically going to use this important things.
One is user input okay.
And then we are also going to have this entire question.
Okay.
And I'm going to say hey um by just giving this question I should be getting my Cypher query.
And this we will give another input parameter, which is called as query.
Okay.
Now see this is fine right.
You have an example prompt template is over there.
Now why this few short prompt template will be useful.
So now let me just go ahead and create my prompt.
I will go ahead and use this few, uh, short prompt template.
And inside this let me go ahead and give some examples.
So here I will see saying hey examples is equal to example examples of let's say I will go ahead and
take some five examples.
The top five examples okay.
From here not all, but I'll give some sample of five examples.
Then I will say hey use this example prompt okay.
And this will basically be my example prompt okay.
So here what we are doing is that inside this prompt template from our prompt template I'm giving this
user input questions over here.
Right.
So here you can see that I'm giving some examples over here.
And I'm also providing this particular example prompt okay.
Now see what will happen.
It will you'll be able to understand I will create a prefix okay.
The prefix will say what what the LM model has to do.
So I'll say hey, you are a neo four J expert.
Okay, then I'll say given an input question, create a syntax syntactically.
Very accurate.
Cypher query okay, so So I'm just going to write Cypher query.
So once I probably create this.
So this basically becomes my prefix right.
And now here itself with respect to the suffix.
So there is also one more parameter that we will be using.
I will just provide this.
Hey this is the user input.
This is the question.
This is Cypher query that we are getting right.
This query needs to be supplied right.
And with respect to this, you know that the input variables.
What are the input variables I'm using over here?
One I can definitely see something called as question okay.
The question.
And second we will also be giving schema.
Now why do you think we have to give schema over here.
Let's execute this okay.
So let's make a new code cell and let's see the prompt.
So now I will just go ahead and execute this.
Let's see the prompt.
So now with respect to the prompt you can see input variable.
Is this how many artists are there.
See this is the list of things it has made.
So few shot template prompt template.
When we give the specific examples you have considered that.
How many examples is there?
The top five examples, right?
Right.
Then prefix is basically added okay.
And then suffix is also added.
Right.
So give you are a given an uh syntax I have to probably use the spelling correct syntax.
Uh syntactically.
Okay.
Perfect.
Very accurate Cypher query.
So this is your prefix.
Uh, this is your suffix.
Suffix is also added.
Uh, and here you can see that your question is going right.
All the other information is specifically going.
So this looks very good right.
It's, it's it's perfect mostly.
Now let me do this thing okay.
I will just go ahead and ask this particular question.
And here I'm giving some kind of schema okay.
Schema like foo okay.
Just take as an example here I'm actually going to give the schema.
So it is first of all going to give me a error saying that key error questions okay.
So questions is there a question is there.
Let's see.
Print format question.
How many artists are there.
Questions okay.
This should be question okay.
Perfect.
Now let's execute this.
So here you can see you are a new forge expert.
These are my user inputs.
Everything is visible over here.
How many artists are there?
And here you are specifically getting the Cypher query, right.
So all this information, you are able to see it right.
And through this, this is your entire information with respect to the, uh, things that we have given
over here in a few shot prompt template.
But now it's time that let's go ahead and execute this and see.
And then you will be able to understand that how this will specifically work with graph Cypher.
Shane.
Okay.
Now my LM model is obviously there.
Right.
So you have your LM model.
So my LM model is nothing but this one.
Let's see.
Okay.
Now at least we should be able to get a good accuracy.
So this is my linear model okay I will go ahead and create my chain okay.
Now chain will be nothing but graph Cypher key chain.
Here I will say hey dot from underscore LMS.
The reason why I'm initializing this because my prompt has been changed now.
So I will go ahead and write.
Graph is equal to graph LM is equal to lm.
And now I'm going to use my cipher prompt.
See before inside this chain when we created this chain we did not give any prompt.
But now we have to give some cipher prompt.
Now cipher prompt is nothing.
But we will give this particular prompt, which is my few short prompt template.
This just to give an LM some ideas about how kind of what kind of questions we are asking and what kind
of cipher queries been generated.
Okay.
And then finally I will go ahead and write verbose is equal to true okay.
So once I execute this.
So once we have executed this chain we have our chain ready.
Now we will just go ahead and take some of the examples okay.
Let's say I'll go ahead and write list all the genres of the movie.
So and so.
And I've just taken the top five queries.
You can take any number of queries in your few shot prompt template.
Okay.
So I'll just go ahead and write chain dot invoke give this entire statement and it says uh of M this
match is not happening.
Let's see why this match is not happening.
Okay, I will execute this.
Okay.
Signed Schindler's list.
Okay.
Schindler's List, if I remove this particular list.
Okay.
There is no matching records.
Let's see.
We are not able to get any information.
Uh, but I will try like this.
How many artists are there?
Which actors played in the movie casino.
Okay, let's try this one and we'll see whether we are able to get some kind of response or not.
So here I'll go ahead and execute it.
So here it shows Robert De Niro Joe Pesci this is coming as right.
Okay.
Let's see some more examples.
Um, how many movies Tom Hanks acted in?
Okay.
So I will just go ahead and search for it and paste it over here.
Chain dot invoke and I will write this particular query.
So here you can see count is three.
But I don't know the answer why Why.
Oh return em.
So count is three over here.
This query is basically getting matched.
Let's execute this one.
Here you can see the count is three okay.
But when we try to ask this in our prompt template what was the answer that we are getting.
Okay.
So here also the same type of query is generated okay.
So let me go ahead and execute this uh match.
Okay.
This query is wrong.
That is the problem.
Right?
Uh, this was the problem.
So I'll go ahead and execute this.
Uh.
Remember this?
Okay.
This was the right thing, but you can see, right.
It is been able to generate perfectly over here.
So I will just remove this.
Okay.
Double double titles were used over here.
I'll just go ahead and use this.
Like this.
Yeah.
Perfect.
But, uh, we need to think over this.
Why?
This had actually come as count of M is equal to three.
Okay.
How many movies acted in?
Tell me the number.
Okay, I'll just go ahead and execute this.
Let's see.
I don't know the answer, but it is able to provide the full context.
Okay.
We really need to see to this why these things are happening.
Okay, but let's try to ask one more question.
Um.
Uh, provide, uh, tell about the actors or provide display the actors.
Who acted in multiple movies?
I think now it should be able to generate this.
Okay.
Where size current generated statement is not valid.
Okay.
Cipher.
Let's see what query this is.
Again these are the problems that usually happens right.
And here you can actually see what all problems we are getting or size of this one where size of a.
Pattern should only be used.
So this is basically generated by the LM model.
And this is one of the disadvantage that you can see display all the actors or I'll just say actors.
And here it shows size of a, I don't know, actors who acted in multiple movies.
I don't know why the size of A is basically taken.
So these are the scenarios where your LM model can also go wrong.
Okay.
But no worries at all.
Uh, because we also, uh, whenever we create some kind of applications, not everything is perfectly
right.
Right.
So these are some of the examples that you can actually check it out.
Uh, but yes, uh, I hope you got an idea with respect to the prompting strategies.
Uh, but just go ahead and check it out.
Uh, but I just want to keep this error like this so that you will be able to see it.
Okay.
So.
Yes, uh, this was it for my side.
I will see you all in the next video.
Thank you.