We need the answer in natural language, so we'll use the large language model to
get it.
I'm creating a new function.
df askAndGetAnswer () The function will have two parameters.
The vector store and the question or the query.
First, we'll retrieve the most relevant
chunks of text from our vector database.
Then, we'll feed those chunks to the LLM
to get the final answer.
In the previous section, I've already
explained the code and now I'm just copy pasting it here.
This is the notebook from the previous section.
Ok.
This is the code.
I'm copy pasting it in the function.
And I'm indenting it correctly.
I'm also running the chain.
answer equals chain .run of q And return answer.
I'm running the set.
Let's test it.
So, q equals.
What is the whole document about?
And the answer equals askAndGetAnswer of vector store and q
And of course, print answer.
I'm running the cell.
And it says that the document is about the US Constitution.
Very well.
What I'll do next is create a loop so
that the user can send questions to the application continuously.
Import time.
i equals one.
This is a counter to display the question number.
And the message.
Write quit or exit to quit.
And now the while loop.
So, while true colon.
This will be an infinite loop.
And it will stop when the user enters quit or exit.
So, q equals input question and hash and i in curly braces.
And of course, this is an f string.
Like this.
i equals i plus one.
I'm incrementing the counter.
I'm checking if q equals exit or quit.
And if it's the case, I'm breaking out.
So, if q dot lower in quit and exit .
If it's the case, I'm printing a message quitting.
Bye -bye.
Time dot slip of two.
I'm pausing for two seconds so that the
user has enough time to read the message.
And I'm breaking out of the while loop.
If q is not quit or exit, I'm asking the question.
Answer equals ask and get answer of
vector store and q.
And I'm printing the answer.
So, answer colon and the answer in curly braces.
And I'm adding a new line before the answer.
This is for a better readability.
And also 50 dashes after each answer.
Backslash n dash times 50 and another backslash n at the end.
Ok.
This is just for readability.
For a better readability.
I have loaded the U .S. Constitution.
So, let's ask questions about the U .S. Constitution.
We have in front of us the best expert on
the U .S. Constitution.
I'm running the cell.
The first question.
What is the first amendment of the U .S. Constitution?
Let's see what it will answer.
Very good.
I've got a very good answer.
Ok.
Another question.
Explain the concept of federalism as it
is presented in the U .S. Constitution.
And I've got the answer.
It's about federalism.
Now describe the Bill of Rights.
Very good.
Note that you can send more than one
question to the LLM at the same time.
For example.
Q1.
How does the Constitution address the
issue of presidential succession?
And Q2.
The second question.
Describe the Bill of Rights.
And answer both questions.
And I'm hitting enter.
Depending on how busy the OpenAI's backend is, you'll wait a few seconds or more.
I've got the answers of both questions.
A1 and A2.
You can get accurate answers from the LLM on any topic, even if that data wasn't
included in the training.
You can also use other languages.
I'm quitting.
Now I'm loading another document.
This time I'll load a Wikipedia page in Romanian, my native language.
I use the Pinecone free tier.
And I will delete the existing index.
Because only one index is permitted.
So, delete.
Pinecone index.
And I'm running the code.
Good.
It's deleting all indexes.
And data equals.
Load from Wikipedia.
ChartGPT is the term.
And RO the language.
And chunks equals.
Chunk data.
Of data.
And index name equals.
ChartGPT.
VectorStore equals.
Insert or fetch embeddings.
Of index name.
And that's it.
I'm running it.
It will get the data.
From Wikipedia.
Chunk it.
Create an index.
Embed the chunks.
And insert both the chunks and the
embeddings into a Pinecone index.
It's creating and initializing the new index.
ChartGPT.
I'm preparing the first question in Romanian.
Q equals.
What is ChartGPT?
I'm waiting for it to finish.
And answer.
Equals.
Ask and get answer.
Of VectorStore.
And Q.
And I'm printing the answer.
It's still running.
So, I'm waiting a few more seconds.
It's done.
If you get an error.
You can run the code again.
Because Pinecone is overloaded sometimes.
I'm running this cell.
And I've got a pretty good answer in Romanian.
When was GPT -4 launched?
Very good.
And the last question.
What is InstructGPT?
Of course, I'm writing all questions in Romanian.
And I've got very good answers for each question.
The LLM was able to answer a question
from a new document that was not included in its training data.
We have a very useful application that can be used for real businesses.
However, it lacks memory or chart history.
We'll take a break.
And in the next lecture, I'll show you how to add memory or chart history to
this application.