RAG app with GROQ with unlimited rate limit!
Published: 2024-05-05 18:09:16

Today, we’re diving into the exciting world of LangChain RAG applications, a revolutionary approach to building intelligent and informative AI experiences.

**What is LangChain RAG?**

Imagine an LLM that can not only access its vast internal knowledge but can also tap into the specific data you provide. That’s the magic of Retrieval-Augmented Generation (RAG) with LangChain. Here’s the gist:

- Data Retrieval: LangChain’s RAG framework lets you create a custom index of your data, be it documents, code, or any other relevant information.
- Smart Assistant: When a user interacts with your application, LangChain retrieves the most relevant data points from your index based on their query.
- Supercharged LLM: This retrieved data is then fed to the LLM, essentially giving it context and boosting its ability to understand and respond to the user’s specific needs.

**Why Use LangChain RAG?**

The benefits of LangChain RAG are numerous. Here are a few to get you excited:

- Domain Expertise: Inject your LLM with the knowledge of your specific field, allowing it to answer complex questions and generate human-quality text tailored to your domain.
- Private Data Power: Use your own private data that LLMs wouldn’t normally have access to, unlocking a whole new level of personalization and accuracy.
- Always Up-to-Date: Keep your LLM sharp by incorporating the latest information from your data source, ensuring your application stays relevant.

**Building Your First LangChain RAG App with no rate limit!
**
The LangChain framework offers a user-friendly platform to build your RAG application. Here’s a quick peek at the process:

- Data Preparation: Structure your data for efficient retrieval. This might involve cleaning, transforming, and indexing your information.
- LangChain Pipeline: Craft a LangChain pipeline that retrieves relevant data points based on user queries and feeds them to your chosen LLM.
- Interactive Interface: Design a user-friendly interface where users can interact with your AI-powered application.

**So lets Dive into code:**

You can start selecting the build in IDE, Google colab which has free RAM and GPUs in it to give you freedom for creativity in AI/ML etc.

Click the following link:

[Google colab](https://colab.research.google.com/)

Lets start coding!

_Side by side do it with me_ (Press windows + left arrow key and blog on window + right arrow key)

Install the framworks from langchain in colab

```
! pip install langchain_community tiktoken langchain-groq langchainhub chromadb langchain langchain_core sentence-transformers pypdf
```

Sign up at [Groq.com](https://groq.com/) for your free API key!

```
os.environ['GROQ_API_KEY'] = '<your API key>'
```

Run the following code for uploading a document in colab:)

```
from google.colab import files
uploaded = files.upload()
```

RAG code for Talking to your pdf document:

```
import bs4
from langchain import hub
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain_groq import ChatGroq
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_core.embeddings import Embeddings
from langchain_community.document_loaders import TextLoader
from langchain_community.document_loaders import PyPDFLoader


#### INDEXING ####


# Load pdf
loader = PyPDFLoader("./press ctrl+space")
pages = loader.load_and_split()


# Split

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(pages)


# Embed

vectorstore = Chroma.from_documents(documents=splits,
                                    embedding=HuggingFaceEmbeddings())

retriever = vectorstore.as_retriever()


#### RETRIEVAL and GENERATION ####


# Prompt
prompt = hub.pull("rlm/rag-prompt")


# LLM

llm = ChatGroq(model_name="mixtral-8x7b-32768", temperature=0)


# Post-processing

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)


# Chain

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)


# Question

rag_chain.invoke("")# write a question related to pdf data!
```


ooha! You completed the project of Talk to your pdfs with Retrieval Augment Generation model.

Writing blogs on open source projects!

stay tuned for the next blog, follow!

I will build next with Llama 3 and other open source model.

If you any problem you can join my discord community we build projects daily, So I named it [Projects Every](https://discord.com/invite/z38ZJ55nbv).

Subscribe:)