o
    j j                     @   st   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 e	dd ee 
dd	Zd
ZdedefddZdS )    N)Path)OpenAI)read_file_content)load_dotenvT)overrideOPENAI_API_KEY)api_keya  You are a pharma document classifier.

Classify the uploaded document based on its content.

Return JSON only with these exact fields:
{
  "filename": "<filename>",
  "document_type": one of ["SOP", "Batch Record", "Equipment Log", "LIMS Results", "Maintenance Log", "Deviation Report", "Unknown"],
  "document_type_code": one of ["SOP", "BMR", "EQP", "LIMS", "MAINT", "DEV", "UNKNOWN"],
  "confidence": number between 0 and 100,
  "batch_id": string or null,
  "product_name": string or null,
  "equipment_id": string or null,
  "plant": string or null,
  "date_range": string or null,
  "key_fields_found": array of strings,
  "missing_context": array of strings
}

Document filename: <filename>
Document content:
<content>
	file_pathreturnc                    sD  zbt | }td| j}|d|d d }tjjjddddd|dgd	d
idd}|jd j	j
}t|}| j|d< d|d< |dd}|dkrRd|d< |W S |dkr]d|d< |W S d|d< |W S  tjy } z| jdddddt|dW  Y d }~S d }~w ty } z| jdddddt|dW  Y d }~S d }~ww )Nz
<filename>z	<content>i  zgpt-4osystemzFYou are a pharma document classifier. Return JSON only. No extra text.)rolecontentusertypejson_objectr   )modelmessagesresponse_formattemperaturefilename
classifiedstatus
confidenceU   VERIFIEDverification_status<   zNEEDS REVIEWzLOW CONFIDENCEUnknownUNKNOWNparse_error)r   document_typedocument_type_coder   r   r   errorr"   )r   CLASSIFY_PROMPTreplacenameclientchatcompletionscreatechoicesmessager   jsonloadsgetJSONDecodeErrorstr	Exception)r	   r   promptresponserawresultr   e r7   ,/var/www/html/fyndo/pharma/fyndo/classify.pyclassify_document$   sf   


	r9   )osr,   pathlibr   openair   file_readerr   dotenvr   getenvr&   r#   dictr9   r7   r7   r7   r8   <module>   s    
