
    #i                         S r SSKrSSKrSSKrSSKJrJrJrJr  SSK	J
r
Jr  SrSrSrS\
\S4S\S	\\   S
\S\S\\\\\4         S\\   4S jjr SS\S\S\\   S\4S jjrg)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TEMPERATUREzTxai-bYupNvIKS2S5I41aDUOKj7XSzoIitscspt9HvhesX0wK58Bnggck8gPAbK1jXvgc7PzKZJntt8HIheqEz$https://api.x.ai/v1/chat/completionszgrok-4-fast-reasoningpromptsystem_prompt
max_tokenstemperatureconversation_historyreturnc                    S[          3SS.n/ nU(       a  UR                  SUS.5        U(       a  UR                  U5        UR                  SU S.5        [        UUUSS.n [        R                  " 5       n[
        R                  " [        UUS	S
9n	[        R                  " 5       U-
  n
U	R                  S:X  aE  U	R                  5       nSU;   a   [        US   5      S:  a  US   S   S   S   $ [        SU 35        g[        SU	R                   SU	R                   35        g! [
        R                  R                   a)    [        S[        R                  " 5       W-
  S S35         g[
        R                  R                   a  n[        SU 35         SnAgSnAf[          a  n[        SU 35         SnAgSnAff = f)aQ  
Call Grok Fast API to generate a response

Args:
    prompt: The user prompt/question
    system_prompt: Optional system prompt for context
    max_tokens: Maximum tokens to generate
    temperature: Sampling temperature
    conversation_history: Optional conversation history
    
Returns:
    Generated response text or None if error
z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: NzGrok API Error: z - zGrok API Timeout after z.2fszGrok API Request Error: zGrok API Unexpected Error: )GROK_API_KEYappendextend
GROK_MODELtimerequestspostGROK_API_URLstatus_coder   lenprinttext
exceptionsTimeoutRequestException	Exception)r	   r
   r   r   r   r   r   payload
start_timeresponsedurationdataes                5/var/www/html/leadgen/backtest/airagagent/grok_api.pycall_grok_apir7      s   * #<.1*G
 H $
 	 ,- OO   "GYY[
==	
 99;+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   #BD& 1D&  %D& &AG,G	FG)F<<Gcontextquestionsystem_instructionsc                 h    U (       d  gU=(       d    SnSU  SU S3n[        UUSSS9nU=(       d    S	$ )
z
Generate an answer using Grok Fast API with RAG context

Args:
    context: Formatted RAG context from vector search
    question: User's question
    system_instructions: Optional system instructions
    
Returns:
    Generated answer
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.)r7   )r8   r9   r:   r
   user_promptanswers         r6   generate_answer_with_grokr>   h   sc      j ( 4 ,4M 		 
* K #	F VVV    )N)__doc__r%   r   r$   typingr   r   r   r   airagagent.configr   r   r    r'   r#   strintfloatr7   r>    r?   r6   <module>rG      s   
    , , 9 f5$
 $($$;?RRC=R R 	R
 #4S#X#78R c]Rp *.-W-W-W "#-W 		-Wr?   