RAG by Ollama + langchain + faiss


wdnlotm

Don’t want to read papers?
Let LLMs read them for you.



2025-07-02

Let’s build a RAG agent on Rivanna that reads papers for you.

  • We need to download ollama first.
  • We will use an apptainer image.

Download LLM - Llama3.1

  • With the ollama server running - ollama serve
  • Download Llama3.1 - ollama pull llama3.1

With ollama server running, the coding begins

# from langchain_ollama.llms import OllamaLLM
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama import OllamaLLM
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA

Let’s test the Llama3.1 alone.

“how many teams are in MLB?”

# Make sure that ollama serve is running
print(chain.invoke({"question": "how many teams are in MLB?"}))
There are currently 30 teams in Major League Baseball (MLB).

“Tell me a joke”

response = model.invoke("Tell me a joke")
print(response)
A man walks into a library and asks the librarian, "Do you have any books on Pavlov's dogs and Schrödinger's cat?"

The librarian replies, "It rings a bell, but I'm not sure if it's here or not."

“Tell me another joke”

response = model.invoke("Tell me another joke")
print(response)
Here's one:

What do you call a fake noodle?

(Wait for it...)

An impasta!

I hope that one made you laugh! Do you want to hear another?

Read a pdf file. It’s a paper regarding atherosclerosis.

# Load the document
loader = PyPDFLoader("docs/newman2021.pdf")
documents = loader.load()

# Split the document into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=30, separator="\n")
docs = text_splitter.split_documents(documents=documents)

Agent

while True:
    query = input("Type your query (or type 'Exit' to quit): \n")
    if query.lower() == "exit":
        break
    # result = qa.run(query)
    result = qa.invoke(query)
    print(result['result'])
Type your query (or type 'Exit' to quit): 
 Where are fibrous cap cells from?
According to the text, fibrous cap cells can originate from multiple cell types. The proportion of ACTA2+ cells in the fibrous cap derived from a SMC (smooth muscle cell) origin is determined by equation (1), which calculates efficiency as ¼ medial PLAþ ACTA2þ cell count / total medial ACTA2þ cell count.

However, it's also stated that not all ACTA2+ cells are of SMC origin. In fact, the proportion of ACTA2+ cells in the fibrous cap derived from a non-SMC origin is determined by equation (3), which calculates this as 1 - corrected PLAþ ACTA2þ / total fibrous cap ACTA2þ cell count.

Additionally, the text mentions that up to 40% of ACTA2+ cells were derived from endoMT or MMT in PdgfrbSMC-WT /WT lesions. Furthermore, around 20% of ACTA2+ cells within the fibrous cap were derived from eCs (endothelial cells), as determined by lineage tracing mice.

So, to answer your question, fibrous cap cells can originate from a variety of cell types, including smooth muscle cells, endothelial cells, and possibly others.
Type your query (or type 'Exit' to quit): 
 Exit