data_processing_openai_trigger / extract_insights.py
Omkar008's picture
Update extract_insights.py
4707fe2 verified
from fastapi import FastAPI, Request, BackgroundTasks
import json
import io
from openai import Client
from supabase import create_client
from typing import List, Dict, Any
import asyncio
import logging
from datetime import datetime
import os
import tiktoken
import re
app = FastAPI()
client = Client(api_key=os.getenv('OPENAI_API_KEY'),organization=os.getenv('ORG_ID'))
url: str = os.getenv('SUPABASE_URL')
key: str = os.getenv('SUPABASE_KEY')
supabase = create_client(url, key)
# @app.post("/send/synchronous_processing")
# async def testv1(request: Request, background_tasks: BackgroundTasks):
# try:
# body_data = await request.json()
# user_id = body_data.get("user_id")
# email = body_data.get("email")
# message_id = body_data.get("message_id")
# raw_text = body_data.get("raw_text")
# # Add processing to background tasks
# background_tasks.add_task(process_synchronous_job, user_id,email,message_id,raw_text)
# return {'data': 'Batch job is scheduled!'}
# except Exception as e:
# return {'error': str(e)}
# def receipt_radar_prompt(raw_text:str)->str:
# insurance_response_structure = """
# {
# "insurance_type": "Classify it into 8 categories travel , health , term , vehicle, property,liability, life , buisness only .Try to find the closest possible based on the receipt text, if you don't understand the type classify it as others.",
# "policy_details": {
# "policyholder_name": "",
# "policy_number": "",
# "insurance_start_date": "",
# "insurance_end_date": "",
# "premium_amount": "",
# "payment_frequency": ""
# },
# "coverage_details": {
# "covered_items": {
# "item_type": "",
# "product_company": "",
# "product_model": "",
# "product_manufacturing_year": ""
# },
# "comprehensive_coverage_type_policy": "yes/no"
# }
# }
# """
# travel_response_structure = """
# travel_type(bus,train,airplane,taxi,bike,rickshaw classify in these categories only strictly),travel_company_name , departure_destination , arrival_destination , arrival_city(if you are not able to find the arrival city add the arrival destination into this field strictly. ), departure_date,arrival_date .If the arrival and departure dates are the same from receipt text given to you analyse it properly to check that, then only use the same date in both the fields .if you don't find any field mark it as null.
# """
# hotel_data_points = """ hotel_type(hotel_stay , dine_in , dine_in + stay(use both keyword strictly)), hotel_brand_name , hotel_location , hotel_checkin_date , hotel_checkout_date. if you don't find any field mark it as null """
# coupon_data_points = """
# {
# "brand_name": "Extract the brand_name from where the coupon is sent",
# "validity": {
# "start_date": "Date the coupon is valid from, if provided.other wise mark it as null",
# "expiration_date": "Date the coupon expires. Leave blank if not mentioned."
# },
# "coupon_code": "Extract the unique code found in the email. If unavailable, leave it blank.",
# "description": "Provide the discount details (amount/percentage), minimum purchase, eligible products, retailer, any terms or restrictions, usage limit (single-use/multi-use), and where the coupon applies (in-store/online)."
# }
# """
# system_prompt = f"""Extract information from the following receipt OCR text and return a JSON object with these exact keys: brand, total_cost, location, purchase_category, brand_category, Date, currency, filename, payment_method, metadata.
# Rules:
# 1. For total_cost, use the highest monetary value in the text.
# 2. For brand_category, choose the closest match from: ["Fashion and Apparel", "Jewelry and Watches", "Beauty and Personal Care", "Automobiles", "Real Estate", "Travel(it may contain reciepts of airlines , trains , taxi ,cruise ,etc)", "Hospitality(it will include reciepts of Hotels (stays) , restaurants , cafe's , bar's , Accommodation Services , Beverages Services (don't include food delivery service in hospitality))","Food Delivery Services(like swiggy , zomato,eatsure and any other you can analyse from receipt text)", "Home and Lifestyle", "Technology and Electronics", "Sports and Leisure", "Art and Collectibles", "Health and Wellness", "Stationery and Writing Instruments", "Children and Baby", "Pet Accessories", "Insurance"]
# 3. Format Date as dd-mm-yyyy.Strictly return the date in the format dd-mm-yyyy.
# 4. metadata: For insurance receipts extract the data points given in the JSON and return the JSON with structure: \n """ + insurance_response_structure + """
# 5.metadata : For travel receipts(flight ,bus,train) extract these data points as a JSON object exactly""" + travel_response_structure + """
# 6. metadata : For hotel receipts extract these data points as a JSON object exactly""" + hotel_data_points + f"""
# 7. metadata : For coupon receipts extract these data points as a JSON object exactly""" + coupon_data_points + f"""
# For non-insurance and non-travel , non-hotel receipts, return metadata as null.
# 8. Use currency codes (e.g., USD, EUR) instead of symbols.
# 9. Generate filename as 'PURCHASE_TYPE_BRAND_DATE' (e.g., 'clothing_gucci_20230715').
# 10. If a value is not found, return null.
# 11. If all values are null, return null.
# Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
# Here's the OCR text below analyse it and convert into json using keys provided in first line and using the rules provided in rules section:
# Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
# {raw_text}
# """
# return system_prompt
def receipt_radar_prompt(raw_text:str)->str:
insurance_response_structure = """
{
"insurance_type": "Classify it into 8 categories travel , health , term , vehicle, property,liability, life , buisness only .Try to find the closest possible based on the receipt text, if you don't understand the type classify it as others.",
"policy_details": {
"policyholder_name": "",
"policy_number": "",
"insurance_start_date": "",
"insurance_end_date": "",
"premium_amount": "",
"payment_frequency": ""
},
"coverage_details": {
"covered_items": {
"item_type": "",
"product_company": "",
"product_model": "",
"product_manufacturing_year": ""
},
"comprehensive_coverage_type_policy": "yes/no"
}
}
"""
travel_response_structure = """
travel_type(bus,train,airplane,taxi,bike,rickshaw classify in these categories only strictly),travel_company_name , departure_destination , arrival_destination , arrival_city(if you are not able to find the arrival city add the arrival destination into this field strictly. ), departure_date,arrival_date .If the arrival and departure dates are the same from receipt text given to you analyse it properly to check that, then only use the same date in both the fields .if you don't find any field mark it as null.
"""
hotel_data_points = """ hotel_type(hotel_stay , dine_in , dine_in + stay(use both keyword strictly)), hotel_brand_name , hotel_location , hotel_checkin_date , hotel_checkout_date. if you don't find any field mark it as null """
coupon_data_points = """
{
"brand_name": "Extract the brand_name from where the coupon is sent",
"validity": {
"start_date": "Date the coupon is valid from, if provided.other wise mark it as null",
"expiration_date": "Date the coupon expires. Leave blank if not mentioned."
},
"coupon_code": "Extract the unique code found in the email. If unavailable, leave it blank.",
"description": "Provide the discount details (amount/percentage), minimum purchase, eligible products, retailer, any terms or restrictions, usage limit (single-use/multi-use), and where the coupon applies (in-store/online)."
}
"""
#7. metadata : For coupon receipts extract these data points as a JSON object exactly""" + coupon_data_points +
system_prompt = f"""Extract information from the following receipt OCR text,Strictly first analyse if the text actually is actually related to a purchase , insurance receipt , travel receipt , hotel receipt or a text having some coupon codes , If it is not a purchase or contains a coupon code then just return null strictly , and return a JSON object with these exact keys: brand, total_cost, location, purchase_category, brand_category, Date, currency, filename, payment_method, metadata.
Rules:
1. For total_cost, use the highest monetary value in the text.
2. For brand_category, choose the closest match from: ["Fashion and Apparel", "Jewelry and Watches", "Beauty and Personal Care", "Automobiles", "Real Estate", "Travel(it may contain reciepts of airlines , trains , taxi ,cruise ,etc)", "Hospitality(it will include reciepts of Hotels (stays) , restaurants , cafe's , bar's , Accommodation Services , Beverages Services (don't include food delivery service in hospitality))","Food Delivery Services(like swiggy , zomato,eatsure and any other you can analyse from receipt text)", "Home and Lifestyle", "Technology and Electronics", "Sports and Leisure", "Art and Collectibles", "Health and Wellness", "Stationery and Writing Instruments", "Children and Baby", "Pet Accessories","Insurance","Coupons(This may include Discounted Offers,Promo Coupons,Coupon Codes,Voucher Deals,Exclusive Discounts,Special Offers,Couponized Receipts,Promo Receipts,Coupon Tags,Discount Emails)"]
3. Format Date as dd-mm-yyyy.Strictly return the date in the format dd-mm-yyyy.
4. Check if the text includes the purchaser's name, receipt or transaction ID, relevant dates, amount paid, service details (insurance, travel, or hotel), and issuer information to verify if it's a valid insurance, travel, or hotel receipt. If you find that its just a promotion email ,don't assum its a travel , insurance or hotel receipt.
4. metadata: For insurance receipts extract the data points given in the JSON and return the JSON with structure: \n """ + insurance_response_structure + """
5.metadata : For travel receipts(flight ,bus,train) extract these data points as a JSON object exactly""" + travel_response_structure + """
6. metadata : For hotel receipts extract these data points as a JSON object exactly""" + hotel_data_points +f"""
For non-insurance and non-travel , non-hotel , non-coupon receipts, return metadata as null.
8. Use currency codes (e.g., USD, EUR) instead of symbols.
9. Generate filename as 'PURCHASE_TYPE_BRAND_DATE' (e.g., 'clothing_gucci_20230715').
10. If a value is not found, return null.
11. If all values are null, return null.
Ensure the strictly that output is a valid JSON object containing strictly the above keys, without any explanations.
Here's the OCR text below analyse it and convert into json using keys provided in first line and using the rules provided in rules section:
Generate a JSON response in the following format without using the ```json block. Ensure the output is properly formatted as plain text JSON.
{raw_text}
"""
return system_prompt
def parse_number(num_str):
# Remove commas and strip any unnecessary whitespace
cleaned_str = re.sub(r'[^\d.]', '', num_str.strip())
try:
# Convert to float
return float(cleaned_str)
except ValueError:
# Handle the case where the conversion fails
print(f"Error: {num_str} is not a valid number.")
return None
def adjust_prompt_tokens_v1(prompt: str) -> str:
max_tokens = 127500
encoding = tiktoken.encoding_for_model('gpt-4o-mini')
tokenized_prompt = encoding.encode(prompt)
# If token count exceeds max_tokens, trim it from the end while keeping full words
if len(tokenized_prompt) > max_tokens:
# Find the maximum index for the tokens that keeps the length within max_tokens
trimmed_tokens = tokenized_prompt[:max_tokens]
# Decode the trimmed tokens back to text
trimmed_text = encoding.decode(trimmed_tokens)
# Ensure we don't end up with a partial word; trim back to the last full word
last_space = trimmed_text.rfind(' ')
if last_space != -1:
trimmed_text = trimmed_text[:last_space]
else:
# If within the limit, no trimming needed
trimmed_text = prompt
return trimmed_text
async def process_synchronous_job(dataset: Dict[str, Any]):
"""
Background task to process the batch job
"""
for ds in dataset.get('data'):
user_id = ds.get('user_id')
message_id = ds.get('message_id')
raw_text = ds.get('receipt_text')
email = ds.get('email')
try:
prompt = adjust_prompt_tokens_v1(receipt_radar_prompt(raw_text))
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": prompt
}
],
response_format={"type": "json_object"},
temperature=0.1
)
print("Printing GPT response")
print(completion.choices[0].message)
# inserting data into supabase
insert_data = json.loads(completion.choices[0].message.content)
insert_data['email'] = email
insert_data['user_id'] = user_id
insert_data['message_id'] = message_id
if isinstance(insert_data['total_cost'], str) and ',' in insert_data['total_cost']:
insert_data['total_cost'] = parse_number(insert_data['total_cost'])
print("Printing user_id")
print(user_id)
insert_response = (
supabase.table("receipt_radar_structured_data_duplicate")
.insert(insert_data)
.execute()
)
update_status_response = (
supabase.table("receipt_ocr_data")
.update({"status": "processing completed"})
.eq("message_id", message_id)
.eq("user_id", user_id)
.eq("email", email)
.execute()
)
print(update_status_response)
except Exception as e:
print(f"Error occurred during processing: {e}")
# try:
# prompt = receipt_radar_prompt(raw_text)
# completion = client.chat.completions.create(
# model="gpt-4o-mini",
# messages=[
# {
# "role": "user",
# "content": prompt
# }
# ],
# response_format={"type": "json_object"},
# temperature=0.1
# )
# print("Printing GPT response")
# print(completion.choices[0].message)
# # inserting data into supabase
# insert_data = json.loads(completion.choices[0].message.content)
# insert_data['email'] = email
# insert_data['user_id'] = user_id
# insert_data['message_id'] = message_id
# insert_data['total_cost'] = parse_number(insert_data['total_cost'])
# print("Printing user_id")
# print(user_id)
# insert_response = (
# supabase.table("receipt_radar_structured_data_duplicate")
# .insert(insert_data)
# .execute()
# )
# update_status_response = (
# supabase.table("receipt_ocr_data")
# .update({"status": "processing completed"})
# .eq("message_id", message_id)
# .eq("user_id", user_id)
# .eq("email", email)
# .execute()
# )
# print(update_status_response)
# except Exception as e:
# print(f"Error occurred during processing: {e}")