
    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 ddlmZ  ej                  e      Z G d d	e      Zy)
z4
OpenAI API service for accessing LLM capabilities.
    N)DictListOptionalAnyUnion)settings)OpenAI   )
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 )OpenAIServicez6
    Service for interacting with the OpenAI API.
    api_keymodelc                     |xs t         j                  }|xs t         j                  }t        |   ||       t        | j                        | _        y)z
        Initialize the OpenAI service with API key and model.
        
        Args:
            api_key: OpenAI API key, defaults to settings.OPENAI_API_KEY
            model: OpenAI model to use, defaults to settings.OPENAI_MODEL
        )r   N)r   OPENAI_API_KEYOPENAI_MODELsuper__init__r	   r   client)selfr   r   	__class__s      4/home/cursorai/projects/iching/ai/services/openai.pyr   zOpenAIService.__init__   sD     4X44...%(T\\2    promptsystem_prompttemperature
max_tokenstop_preturnc                    g }|r|j                  d|d       |j                  d|d       	 dg}dg}dg}	| j                  |d}
| j                  |	vr||
d<   | j                  |vr||
d<   | j                  |v r||
d<   n||
d	<    | j                  j                  j                  j
                  di |
}|j                  d
   j                  j                  S # t        $ r'}t        j                  dt        |               d}~ww xY w)a  
        Get a text completion from the OpenAI 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userzgpt-5)r   messagesr   r   max_completion_tokensr   r   zError calling OpenAI API: N )appendr   r   chatcompletionscreatechoicesmessager#   	Exceptionloggererrorstr)r   r   r   r   r   r   r%   &models_requiring_max_completion_tokensmodels_with_fixed_temperaturemodels_not_supporting_top_p
api_paramsresponsees                r   get_completionzOpenAIService.get_completion   s)   &  OOX-HI 	F;<#	 7>Y2 .5I) ,3)' $J zz!<<&+
7# zz!>>,7
=)zzCC6@
23+5
<(:t{{''33::HZHH##A&..666 	LL5c!fX>?	s   B)C 	D!"DD	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 OPENAI_MODELS dictionary in settings
            
        Returns:
            True if successful, False otherwise
        OPENAI_MODELSTzModel key 'z%' not found in settings.OPENAI_MODELSF)getattrr   r   r/   warning)r   r9   modelss      r   change_modelzOpenAIService.change_model_   sB     ?B7	*DJNN[3XYZr   c                 $    t        t        di       S )z
        Get a dictionary of available models.
        
        Returns:
            Dictionary with model keys and their corresponding model IDs
        r;   )r<   r   )r   s    r   get_available_modelsz"OpenAIService.get_available_modelsq   s     x"55r   )NN)Ngffffff?i   g?)__name__
__module____qualname____doc__r   r1   r   floatintr8   boolr?   r   rA   __classcell__)r   s   @r   r   r      s    3 3Xc] 3 7;,/)-&)?"?&.sm? %*? $'	?
 $?
 /2?Bc d $6d38n 6r   r   )rE   osloggingtypingr   r   r   r   r   django.confr   openair	   llm_servicer   	getLoggerrB   r/   r   r'   r   r   <module>rQ      sA    
  3 3    #			8	$k6J k6r   