So far, we've split the text into chunks and embedded them into vectors, which
were then inserted into a Pinecone index.
Now, let's see how to ask questions and
do similarity searches.
Here's how it works.
The user defines a query.
The query is embedded into a vector.
A similarity search is performed in the vector database and the text behind the
most similar vectors is the answer to the user's question.
I am defining a query.
So query equals.
I've used the famous speech by Winston Churchill.
We shall fight on the beaches and the
query is where should we fight.
Now, I'm extracting all the relevant chunks.
So result equals vector store dot similarity search of query and I'm
printing the result.
We see how it extracted the chunks
relevant to the query.
I can also iterate over the chunks and
print only the chunk text.
So for r in result print r dot page
content and for readability I'm adding 50 dashes after each chunk.
I've got four chunks.
These chunks represent the answer but you
can't give them to users like this.
We need the answer in a natural language.
That's where an LLM comes in.
We'll retrieve the most relevant chunks
of text and feed them to the language model for the final answer.
Let's do this.
I'm importing the retrieval QA chain from
lang chain chains import retrieval QA.
I'm also importing chat open AI the LLM
from lang chain chat models import chat open AI.
I'm defining the LLM chat open AI of model equals gpt 3 .5 turbo or gpt 4 and
the temperature equals one.
Next, I'll expose this index in a
retriever interface.
The retriever interface is a generic
interface that makes it easy to combine documents with language models.
So retriever equals vector store as retriever of search type equals similarity.
Similarity as a string and the second argument search k works equals and the dictionary.
The key is k and the value an integer three k equals three means that it will
return the three most similar chunks to the user's query.
And finally, I'm creating a chain to answer questions chain equals retrieval
QA dot from chain type of LLM equals LLM chain type equals stuff and the retriever
equals retriever.
The default chain type equals stuff uses
all of the text from the documents in the prompt.
I'm running the code in this cell.
And I've got this warning because I have misspelled the word model.
It's model not mode.
I'm running the cell again.
There are no warnings.
Let's ask some questions about the
content of the document query where should we fight?
And the answer equals chain dot run of query.
I'm running the chain.
I'm running it.
And I'm printing the answer.
And I've got the correct answer from the document in an actual language format.
Let's ask something different query equals who was the king of Belgium at
that time?
It's in the document.
Perfect.
This is the correct answer.
And what about the French armies?
Great.
I'm running the code again.
If you ask it again, you'll get a
different answer.
Each answer is original.
This is what the transformer technology means.
Great.
I hope you enjoyed this section.
You've learned a lot.
You've learned how to install a link chain in pine cone and how to set up the environment.
You've also learned about LLM models and how to call them from link chain, prompt
templates and how to create dynamic prompts, simple and sequential chains and
the link chain agents.
We then moved on to embeddings and vector stores.
At the end, we saw how to combine everything together to create the
backbone of an OPL application.
OPL stands for open AI, pine cone and
link chain.
In the next section, we'll put together
everything we've learned so far and develop an LLM powered application that
can answer questions about the content of private documents.
We'll use Python, link chain, pine cone and open AI.
This will be really fun.