In the last video, we developed a basic RAC system using Lankchain, OpenAI and Pinecon.
RAC stands for Retrieval Augmented Generation and is a technique that
combines an LLM with a way to search for information.
This lets the model look at stuff like
private documents or databases while its generating text.
RAC helps overcome knowledge limits, makes answers more factual and lets the
model handle complex questions.
We used Pinecon which is a powerful
vector store great for big systems and apps.
In this video, I ll show you an
alternative to Pinecon called Chroma.
Chroma is an open source in -memory
vector store making it a better fit for small to medium -sized projects, you
might not need a separate server or hosting for it, plus you don t have to
mess around with indexes and namespaces which saves time and effort.
To install Chroma, just run pip install -q chroma -db.
Finally, I cleaned up the code a bit by getting rid of stuff outside the
functions I have defined, these functions.
I am defining a new function that creates
the embeddings using the OpenAIEmbeddings class, saves them in a Chroma database
and returns the database, df createEmbeddings Chroma.
The parameters are the chunks and a persistent directory used for storage,
its default value will be chroma -db in the current directory.
From langchain -vector -stores, I am importing Chroma and from langchain
-OpenAI, I am importing OpenAIEmbeddings, I am instantiating an embedding model to
convert text to numerical representations.
Embeddings equals OpenAIEmbeddings of
model equals and the embedding model, text -embedding -rismol and the
dimensions equals 1536.
Now I am creating a ChromaVectorStore
object, storing the embeddings and linking them back to the original text,
so vectorStore equals Chroma from documents and the arguments, chunks,
embeddings and persistent directory equals persistent directory, the
functions parameter.
I am returning the vectorStore, that's it
for this function.
I am also defining a function, that loads
the existing embeddings from disk, to a vectorStore object, def
loadEmbeddingsChroma and the function will have one argument, the persistent directory.
It will be by default, chroma -db.
I am copy -pasting the first three lines
of code from the previous function.
I am loading the existing Chroma database
from the persistent directory, specifying the embedding, so vectorStore equals
Chroma of persistent directory equals persistent directory and the embedding
function equals embeddings, the object, I have just defined one line of code above.
I am returning the vectorStore, I am running the code, let's load a PDF file,
this is about REX systems powered by Google search technology, it is worth
reading the document and you will find it in the resources, let's get back to code.
Data equals loadDocument of and the path to the files and the reg powered by
Google search .pdf.
I am splitting the document into chunks,
chunks equals chunkData of data and the chunkSize equals 256.
This is the default value, anyway, finally, I am creating the vectorStore,
vectorStore equals createEmbeddingsChroma, I am calling this
function and the functions argument are the chunks, I am running this code, I
noticed a small mistake, the argument of the function from documents is persist,
not persistent, so I will change everywhere to persist directory, I am
running the code, the data from the PDF was loaded, chunked and embedded.
Let's ask the first question, Q equals what is vertexAsearch and I will call the
function askAndGetAnswer, the one I have defined in the previous lecture, answer
equals askAndGetAnswer of vectorStore and Q, the question.
And I am printing the answer, I am running the code, I got a good answer,
that matched what was in the document, in the PDF document about regs powered by
Google search, if I ask childGPT the same question, what is vertexAsearch, it will
say something like this, as of my last update in January 2022, vertexAsearch is
not a specific product or service provided by Google, that is how it is
supposed to work, the application is pulling the answer from the PDF, not its
general knowledge.
Let's test the function that loads the
embeddings from disk into a vectorStore object, the loadEmbeddingsChroma
function, db equals loadEmbeddingsChroma, I will use the default value for the
persist directory, ChromaDb in the current directory, this one, and the
question, how many pairs of questions and answers had the StackOverflow dataset, I
am calling the askAndGetAnswer function and printing the answer, I am running the
code, alright, I got the good answer, the StackOverflow dataset had 8 million pairs
of questions and answers, let's check the answer in the PDF, ok, the answer was
correct, the StackOverflow dataset contained 8 million pairs of questions
and answers, that is how it works with Chroma, however, there is a drawback, if
I want to send a follow -up question, it will not have access to the previous chat
history and will respond that it doesn't know the context, for example, Q equals,
multiply that number by 2, the number is the one from the last question, 8 million
pairs, it answered, I don't have a specific number to multiply by 2.
Let's take a quick break and in the next video we will learn how to add memory to
our rack system, this way it will remember our old questions and we can ask
follow -ups.