
    P1i                         d Z ddlZddlZddlmZmZmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ  ej                   e      Z G d d	e      Zy)
z2
Groq API service for accessing LLM capabilities.
    N)DictListOptionalAnyUnion)settings)Groq	AsyncGroq   )
LLMServicec                        e Zd ZdZddee   dee   f fdZ	 	 	 	 ddedee   deded	ed
efdZ	ded
e
fdZd
eeef   fdZ xZS )GroqServicez4
    Service for interacting with the Groq API.
    api_keymodelc                     |xs t         j                  }|xs t         j                  }t        |   ||       t        | j                        | _        y)z
        Initialize the Groq service with API key and model.
        
        Args:
            api_key: Groq API key, defaults to settings.GROQ_API_KEY
            model: Groq model to use, defaults to settings.GROQ_MODEL
        )r   N)r   GROQ_API_KEY
GROQ_MODELsuper__init__r	   r   client)selfr   r   	__class__s      2/home/cursorai/projects/iching/ai/services/groq.pyr   zGroqService.__init__   sD     2X22,,,%(4<<0    promptsystem_prompttemperature
max_tokenstop_preturnc                    g }|r|j                  d|d       |j                  d|d       	 | j                  j                  j                  j	                  | j
                  ||||      }|j                  d   j                  j                  S # t        $ r'}t        j                  dt        |               d}~ww xY w)a  
        Get a text completion from the Groq API.
        
        Args:
            prompt: The user prompt to send to the model
            system_prompt: Optional system prompt to set context
            temperature: Controls randomness (0-1), lower is more deterministic
            max_tokens: Maximum number of tokens to generate
            top_p: Nucleus sampling parameter
            
        Returns:
            The text response from the model
        system)rolecontentuser)r   messagesr   r   r   r   zError calling Groq API: N)appendr   chatcompletionscreater   choicesmessager$   	Exceptionloggererrorstr)	r   r   r   r   r   r   r&   responsees	            r   get_completionzGroqService.get_completion   s    &  OOX-HI 	F;<	{{''33::jj!'% ; H ##A&..666 	LL3CF8<=	s   A B 	B?"B::B?	model_keyc                 v    t        t        di       }||v r||   | _        yt        j	                  d| d       y)z
        Change the model being used by the service.
        
        Args:
            model_key: Key from the GROQ_MODELS dictionary in settings
            
        Returns:
            True if successful, False otherwise
        GROQ_MODELSTzModel key 'z#' not found in settings.GROQ_MODELSF)getattrr   r   r.   warning)r   r4   modelss      r   change_modelzGroqService.change_modelI   sB     ="5	*DJNN[3VWXr   c                 $    t        t        di       S )z
        Get a dictionary of available models.
        
        Returns:
            Dictionary with model keys and their corresponding model IDs
        r6   )r7   r   )r   s    r   get_available_modelsz GroqService.get_available_models[   s     x33r   )NN)Ngffffff?i   g?)__name__
__module____qualname____doc__r   r0   r   floatintr3   boolr:   r   r<   __classcell__)r   s   @r   r   r      s    1 1Xc] 1 7;,/)-&))")&.sm) %*) $'	)
 $)
 /2)Vc d $4d38n 4r   r   )r@   osloggingtypingr   r   r   r   r   django.confr   groqr	   r
   llm_servicer   	getLoggerr=   r.   r    r   r   <module>rM      sA    
  3 3     #			8	$U4* U4r   