"* how to run Llama2 locally on a Mac using llama-cpp-python and the llama-cpp's quantized Llama2 model;\n",
"* how to use LangChain to ask Llama general questions;\n",
"* how to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and ask questions about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama2 be able to answer questions about the data not publicly available when Llama2 was trained, or about your own data. RAG is one way to prevent LLM's hallucination. "
"* How to run Llama2 locally on a Mac using llama-cpp-python and the llama-cpp's quantized Llama2 model\n",
"* How to use LangChain to ask Llama general questions\n",
"* How to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and ask questions about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama2 be able to answer questions about the data not publicly available when Llama2 was trained, or about your own data. RAG is one way to prevent LLM's hallucination"
*how to run Llama2 locally on a Mac using llama-cpp-python and the llama-cpp's quantized Llama2 model;
*how to use LangChain to ask Llama general questions;
*how to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and ask questions about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama2 be able to answer questions about the data not publicly available when Llama2 was trained, or about your own data. RAG is one way to prevent LLM's hallucination.
*How to run Llama2 locally on a Mac using llama-cpp-python and the llama-cpp's quantized Llama2 model
*How to use LangChain to ask Llama general questions
*How to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and ask questions about it. This is the well known RAG (Retrieval Augmented Generation) method to let LLM such as Llama2 be able to answer questions about the data not publicly available when Llama2 was trained, or about your own data. RAG is one way to prevent LLM's hallucination
%% Cell type:markdown id:22450267 tags:
We start by installing necessary requirements and import packages we will be using in this example.
-[llama-cpp-python](https://github.com/abetlen/llama-cpp-python) a simple Python bindings for [llama.cpp](https://github.com/ggerganov/llama.cpp) library
- pypdf gives us the ability to work with pdfs
- sentence-transformers for text embeddings
- chromadb gives us database capabilities
- langchain provides necessary RAG tools for this demo
Next, initialize the langchain `CallBackManager`. This handles callbacks from Langchain and for this example we will use token-wise streaming so the answer gets generated token by token when Llama is answering your question.
%% Cell type:code id:01fe5b9c tags:
``` python
# for token-wise streaming so you'll see the answer gets generated token by token when Llama is answering your question
Replace `<path-to-llama-gguf-file>` with the path either to your downloaded quantized model file [here](https://drive.google.com/file/d/1afPv3HOy73BE2MoYCgYJvBDeQNa9rZbj/view?usp=sharing),
or to the `ggml-model-q4_0.gguf` file built with the following commands:
One way we can fix the hallucinations is to use RAG, to augment it with more recent or custom data that holds the information for it to answer correctly.
First we load the Llama2 paper using LangChain's [PDF loader](https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf)
%% Cell type:code id:f3ebc261 tags:
``` python
fromlangchain.document_loadersimportPyPDFLoader
loader=PyPDFLoader("llama2.pdf")
documents=loader.load()
```
%% Cell type:code id:302eaa54 tags:
``` python
# quick check on the loaded document for the correct pages etc
There are more than 30 vector stores (DBs) supported by LangChain.
For this example we will use [Chroma](https://python.langchain.com/docs/integrations/vectorstores/chroma) which is light-weight and in memory so it's easy to get started with.
For other vector stores especially if you need to store a large amount of data - see https://python.langchain.com/docs/integrations/vectorstores
We will also import the `HuggingFaceEmbeddings` and `RecursiveCharacterTextSplitter` to assist in storing the documents.
%% Cell type:code id:4f94f6f8 tags:
``` python
fromlangchain.vectorstoresimportChroma
# embeddings are numerical representations of the question and answer text
To store the documents, we will need to split them into chunks using [`RecursiveCharacterTextSplitter`](https://python.langchain.com/docs/modules/data_connection/document_transformers/text_splitters/recursive_text_splitter) and create vector representations of these chunks using [`HuggingFaceEmbeddings`](https://www.google.com/search?q=langchain+hugging+face+embeddings&sca_esv=572890011&ei=ARUoZaH4LuumptQP48ah2Ac&oq=langchian+hugg&gs_lp=Egxnd3Mtd2l6LXNlcnAiDmxhbmdjaGlhbiBodWdnKgIIADIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCjIHEAAYgAQYCkjeHlC5Cli5D3ABeAGQAQCYAV6gAb4CqgEBNLgBAcgBAPgBAcICChAAGEcY1gQYsAPiAwQYACBBiAYBkAYI&sclient=gws-wiz-serp) on them before storing them into our vector database.
# create the vector db to store all the split chunks as embeddings
embeddings=HuggingFaceEmbeddings()
vectordb=Chroma.from_documents(
documents=all_splits,
embedding=embeddings,
)
```
%% Cell type:markdown id:bddc38e8 tags:
We then use ` RetrievalQA` to retrieve the documents from the vector database and give the model more context on Llama 2, thereby increasing its knowledge.
%% Cell type:code id:1a2472c9 tags:
``` python
# use another LangChain's chain, RetrievalQA, to associate Llama with the loaded documents stored in the vector db
fromlangchain.chainsimportRetrievalQA
qa_chain=RetrievalQA.from_chain_type(
llm,
retriever=vectordb.as_retriever()
)
```
%% Cell type:markdown id:db71e5d7 tags:
For each question, LangChain performs a semantic similarity search of it in the vector db, then passes the search results as the context to the model to answer the question.
It takes close to 2 minutes to return the result (but using other vector stores other than Chroma such as FAISS can take longer) because Llama2 is running on a local Mac.
To get much faster results, you can use a cloud service with GPU used for inference - see HelloLlamaCloud for a demo.