U
    Jje                      @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlZe  ee ddZ	dddgZ
d	Zd
Zdd ZdddZdddZdd Zdd Zdd Zdd Zdd Zdd ZdS )     N)Groq)load_dotenvZGROQ_API_KEY)Zapi_keyzllama-3.3-70b-versatilezllama-3.1-8b-instantzgemma2-9b-itZ	social_v1Zinternal_v1c                 C   sh   zt | W S  tk
rb   z,td| tj}|rFt | W  Y S W n tk
r\   Y nX Y nX g S )z6
    Strict JSON parser with fallback extraction.
    z\[.*\])jsonloads	ExceptionresearchDOTALLgroup)textmatch r   J/var/www/api.dyntland.com/public_html/python_social_comments/llm_client.pysafe_json_parse'   s    r   皙?c           	      C   s   d }t D ]}zRtd|  tjjj|d| dg|d}|jd jj	 }t
|}||fW   S  tk
r } zHt| }d|krtd|  ntd| d	|  |}W Y qW 5 d }~X Y qX q|d S )
Nz[LLM] Trying model: user)rolecontent)modelmessagestemperaturer   z
rate limitz[RATE LIMIT] model=z[LLM ERROR] model=z error=)MODEL_VERSIONSprintclientZchatZcompletionscreatechoicesmessager   stripr   r   strlower)	promptr   Z
last_errorr   responser   parsedeZ
error_textr   r   r   execute_with_fallback:   s,    	r$   c              
   C   sh   z&t | |\}}td|  ||fW S  tk
rb } ztd|  g d f W Y S d }~X Y nX d S )Nz[LLM SUCCESS] model=z[LLM ERROR] )r$   r   r   )r    r   r"   
used_modelr#   r   r   r   call_llmv   s    
r&   c              	   C   s>   d dd t| D }d| d| d| d| d	}t|S )	N
c                 S   s"   g | ]\}}|d   d| qS    z. r   .0icr   r   r   
<listcomp>   s     z1analyze_social_comments_batch.<locals>.<listcomp>zc
You are a strict JSON generator.

TASK:
Classify comments in a debate context.

DEBATE:
Question: z
Option A: z
Option B: 

COMMENTS:
u*  

RULES:
- Return ONLY valid JSON array
- No explanation
- No markdown
- No extra text
- Output MUST match schema exactly
- For "side": always output the exact text of Option A or Option B — NEVER output "Neutral" or "A" or "B"
- If a comment seems neutral, pick the option it leans toward most

OUTPUT FORMAT:
[
  {
    "index": 1,
    "side": "exact text of Option A or Option B",
    "confidence": 0.0-1.0,
    "sentiment": "Positive|Negative|Neutral",
    "emotion": "anger|joy|sadness|fear|surprise|disgust|neutral|frustration|hope|sarcasm"
  }
]
join	enumerater&   )comments_batchquestionoption_aoption_bformatted_commentsr    r   r   r   analyze_social_comments_batch   s    	#r8   c                 C   s,   d dd t| D }d| d}t|S )Nr'   c                 S   s"   g | ]\}}|d   d| qS r(   r   r*   r   r   r   r.      s     z2analyze_comments_batch_generic.<locals>.<listcomp>z
You are a strict JSON generator.

TASK:
Analyze generic social comments.

RULES:
- Return ONLY valid JSON array
- No explanation
- No markdown
- No extra text
- Output MUST match schema exactly

COMMENTS:
a
  

OUTPUT FORMAT:
[
  {
    "index": 1,
    "confidence": 0.0-1.0,
    "sentiment": "Positive|Negative|Neutral",
    "emotion": "anger|joy|sadness|fear|surprise|disgust|neutral|frustration|hope|sarcasm",
    "toxicity_score": 0.0-1.0,
    "language_code": "en"
  }
]
r0   r3   r7   r    r   r   r   analyze_comments_batch_generic   s    r:   c                 C   sL   d dd t| D }d dd |D }d| d| d| d}t|S )	Nr'   c                 S   s"   g | ]\}}|d   d| qS r(   r   r*   r   r   r   r.      s     z/analyze_poll_comments_batch.<locals>.<listcomp>c                 S   s   g | ]}d | qS )z- r   )r+   optr   r   r   r.      s     zf
You are a strict JSON generator.

TASK:
Classify comments based on a poll question.

POLL:
Question: z

Options:
r/   u  

RULES:
- Return ONLY valid JSON array
- No explanation
- No markdown
- No extra text
- Output MUST match schema exactly
- For "side": always output the exact text of one of the options above — pick the closest match

OUTPUT FORMAT:
[
  {
    "index": 1,
    "side": "exact text of one poll option",
    "confidence": 0.0-1.0,
    "sentiment": "Positive|Negative|Neutral",
    "emotion": "anger|joy|sadness|fear|surprise|disgust|neutral|frustration|hope|sarcasm"
  }
]
r0   )r3   r4   optionsr7   Zformatted_optionsr    r   r   r   analyze_poll_comments_batch   s    	"r=   c                 C   s,   d dd t| D }d| d}t|S )Nr'   c                 S   s"   g | ]\}}|d   d| qS r(   r   r*   r   r   r   r.     s     z3analyze_internal_comments_batch.<locals>.<listcomp>zT
You are a strict JSON generator.

TASK:
Analyze internal user comments.

COMMENTS:
a  

RULES:
- Return ONLY valid JSON array
- No explanation
- No markdown
- No extra text
- Output MUST match schema exactly

FORMAT:
[
  {
    "index": 1,
    "confidence": 0.0-1.0,
    "sentiment": "Positive|Negative|Neutral",
    "emotion": "anger|joy|sadness|fear|surprise|disgust|neutral|frustration|hope|sarcasm",
    "toxicity": 0.0-1.0,
    "language_code": "en",
    "impact_score": 0.0-1.0
  }
]
r0   r9   r   r   r   analyze_internal_comments_batch  s    r>   c                   C   s   t S N)PROMPT_VERSION_SOCIALr   r   r   r   get_social_prompt_version0  s    rA   c                   C   s   t S r?   )PROMPT_VERSION_INTERNALr   r   r   r   get_internal_prompt_version3  s    rC   )r   )r   )osr   r   Zgroqr   dotenvr   sysgetenvr   r   r@   rB   r   r$   r&   r8   r:   r=   r>   rA   rC   r   r   r   r   <module>   s,   
<
,'-+