## This demo app shows:
* How to run Llama2 in the cloud hosted on Replicate
* How to use LangChain to ask Llama general questions and follow up questions
* How to use LangChain to load a recent PDF doc - the Llama2 paper pdf - and chat 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
* You should also review the [HelloLlamaLocal](HelloLlamaLocal.ipynb) notebook for more information on RAG
**Note** We will be using Replicate to run the examples here. You will need to first sign in with Replicate with your github account, then create a free API token [here](https://replicate.com/account/api-tokens) that you can use for a while.
After the free trial ends, you will need to enter billing info to continue to use Llama2 hosted on Replicate.
Let's start by installing the necessary packages:
- sentence-transformers for text embeddings
- chromadb gives us database capabilities
- langchain provides necessary RAG tools for this demo
And setting up the Replicate token.
Next we call the Llama 2 model from replicate. In this example we will use the llama 2 13b chat model. You can find more Llama 2 models by searching for them on the [Replicate model explore page](https://replicate.com/explore?query=llama).
You can add them here in the format: model_name/version
With the model set up, you are now ready to ask some questions. Here is an example of the simplest way to ask the model some general questions.
We will then try to follow up the response with a question asking for more information on the book.
Since the chat history is not passed on Llama doesn't have the context and doesn't know this is more about the book thus it treats this as new query.
To get around this we will need to provide the model with history of the chat.
To do this, we will use [`ConversationBufferMemory`](https://python.langchain.com/docs/modules/memory/types/buffer) to pass the chat history to the model and give it the capability to handle follow up questions.
Once this is set up, let us repeat the steps from before and ask the model a simple question.
Then we pass the question and answer back into the model for context along with the follow up question.
Next, let's explore using Llama 2 to answer questions using documents for context.
This gives us the ability to update Llama 2's knowledge thus giving it better context without needing to finetune.
For a more in-depth study of this, see the notebook on using Llama 2 locally [here](HelloLlamaLocal.ipynb)
We will use the PyPDFLoader to load in a pdf, in this case, the Llama 2 paper.
We need to store our documents. 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.
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.
In general, you should use larger chuck sizes for highly structured text such as code and smaller size for less structured text. You may need to experiment with different chunk sizes and overlap values to find out the best numbers.
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.
For each question, LangChain performs a semantic similarity search of it in the vector db, then passes the search results as the context to Llama to answer the question.
Now, lets bring it all together by incorporating follow up questions.
First we ask a follow up questions without giving the model context of the previous conversation.
Without this context, the answer we get does not relate to our original question.
As we did before, let us use the `ConversationalRetrievalChain` package to give the model context of our previous question so we can add follow up questions.
Further follow ups can be made possible by updating chat_history.
Note that results can get cut off. You may set "max_new_tokens" in the Replicate call above to a larger number (like shown below) to avoid the cut off.
```python
model_kwargs={"temperature": 0.01, "top_p": 1, "max_new_tokens": 1000}
```