Terminal 1
%pip install indexify-extractor-sdk indexify-langchain indexify
# Download Indexify Server
!curl https://getindexify.ai | sh
# Install Poppler (required for PDF extraction)
# You can use brew on MacOS.
!sudo apt-get install -y poppler-utils
# Download Extractors
!indexify-extractor download tensorlake/chunk-extractor
!indexify-extractor download tensorlake/minilm-l6
!indexify-extractor download tensorlake/pdf-extractor
After installing the necessary libraries, download the server, and the extractors, you need to restart the runtime. Then, you have to run Indexify Server with the Extractors.
Open 2 terminals and run the following commands:
# Terminal 1
./indexify server -d
# Terminal 2
indexify-extractor join-server
Create Extraction Graph¶
Instantiate the Indexify Client
from indexify import IndexifyClient, ExtractionGraph
client = IndexifyClient()
First, create policies to get contents out of the Insurance PDF and then create chunks and embeddings.
extraction_graph_spec = """
name: 'knowledgebase'
extraction_policies:
- extractor: 'tensorlake/pdf-extractor'
name: 'pdfextractor'
- extractor: 'tensorlake/chunk-extractor'
name: 'chunks'
content_source: 'pdfextractor'
input_params:
chunk_size: 512
overlap: 150
- extractor: 'tensorlake/minilm-l6'
name: 'getembeddings'
content_source: 'chunks'
"""
extraction_graph = ExtractionGraph.from_yaml(extraction_graph_spec)
client.create_extraction_graph(extraction_graph)
Upload a Insurance PDF File¶
import requests
req = requests.get("https://irdai.gov.in/documents/37343/931203/CHIHMGP22132V012122_HEALTH2083.pdf/b5c15df2-8a5a-5927-10d8-c3d136055139?version=1.1&t=1668769248703&download=true")
with open('HEALTH2083.pdf','wb') as f:
f.write(req.content)
cid = client.upload_file("knowledgebase", path="HEALTH2083.pdf")
client.wait_for_extraction(cid)
What is happening behind the scenes¶
Indexify is designed to seamlessly respond to ingestion events by assessing all existing policies and triggering the necessary extractors for extraction. Once the PDF extractor completes the process of extracting texts, bytes, and JSONs from the document, it automatically initiates the embedding extractor to chunk the content, extract embeddings, and populate an index.
With Indexify, you have the ability to upload hundreds of Insurance PDF files simultaneously, and the platform will efficiently handle the extraction and indexing of the contents without requiring manual intervention. To expedite the extraction process, you can deploy multiple instances of the extractors, and Indexify's built-in scheduler will transparently distribute the workload among them, ensuring optimal performance and efficiency.
Perform RAG¶
Initialize the Langchain Retriever.
from indexify_langchain import IndexifyRetriever
params = {"name": "knowledgebase.getembeddings.embedding", "top_k": 3}
retriever = IndexifyRetriever(client=client, params=params)
Now create a chain to prompt OpenAI with data retrieved from Indexify to create a simple Q&A bot
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
model = ChatOpenAI()
chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
Now ask any question related to the ingested Insurance PDF document
chain.invoke("What is a Ayush Hospital?")
# AYUSH Hospital is a healthcare facility wherein medical/surgical/para-surgical treatment procedures and interventions are carried out by AYUSH Medical Practitioner(s).