
    i"                         U d Z ddlZddlZddlZddlmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ  G d	 d
      Zdaee   ed<   defdZdedefdZy)z_
LLM Client Module

Provides HTTP and mock inference backends for language model interactions.
    N)Optional   )LLM_MODELLM_HTTP_URLLLM_TIMEOUT_SECLLM_MAX_TOKENSLLM_FALLBACK_TO_MOCKlog_llm   )guess_intent)LLM_SYSTEM_PROMPTc                   h    e Zd ZdZeeeeefde	de	de
dedef
dZded	efd
Zded	efdZded	efdZy)	LLMClientz
    LLM inference client supporting HTTP and mock modes.

    Supports automatic fallback to mock responses when HTTP fails.
    modehttp_urltimeout_sec
max_tokensfallback_to_mockc                 J    || _         || _        || _        || _        || _        y)aO  
        Initialize LLM client.

        Args:
            mode: Inference mode ('http' or 'mock')
            http_url: URL for HTTP inference endpoint
            timeout_sec: Request timeout in seconds
            max_tokens: Maximum response tokens
            fallback_to_mock: Whether to fallback to mock on HTTP failure
        N)r   r   r   r   r   )selfr   r   r   r   r   s         ,/home/sas/my/fyp/ringai/ringai/llm/client.py__init__zLLMClient.__init__   s)    $ 	 &$ 0    payloadreturnc                 d   | j                   dk(  r| j                  |      S | j                   dk(  r	 | j                  |      S dd| j                    iS # t        $ rS}t	        j
                  d|       | j                  r+t	        j                  d       | j                  |      cY d}~S  d}~ww xY w)a  
        Run LLM inference.

        Args:
            payload: Request payload containing:
                - system_prompt: System prompt string
                - history: List of conversation messages
                - latest_user_utterance: Current user input

        Returns:
            Response dict containing:
                - assistant_reply: Generated response text
                - intent: Detected intent
                - needs_clarification: Whether clarification is needed
        mockhttpzHTTP inference failed: %rzFalling back to mock inferenceNerrorzUnsupported mode: )r   _infer_mock_infer_http	Exceptionr
   r   r   warning)r   r   es      r   inferzLLMClient.infer7   s      99##G,,99''00 -dii[9::  91=((OO$DE++G44s$   A 	B/AB*#B/)B**B/c                    d|j                  dt              dg}|j                  dg       D ]  }|j                  |d   |d   d        d|d| j                  d	d
id}d}| j                  |z  }t        |      D ]A  }	 t        j                  j                  | j                  t        j                  |      j                  d      ddid      }t        j                  j                  ||      5 }	|	j                         j                  dd      }
ddd       t        j                   
      }|j                  d      }|rt#        |t$              st'        d|       |d   j                  di       j                  dd      j)                         }	 t        j                   |      }d|vrd|v r|d   |d<   |c S  d"d#iS # 1 sw Y   xY w# t        j*                  $ r |dddcY c S w xY w# t        j,                  j.                  t0        f$ r/}||d z
  k  r!t3        j4                  d!|d z   ||       Y d}~ d}~ww xY w)$z+HTTP inference via llama.cpp compatible APIsystemsystem_prompt)rolecontenthistoryr)   r*   qweng?typejson_object)modelmessagestemperaturer   response_formatr   zutf-8zContent-Typezapplication/jsonPOST)dataheadersmethod)timeoutreplace)errorsNchoiceszInvalid LLM response: r   message assistant_replyquestionFunknown)r=   needs_clarificationintentr   z*HTTP attempt %d/%d failed: %r, retrying...r   zAll retry attempts failed)getr   appendr   r   rangeurllibrequestRequestr   jsondumpsencodeurlopenreaddecodeloads
