
In artificial intelligence (AI), retrieval augmented generation (RAG) is the technique of retrieving data from external data sources to improve large language models' (LLMs) response.
Sometimes, LLM's trained data isn't enough, and this is where RAG comes into play to reduce a model's knowledge gaps and avoid hallucinations.
In this guide, you'll use Chroma, an open-source vector database, to improve the quality of the Llama 2 model.
Before you begin:
Deploy a new Ubuntu 22.04 A100 Vultr Cloud GPU Server with at least:
Create a non-root user with sudo rights and switch to the account.
The sample Python source codes in this guide require some Python libraries. Install the libraries using the following command.
In Natural Language Processing (NLP), an embedding is a numerical representation of a word. Embeddings of words with similar meanings are always close to each other in a vector space. Computers understand numbers better than words, and converting words to embeddings improves the quality of NLP. To know how embeddings work in LLM, follow the steps below:
Create a new embeddings.py file using a text editor like nano.
Enter the following information into the embeddings.py file.
Save and close the file.
Run the embeddings.py file.
Output:
From the above output:
phrase1 and phrase3 are semantically closer to each other. Similary, phrase2 and phrase4 are also closer in the vector space.Chroma is an open-source vector database that allows you to store and query embeddings using sematic search. Unlike relational database management systems like MySQL or PostgreSQL, Chroma uses collections instead of data tables to organize data.
The following is the basic process of how you should perform a semantic search works in a Chroma database:
To understand how you can implement the above process in a real-life example, follow the steps below:
Create a new chroma.py file.
Enter the following information into the chroma.py file.
Save and close the file.
Run the chroma.py file.
The following output returns the list items closely related to the Mary got half-baked cake from John search phrase.
You populated the Chroma database using a Python list in the previous step. You can now populate the database with data from external sources to address advanced use cases.
In this section, you're to use a sample CSV file for Oscars winners and nominees to populate the Chroma database
Download the oscars.csv file using the Linux wget command.
Create a new search.py file.
Enter the following information into the search.py file.
Save and close the file
Run the search.py file.
Output:
In this section, you'll implement the power of RAG and vector database to improve the response of a Llama model. Follow the steps below to launch a Docker container, expose a Llama model through an API, and access the API using a Python code:
Initialize the container variables. Replace $HF_TOKEN with the correct Hugging Face access token.
Run the following command to download and start the Hugging Face text generation inference container.
Wait for the image to download and install.
Monitor the Docker logs as the container loads.
Wait until you see the following output that confirms the API is listening for incoming connections.
Open a new rag.py file.
Enter the following information into the rag.py file.
Save and close the file.
Run the rag.py file.
Output:
In this guide, you've implemented a Chroma vector database to store vector data and improve the quality of LLM semantic search. You've also learned how embeddings work in NLP.
0 Comments
Be the first to comment and share your perspective with the community.