Data Extraction
Last updated
!pip install --no-cache-dir --upgrade colivara_pypip install --no-cache-dir --upgrade colivara_pyimport requests
import os
def download_file(url, local_filename):
response = requests.get(url)
if response.status_code == 200:
os.makedirs('docs', exist_ok=True)
with open(local_filename, 'wb') as f:
f.write(response.content)
print(f"Successfully downloaded: {local_filename}")
else:
print(f"Failed to download: {url}")
# URLs and local filenames
files = [
{
"url": "https://github.com/tjmlabs/colivara-demo/raw/main/docs/Work-From-Home%20Guidance.pdf",
"filename": "docs/Work-From-Home-Guidance.pdf"
},
{
"url": "https://github.com/tjmlabs/colivara-demo/raw/main/docs/StaffVendorPolicy-Jan2019.pdf",
"filename": "docs/StaffVendorPolicy-Jan2019.pdf"
}
]
# Download each file
for file in files:
download_file(file["url"], file["filename"])from colivara_py import ColiVara
from pathlib import Path
import base64
rag_client = ColiVara(
base_url="https://api.colivara.com",
api_key="your-api-key"
)
new_collection = rag_client.create_collection(
name="my_collection",
metadata={"description": "A sample collection"}
)
def sync_documents():
# get all the documents under docs/ folder and upsert them to colivara
documents_dir = Path('docs')
files = [f for f in documents_dir.glob('**/*') if f.is_file()]
for file in files:
with open(file, 'rb') as f:
file_content = f.read()
encoded_content = base64.b64encode(file_content).decode('utf-8')
rag_client.upsert_document(
name=file.name,
document_base64=encoded_content,
collection_name="my_collection",
wait=True
)
print(f"Upserted: {file.name}")
sync_documents()from IPython.display import display, HTML, Image
def display_image_from_document(document):
pages = doc.pages
for page in pages:
base64_img = page.img_base64
if base64_img.startswith('data:image'):
base64_img = base64_img.split(',')[1]
image = Image(data=base64.b64decode(base64_img))
display(image)
document_name = "Work-From-Home-Guidance.pdf"
doc = client.get_document(document_name=document_name, collection_name="all", expand="pages")
display_image_from_document(doc)from IPython.display import Image
from io import BytesIO
from PIL import Image
def display_image_from_document(document):
pages = doc.pages
for page in pages:
base64_img = page.img_base64
if base64_img.startswith('data:image'):
base64_img = base64_img.split(',')[1]
image_data = base64.b64decode(base64_img)
image = Image.open(BytesIO(image_data))
image.show()
document_name = "Work-From-Home-Guidance.pdf"
doc = rag_client.get_document(
document_name=document_name,
collection_name="my_collection",
expand="pages")
display_image_from_document(doc)!pip install openai!pip install openaiimport json
from openai import OpenAI
llm_client = OpenAI(api_key="your-api-key")
# if your document is too big (20+ pages), or you want to extract data across multiple documents,
# consider a pipeline where you search first and get top 3 pages, and then do this step.
# here since our document is small - we are passing the whole document at once.
def extract_data(data_to_extract, colivara_document):
string_json = json.dumps(data_to_extract)
content = [
{
"type": "text",
"text": f"""Use the following images as a reference to extract structured data with the following user example as a guide: {string_json}.\n
If information is not available, keep the value blank.
""",
}
]
pages = colivara_document.pages
for page in pages:
base64 = f"data:image/png;base64,{page.img_base64}"
content.append(
{
"type": "image_url",
"image_url": {"url": base64}
}
)
messages = [
{"role": "system", "content": "Our goal is to find out when a policy was issued to remind our users to review it at regular intervals. Always respond in JSON"},
{"role": "user", "content": content}
]
completion = llm_client.chat.completions.create(
model="gpt-4o",
messages=messages,
response_format= { "type": "json_object" },
temperature=0.25,
seed=123
)
return completion.choices[0].message.content
document_name = "Work-From-Home-Guidance.pdf"
doc = rag_client.get_document(
document_name=document_name,
collection_name="my_collection",
expand="pages")
data_to_extract = {"year_issued": 2014, "month_issued": 3}
data = extract_data(data_to_extract, doc)
print(data){
"year_issued": 2020,
"month_issued": 3
}