Machine Learning Spot

LangChain Claude AI: Guru’s Path to Powerful RAG (2024)

Let’s talk about an LLM with a 200K context window, a more intelligent but still cheaper LLM. Claude is an LLM developed by Anthropic which claims to be better than ChatGPT in many benchmarks; even customer reviews are better than ChatGPT. This blog on Langchain Claude AI will teach you how to build a RAG with the best LLM available.

Langchain Claude AI: Learning Objectives Of The Blog

This blog is going to teach you a lot here is list of things that it will teach you about.

  1. What is Anthropic?
  2. What is Claude AI
  3. ChatGPT vs CLaude
  4. A brief company Profile of Anthropic
  5. How to use Claude AI’s API?
  6. How to Use CLaude AI chat?
  7. How to make a RAG using Claude AI

Story of Anthropic and Claude: A Brief Overview of the Company

Anthropic AI is a San Francisco-based AI research and safety company. I recently came across this fascinating company; they are doing marvelous work in the field of AI. Their research teams are focused on ensuring AI develops in a way that benefits society.

Sounds Familiar? Yes, because this company was founded by ex-OpenAI researchers back in 2021. Since its foundation, investors have taken an interest in it. The company has received backing from various companies, like Google’s $300 Million, Menlo Ventures investment of $750 Million, Amazon’s first of $1.25, then of $2.75 in total $4 billion as they had promised, and others raised their valuation to $18.4 billion.

What I like about them most is how their research teams operate. They have a team called Interpretability, which digs into how large language models work, ensuring they are safe and effective. Another team, Alignment, is dedicated to keeping future AI advancements honest and harmless. The Societal Impacts team works with policy experts to ensure AI interacts positively with people.

Now let’s talk about the flagship product of Anthropic, “The Claud.” What’s special about Claud is its reasoning abilities, which go beyond just pattern recognition or text generation; it can easily transcribe and analyze almost any static image, whether it be handwritten notes or graphs. For example, as you can see below, I gave it an old piece of paper of Isaac Newton’s work; in which there are both diagrams and old handwritten text. You can see the performance on your own.

Claude also excels in code generation, is capable of creating websites in HTML and CSS, converting images into structured JSON data, and debugging complex codebases thus also a good choice for code generation

The Claude 3 offers three models to choose giving it more versatility

  1. Haiku is their fastest model, giving it industry-level speed ideal for lightweight actions, according to them.
  2. For high-throughput task, they have Sonnet the best combination of performance and speed.
  3. The third one is Opus which is the most intelligent one designed for complex analysis, longer tasks with multiple steps, and higher-order math and coding tasks.

Now as we are also going to use it for Langchain Claude AI-based RAG let’s see a quick comparison between the performance of ChatGPT and Claude AI before writing our code on Langchain Claude AI in which I have made a RAG using langchain and Claude.

Claude 3 OpusClaude 3 SonnetClaude 3 HaikuGPT4GPT 3.5
Undergraduate-level knowledge of MMLU 86.8%
(5 shot)
79.0%
(5 shot)
75.2%
(5 shot)
86.4%
(5 shot)
70%
(5 shot)

Graduate level reasoning GPQA, Diamond
50.4%
(0 shot COT)
40.4%
(0 shot COT)
33.3%
(0 shot COT)
35.7%
(0 shot COT)
28.1%
(0 shot COT)
Grad school math GSM&K
(0 shot COT)
95.0%
(0 shot COT)
92.3%
(0 shot COT)
88.9%
(0 shot COT)
92.0%
(5 shot COT)
57.1%
(5 shot)
Math problem-solving MATH
60.1%
(0 shot COT)
43.1%
(0 shot COT)
38.9%
(0 shot COT)
52.9%
(4 shot)
34.1%
(4 shot)
Multilingual math MGSM
90.7%
(0 shot)
83.5%
(0 shot)
75.1%
(0 shot)
74.5%
(8 shot)
Code HumanEval
(0 shot)
84.9%
(0 shot)
73%
(0 shot)
75.9%
(0 shot)
67.7%
(0 shot)
48.1%
(0 shot)
Reasoning Over Text Drop, F1score
(0 shot)
83.1%
(3 shot)
78.9%
(3 shot)
78.4%
(3 shot)
80.9%
(3 shot)
64.1%
(3 shot)
Mixed Evaluations Big-Bench-Hard
86.8%
(3 shot COT)
82.9%
(3 shot COT)
73.7%
(3 shot COT)
83.1%
(3 shot COT)
66.6%
(3 shot COT)
Knowledge Q&A
ARC-Challenge
96.4%
(25 shot)
93.2%
(25 shot)
89.2%
(25 shot)
96.3%
(25 shot)
85.2%
(25 shot)
Common Knowledge HellaSwag95.4
(10 shot)
89
(10 shot)
85.9
(10 shot)
95.3
(10 shot)
85.5
(10 shot)

