
    7|hq                         d dl Z d dlZd dlmZmZmZmZmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZmZ d dlmZmZmZmZ d dlmZ  e j0                  e      Z G d d	e      Zy)
    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)LLM)convert_to_secret_strget_from_dict_or_envpre_init)
ConfigDictField	SecretStrmodel_validator)enforce_stop_tokensc                   0   e Zd ZU dZdZeed<   	  ee      Z	e
eef   ed<   	 dZee   ed<    ed	      Z ed
      ede
eef   defd              Zede
de
fd       Zedeeef   fd       Zedefd       Z	 	 ddedeee      dee   dedef
dZy)StochasticAIa2  StochasticAI large language models.

    To use, you should have the environment variable ``STOCHASTICAI_API_KEY``
    set with your API key.

    Example:
        .. code-block:: python

            from langchain_community.llms import StochasticAI
            stochasticai = StochasticAI(api_url="")
     api_url)default_factorymodel_kwargsNstochasticai_api_keyforbid)extrabefore)modevaluesreturnc                 :   t        t        | j                  j                                     }|j	                  di       }t        |      D ]I  }||vs||v rt        d| d      t        j                  | d| d       |j                  |      ||<   K ||d<   |S )z>Build extra kwargs from additional params that were passed in.r   zFound z supplied twice.zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.)	setlistmodel_fieldskeysget
ValueErrorloggerwarningpop)clsr   all_required_field_namesr   
field_names        d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/stochasticai.pybuild_extrazStochasticAI.build_extra*   s     $'tC,<,<,A,A,C'D#E 

>2.v, 	;J!99&$vj\9I%JKK# %))34JN %+JJz$:j!	; "'~    c                 <    t        t        |dd            }||d<   |S )z,Validate that api key exists in environment.r   STOCHASTICAI_API_KEY)r
   r   )r)   r   r   s      r,   validate_environmentz!StochasticAI.validate_environment=   s/      5 )?AWX 
 *>%&r.   c                 >    i d| j                   id| j                  iS )zGet the identifying parameters.endpoint_urlr   )r   r   selfs    r,   _identifying_paramsz StochasticAI._identifying_paramsF   s0    
t||,
t001
 	
r.   c                      y)zReturn type of llm.stochasticai r4   s    r,   	_llm_typezStochasticAI._llm_typeN   s     r.   promptstoprun_managerkwargsc                 8   | j                   xs i }i ||}t        j                  | j                  ||d| j                  j                          ddd      }|j                          |j                         }d}|st        j                  |d   d   | j                  j                          ddd      }	|	j                          |	j                         d   }
|
j                  d	      }|d
u}t        j                  d       |sd   }|t        ||      }|S )at  Call out to StochasticAI's complete endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = StochasticAI("Tell me a joke.")
        )r;   paramszapplication/json)apiKeyAcceptzContent-Type)urljsonheadersFdataresponseUrl)rC   rE   
completionNg      ?r   )r   requestspostr   r   get_secret_valueraise_for_statusrD   r$   timesleepr   )r5   r;   r<   r=   r>   r@   response_postresponse_post_json	completedresponse_getresponse_get_jsontexts               r,   _callzStochasticAI._callS   s0   * ""(b%F%f% "f5!66GGIJ, 2
 	&&(*//1	#<<&v.}=!%!:!:!K!K!M N0$6L ))+ , 1 1 3F ;$((6DD(IJJsO  Aw 'tT2Dr.   )NN)__name__
__module____qualname____doc__r   str__annotations__r   dictr   r   r   r   r   r   r   model_configr   classmethodr-   r   r1   propertyr   r6   r:   r   r   rU   r9   r.   r,   r   r      s5   
 GS#(#>L$sCx.> 15(9-4L (#c3h C   $" $ 4   
WS#X%6 
 
 3   %):>	66 tCy!6 67	6
 6 
6r.   r   )loggingrM   typingr   r   r   r   r   rI   langchain_core.callbacksr   #langchain_core.language_models.llmsr	   langchain_core.utilsr
   r   r   pydanticr   r   r   r   langchain_community.llms.utilsr   	getLoggerrV   r&   r   r9   r.   r,   <module>rh      sI      5 5  = 3 V V B B >			8	$y3 yr.   