isinstancelist
ValueErrorstripJSONDecodeErrorr   URLErrorTimeoutErrorr
   r#   )r   r   r0   mbodymax_retriestimeout_per_tryattemptreqresprawr4   r:   r*   parsedr$   s                   r   r!   zLLMClient._infer_httpV   sd    '++oGX*YZ
 Y+ 	AOO&	Y< 	  // &6
 **[8[) -	G,nn,,MMD)009+-?@!	 -  ^^++C+I HT))+,,WY,GCH zz#((9-j$&?$'=dV%DEE AJSB'SB'UW	 
!ZZ0F(6:;O4::4F01!M;-	^ 455MH H* ++ +2/4"+  LL))<8 [1_,OOD![! sV   A2G23"GBG2&GG	
G2G/*G2.G//G22#I#H?>H??Ic                    |j                  d      xs dj                         }|j                         t              }d}d}d}|dk(  rd}d}d}n|dk(  rd	}d
}n|dk(  rd}d
}n|dk(  rdv sdv sdv rd}nd}d
}nn|dk(  rd}d
}nd|dk(  rd}d
}nZ|dk(  rd}d
}nP|dk(  rdv sdv rd}d}n>d}d
}n9|dk(  rd}d
}n/|d k(  rd!}d
}n%|d"k(  rd#}d
}nt	        fd$d%D              rd&}nd'}d
}|||||d(S ))z,Mock inference using keyword-based responseslatest_user_utterancer<   Flog_onlyclosingz!You're welcome! Have a great day.end_callerp_inquiryzDGot it! Are you interested in our cloud ERP or on-premise solutions?Tcrm_inquiryzVPerfect! Our CRM handles sales, marketing, and support. Which area interests you most?crm_featureslead
conversionopportunityzVGreat! Our CRM tracks full lead-to-close pipeline with automated scoring. Want a demo?zZOur CRM includes lead scoring, pipeline management, and forecasting. What's your priority?pricing_inquiryzTOur plans start at $99/user/month for CRM, $149 for ERP. How many users do you have?implementationz`We offer full implementation support with training. Timeline is typically 4-8 weeks. Sound good?demo_requestzBI'd love to schedule a demo! What's your preferred time this week?introductionerpcrmzIThanks for calling! I can help with that. What specific info do you need?z<Thanks for calling! Are you interested in ERP, CRM, or both?connectivity_issuezJI understand. Is your internet not working at all, or just running slowly?account_access_issuez<Got it. Can you tell me what happens when you try to log in?system_issuez1I see. Can you describe exactly what's happening?c              3   &   K   | ]  }|v  
 y w)N ).0kts     r   	<genexpr>z(LLMClient._infer_mock.<locals>.<genexpr>   s     Qa16Qs   )wantneedzlooking for
interestedzAI can help with ERP, CRM, pricing, or demos. Which interests you?zDI'm here to help. Are you calling about ERP, CRM, or something else?)normalized_textrA   r@   r=   action)rB   rR   lowerr   any)r   r   textrA   r@   r=   r}   rw   s          @r   r    zLLMClient._infer_mock   s   34:AACJJLa# YAO"'F}$dO"&}$vO"&~%{la/=A3E"z"~"&((tO"&'' AO"&~%bO"&~%zUaZ"m&+#"`&*#++jO"&--\O"&~%QO"& Q#PQQ"e"h"&  $#6.
 	
r   N)__name__
__module____qualname____doc__r   r   r   r   r	   strfloatintboolr   dictr%   r!   r    rt   r   r   r   r      s     $,(!511 1 	1
 1 10;T ;d ;>G64 G6D G6RM
4 M
D M
r   r   _clientr   c                  .    t         
t               a t         S )zGet global LLM client instance)r   r   rt   r   r   
get_clientr      s     +Nr   r   c                 4    t               j                  |       S )zS
    Convenience function for LLM inference.

    Uses global client instance.
    )r   r%   )r   s    r   	llm_inferr      s     <g&&r   )r   rH   urllib.requestrE   urllib.errortypingr   configr   r   r   r   r	   r
   rA   r   promptsr   r   r   __annotations__r   r   r   rt   r   r   <module>r      sg         ! &T
 T
p  $)	 #I 't ' 'r   