
    MziQ                        d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZmZ ej                  j                  d      Zes	 es	 dZdZdeedfded	ee   d
ededee	eeef         dee   fdZ	 ddededee   defdZy)z_
Grok Fast API Integration
Switches RAG system from local LLM to Grok Fast Reasoning Model API
    N)OptionalDictAnyList)MAX_NEW_TOKENSTEMPERATUREGROK_API_KEYz$https://api.x.ai/v1/chat/completionszgrok-4-fast-reasoningpromptsystem_prompt
max_tokenstemperatureconversation_historyreturnc                    t         xs t        j                  j                  d      }|st	        d       yd| dd}g }|r|j                  d|d       |r|j                  |       |j                  d	| d       t        |||d
d}	 t        j                         }	t        j                  t        ||d      }
|
j                  dk(  rB|
j                         }d|v rt        |d         dkD  r|d   d   d   d   S t	        d|        yt	        d|
j                   d|
j                          y# t        j                   j"                  $ r( t	        dt        j                         	z
  dd       Y yt        j                   j$                  $ r}t	        d|        Y d}~yd}~wt&        $ r}t	        d|        Y d}~yd}~ww xY w)z3
    Call Grok Fast API to generate a response
    r	   zError: GROK_API_KEY not foundNzBearer zapplication/json)AuthorizationzContent-Typesystem)rolecontentuserF)modelmessagesr   r   stream-   )headersjsontimeout   choicesr   messager   z"Grok API: No choices in response: zGrok API Error: z - zGrok API Timeout after z.2fszGrok API Request Error: zGrok API Unexpected Error: )r	   osenvirongetprintappendextend
GROK_MODELtimerequestspostGROK_API_URLstatus_coder   lentext
exceptionsTimeoutRequestException	Exception)r
   r   r   r   r   api_keyr   r   payload
start_timeresponsedataes                3/var/www/html/eventheodds/ai-lawyer-rag/grok_api.pycall_grok_apir:      s    <bjjnn^<G-. #7),*G
 H $
 	 ,- OO   "GYY[
==	
 3&==?DD Si%9A%=Iq))4Y??:4&AB$X%9%9$:#hmm_MN&& '		j(@'EQGH// (,- +A3/0s8   
A2D2 =D2 %D2 2AG	6G	F%%G	1GG	contextquestionsystem_instructionsc                 .   	 t         j                  j                  t         j                  j                  t                    }t         j                  j                  |d      }t         j                  j                  |      r?t        |dd      5 }|j                         }ddd       rt        |      dkD  rd| d|  } | sy
|xs d}d|  d| d}	t        |	|dd      }
|
xs dS # 1 sw Y   HxY w# t        $ r}t        d	|        Y d}~Od}~ww xY w)zA
    Generate an answer using Grok Fast API with RAG context
    znfl_context_2025.txtrzutf-8)encodingN2   z/=== LIVE REAL-TIME DATA (2025-2026 SEASON) ===
z

=== RETRIEVED DOCUMENTS ===
z(Warning: Failed to inject live context: zYI don't have enough information to answer that question based on the available documents.a  You are a helpful AI assistant that answers questions based on provided context.
    - Use only the information provided in the context
    - If the context doesn't contain relevant information, say so explicitly
    - Be concise and accurate
    - For technical questions (pH, temperature, measurements), extract specific numbers/ranges
    - Do NOT provide generic information if the specific answer isn't in the context
    - Cite sources when mentioned in the contextzFBased on the following context, answer the user's question.

CONTEXT:
z

QUESTION: z	

ANSWER:i   gffffff?)r
   r   r   r   z?I encountered an error generating a response. Please try again.)r!   pathdirnameabspath__file__joinexistsopenreadr-   r2   r$   r:   )r;   r<   r=   base_dirlive_context_pathf	live_datar8   r   user_promptanswers              r9   generate_answer_with_grokrP   m   s4   > 77??277??8#<=GGLL3IJ77>>+,'w? %1FFH	% S^b0LYKWx  zA  yB  C j ( 4 ,4M 		 
* K #	F VVVQ% %  >8<==>s0   BC3 C'  C3 'C0,C3 3	D<DD)N)__doc__r)   r   r(   r!   typingr   r   r   r   configr   r   r"   r#   r	   r+   r'   strintfloatr:   rP        r9   <module>rY      s   
    	 , , . zz~~n-
 		5$

 $($$;?LLC=L L 	L
 #4S#X#78L c]Ld *.9W9W9W "#9W 		9WrX   