Installing every Required Package

%pip install langchain
%pip install langchain-community 
%pip install faiss-cpu  
%pip install tiktoken
%pip install pypdf
%pip install -qU langchain-anthropic
%pip install sentence-transformers

Importing Every Required Package


from langchain_community.document_loaders import PyPDFLoader # To load pdf file

from langchain_community.vectorstores import FAISS

from langchain_text_splitters import RecursiveCharacterTextSplitter # To split text to reduce token size

from langchain.embeddings import HuggingFaceEmbeddings

from langchain_anthropic import ChatAnthropic

from langchain import PromptTemplate

import os

Step 1: Setting Up The Document and API

Claude AI offers 14-day free trial in which you can use any of their model including the best and most intelligent “Opus” which we will use here. So first to get API sign up ,get it, copy it, and save it in a variable which we will use to invoke our LLM.

My_API = "Enter Your API Key"


As RAG stands for Retrieval Augmented Generation we need a document to retrieve something, so let’s upload a document and split it into pages and letter into small chunks of text. The book I am using here is Metamorphosis by Franz Kafka.

loader = PyPDFLoader("/content/Metamorphosis.pdf") # change with path of your Doc
pages = loader.load_and_split()

         # Breaking Text into smaller Chunks

text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)

docs = text_splitter.split_documents(pages)
len(docs) # To check number of chunks it get divided into

Preparations For The Augmentation

Here, we will use a free model available at Hugging Face that will convert our text to embeddings so that we can find our text using semantic search.

Then we will save our embeddings and chunked files in a vector store. Then we will retrieve the our required information by performing a similarity search and then create a prompt template which we will later use to augment our information with the prompt and send it to LLM along with our Query

                   # Converting into Embeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")


                 
                  #Storing into vector store 
 
our_database = FAISS.from_documents(docs, embeddings)

our_database.save_local("faiss_index") # Replace faiss_index with name of your choice
                  # Performing Similarity Search
our_query = "who was gregor"

docs = our_database.similarity_search(our_query)

print(docs)

            # Creating a prompt Template to Augment our retrieved docs

prompt = "this context is taken from a pdf of novel given the context defined in my context answer the user_query \n user_query : {query} context :{context} "
prompt.format(query = our_query , context = docs)

r_template = PromptTemplate(
    input_variables=["context","query"],
    template=prompt)

The Generation

Now, we will invoke the LLM and our blog on Langchain Claude AI. Let’s experience Claude AI and see the response.

chat = ChatAnthropic(temperature=0, api_key= My_API , model_name="claude-3-opus-20240229")

chain = Our_template | chat #model hata dia

chain.invoke({"context": docs, "query": our_query})

Output:

AIMessage(content='Based on the context provided, it appears that Gregor Samsa was the main character in Franz Kafka\'s novella "The Metamorphosis". Gregor wakes up one morning to find himself transformed into a giant insect-like creature. The story follows Gregor as he struggles to adapt to his new condition and the impact it has on his family, who are initially shocked and repulsed. Over time, Gregor becomes increasingly isolated and neglected by his family. Eventually, Gregor dies alone in his room, and his family seems relieved to be free of the burden he had become. The story explores themes of alienation, family duty, and the human condition.', response_metadata={'id': 'msg_01Q6haGFSS6sULHPoeARwsv6', 'model': 'claude-3-opus-20240229', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 63856, 'output_tokens': 149}}, id='run-47eec82e-58ed-4509-8637-87d609f560aa-0')

Conclusion:

Hurray! you have now learned how to develop a RAG with Langchain Claude AI. If you want to learn how Langchain RAG works and how we can implement it from the basics of how openAI is used, then see this blog post, and if you want to learn about advanced RAG, then my this blog post. I hope you enjoyed my blog on Langchain Claude AI; feel free to reach out for feedback

Liked the Post?  You can share as well

Facebook
Twitter
LinkedIn

More From Machine Learning Spot

Get The Latest AI News and Insights

Directly To Your Inbox
Subscribe

Signup ML Spot Newsletter

What Will You Get?

Bonus

Get A Free Workshop on
AI Development