
    &h0                         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
  ej        ej                    e
              ej        d e j        d                    Zd	Zd
 ZdS )    N)BotoCoreErrorClientError)get_db_connection)load_dotenv)levelzbedrock-runtime
AWS_REGION)service_nameregion_namezRarn:aws:bedrock:eu-north-1:003900767884:inference-profile/eu.amazon.nova-lite-v1:0c                 

   t                      }|                                }|                    d| f           |                                }|D ]}|d         }d| d}t	          j        d|  d|            ddd	d
dd|igdgd}	 t	          j        dt          j        |                      t          	                    t          ddt          j        |                    }|d                                                             d          }	t	          j        d|  d|	            d}
|	rt          j        |	          }|                    di                               di                               dg           }|rCt          |t                     r.|d                             dd                                          }
|
s)t	          j        d|             t'          d|            |
                    d          r=|
                    dd                              dd                                          }
n=|
                    d          r(|
                    dd                                          }
|
                    dd                                          }
t	          j        d|  d|
            	 t          j        |
          }nV# t          j        $ rD}t	          j        d |  dt/          |                      t	          j        d!|
             d }~ww xY wt          j        |                    d"g                     }t          j        |                    d#g                     }t          j        |                    d$g                     }|                    d%d          }t          j        |                    d&g                     }t	          j        d'|  d(| d)| d*| d+| d,|            |                    d-|||||
||| f           |                                 t	          j        d.|             # t2          t4          t          j        t&          f$ r[}t	          j        d/|  dt/          |                      t	          j        d0dt7                      v r|
nd1            Y d }~d }~ww xY w|                                 |                                 d S )2NzBSELECT * FROM 6840_sarvamresponse WHERE callid = %s AND status = 1   a  
                        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)
                       

                        Transcript:
                        "z"  
                    zSending prompt for callid z: 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Parsed data for callid z: Keywords: z, Sentiment: z, Emotions: z, Summary: z,slaes:a\  
                UPDATE 7417_calls SET
                status = 2,
                keywords = %s,
                sentiments = %s,
                emotions = %s,
                summary = %s,
                sand_logic_response = %s,
                sales_intent = %s,
                transcripts = %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jsondumpsbedrockinvoke_modelMODEL_IDreaddecodeloadsget
isinstanceliststriperror
ValueError
startswithreplaceJSONDecodeErrorstrcommitr   r   localsclose)call_idconnr   callscall
transcriptpromptr   responseraw_responser   response_bodycontent_listparsedekeywords	sentimentemotionssummarysalesintents                       -C:\wamp64\www\rabbitmqsarvam\openai_helper.pysend_openai_analysisrJ   d   s   D[[]]F
NNWZaYcdddOOE `t `t!W
 &   	E'EEVEEFFF #'"    # &/0 
 
A	tL<$*T*:*:<<===++ .)Z%%	 ,  H $F+002299'BBLLRRRLRRSSSG F $
< 8 8,002>>BB9bQQUUV_acdd FJ|T$B$B F*1o11&"==CCEEG gL7LLMMM !e\c!e!efff!!),, =!//)R88@@KKQQSS##E** =!//%44::<<oodB//5577GLTTT7TTUUUG,,'   QgQQQQQRRR;';;<<<
 z&**-=r"B"BCCH
6::.A2#F#FGGIz&**-@""E"EFFHjj!6;;G*VZZ%C%CDDKL  i7  i  i  i  i_h  i  iv~  i  i  LS  i  i  \g  i  i  j  j  jNN  Ix';PZ\cdf f f KKMMMLBBBCCCC{D,@*M 	t 	t 	tMM'MMSVVMMNNNMryFHH?T?TGGZprrssssssss	t LLNNNJJLLLLLs?   H<Q	KQL1-?L,,L11D(Q"S=ASS)osr#   r!   boto3botocore.exceptionsr   r   	db_configr   dotenvr   basicConfigINFOclientgetenvr%   r'   rJ        rI   <module>rV      s   b 
			    : : : : : : : : ' ' ' ' ' '        ', ' ' ' ' 
%,"	,''  
 `j j j j jrU   