
    h."                     J   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
 d dlmZ  ej                  ej                          e
         e j                  dd      Z e j                  d	d
      Z e j                  dd      Z ej&                  deee      ZdZddZy)    N)BotoCoreErrorClientError)get_db_connection)load_dotenv)send_message)levelAWS_ACCESS_KEY_IDAKIAUWPMKLMSD556YYR5AWS_SECRET_ACCESS_KEY(yOku358ASGrJPRdTWYfyKnUAVH6QN1qlR2fQO7KD
AWS_REGIONz
eu-north-1zbedrock-runtime)service_nameregion_nameaws_access_key_idaws_secret_access_keyzamazon.nova-lite-v1:0c                 H
   t               }|j                         }|r| d}| d}nd}d}|j                  d| d| f       |j                         }|D ]  }|d   }t	        j
                  d|  d	|r|d d
 nd d       |r|j                         dk(  rt	        j                  d|         Zd| d}	t	        j
                  d|  d	|	        dddddd|	igdgd}
	 t	        j
                  dt        j                  |
              t        j                  t        ddt        j                  |
            }|d   j                         j                  d      }t	        j
                  d|  d	|        d}|r|t        j                  |      }|j!                  d i       j!                  d!i       j!                  d"g       }|r3t#        |t$              r#|d#   j!                  dd      j                         }|s&t	        j&                  d$|         t)        d%|        |j+                  d&      r1|j-                  d&d      j-                  d'd      j                         }n1|j+                  d'      r |j-                  d'd      j                         }|j-                  d(d      j                         }t	        j
                  d)|  d	|        	 t        j                  |      }t        j                  |j!                  d,g             }t        j                  |j!                  d-g             }t        j                  |j!                  d.g             }|j!                  d/d      }t        j                  |j!                  d0g             }t        j                  |j!                  d1i             }t	        j
                  d2|  d3| d4| d5| d6| d7| d8|        |j                  d9| d:||||||||| f	       |j3                          t5        | |       t	        j
                  d;|          |j=                          |j=                          y # t        j.                  $ rB}t	        j&                  d*|  d	t1        |              t	        j&                  d+|         d }~ww xY w# t6        t8        t        j.                  t(        f$ rU}t	        j&                  d<|  d	t1        |              t	        j&                  d=d"t;               v rnd>        Y d }~d }~ww xY w)?N_sarvamresponse_calls7417_sarvamresponse
7417_callszSELECT * FROM z! WHERE callid = %s AND status = 1   u%   🔍 Retrieved transcript for callid z: d   Nonez... u+   ⚠️ Empty or null transcript for callid aE  
                        Analyze the following customer call transcript and return the following in JSON format:
                        1. Top 5 Keywords
                        2. Overall Sentiment with all (Positive, Negative, Neutral) with percentage 
                        3. Detected Emotions (e.g., Happy, Angry, Frustrated)
                        4. Summary of the Call
                        5. Sales Intent (High, Medium, Low)
                        6. Customer Details (name, phone, email if available)

                        Transcript:
                        "z"  
                    zSending prompt for callid i  gffffff?g?)max_new_tokenstemperaturetop_pusertext)rolecontent)inferenceConfigmessageszRequest body: zapplication/json)modelIdcontentTypeacceptbodyr'   zutf-8zRaw response body for callid outputmessager!   r   z"Empty content received for callid z8Empty content received from Bedrock response for callid z```jsonz```
z$Cleaned response content for callid zJSON decode error for callid zInvalid content: zTop 5 KeywordszOverall SentimentzDetected EmotionszSummary of the CallzSales IntentzCustomer DetailszParsed data for callid z: Keywords: z, Sentiment: z, Emotions: z, Summary: z	, Sales: z, Customer: z
                UPDATE aa   SET
                status = 2,
                keywords = %s,
                sentiments = %s,
                emotions = %s,
                summary = %s,
                sand_logic_response = %s,
                sales_intent = %s,
                transcripts = %s,
                customer_details = %s
                WHERE callid = %s
            u   ✅ Bedrock processed callid u   ❌ Bedrock error for callid u   ⚠️ Failed content: zNo content available)r   cursorexecutefetchalllogginginfostripwarningjsondumpsbedrockinvoke_modelMODEL_IDreaddecodeloadsget
isinstancelisterror
ValueError
startswithreplaceJSONDecodeErrorstrcommitr   r   r   localsclose)call_idbidconnr+   sarvam_response_tablecalls_tablecallscall
transcriptpromptr'   responseraw_responser!   response_bodycontent_listparsedekeywords	sentimentemotionssummarysalesintentcustomer_detailss                           CC:\Users\pc\OneDrive\Desktop\local2\rabbitmqsarvam\openai_helper.pysend_openai_analysisr\   k   s0   D[[]F #&% 7Vn 5"
NN^$9#::[\_f^hiOOE!W
 	<WIR\f
SWTWHXlrGssvwxZ--/25OOI'ST
 &, ' 	1'"VHEF #'"  # &/0
D	tLL>$**T*:);<=++ .)ZZ%	 , H $F+00299'BLLL8	L>RSG $

< 8,002>BB9bQUUV_acdJ|T$B*1o11&"=CCEG B7)LM #[\c[d!eff!!),!//)R8@@KQQS##E*!//%4::<oodB/557GLL?y7)TUG, zz&**-=r"BCH

6::.A2#FGIzz&**-@""EFHjj!6;G**VZZ%CDK#zz&**5G*LMLL27)<zQ^_h^iiuv~u  @K  LS  KT  T]  ^i  ]j  jv  wG  vH  I  JNN  #} % Ix';PZ\lnuvx KKM#&LL8	BCM X LLNJJLK ''  =gYbQQR 1';<@ {D,@,@*M 	tMM9'"SVHMNMM3yFH?TGZp3qrss	ts?   !G.R)Q%ER)R&$=R!!R&&R))#T!A
TT!)N)osr2   r.   boto3botocore.exceptionsr   r   	db_configr   dotenvr   message_helperr   basicConfigINFOgetenvr	   r   r   clientr4   r6   r\        r[   <module>ri      s   b 
    : '  '   ',, '  BII13IJ !		"9;ef RYY|\2

%,,"'/	 #|rh   