U
    h                     @   s   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mZmZ d dlmZmZmZ d dlmZ e eZG dd	 d	e
ZdS )
    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)LLM)Field	SecretStrroot_validator)convert_to_secret_strget_from_dict_or_envpre_init)
get_fieldsc                   @   sx  e Zd ZU dZeed< dZeed< dZe	ed< dZ
eed< d	Ze	ed
< d	Zeed< dZe	ed< dZe	ed< d	Zeed< eedZeeef ed< eedZeeee	f  ed< dZee ed< G dd dZeddeeef eeef dddZeeedddZeeeef ddd Zee eef dd!d"Z!eedd#d$Z"d(eee#e  ee$ eed%d&d'Z%dS ))GooseAIa  GooseAI large language models.

    To use, you should have the ``openai`` python package installed, and the
    environment variable ``GOOSEAI_API_KEY`` set with your API key.

    Any parameters that are valid to be passed to the openai.create call can be passed
    in, even if not explicitly saved on this class.

    Example:
        .. code-block:: python

            from langchain_community.llms import GooseAI
            gooseai = GooseAI(model_name="gpt-neo-20b")

    clientzgpt-neo-20b
model_namegffffff?temperature   
max_tokens   top_p
min_tokensr   frequency_penaltypresence_penaltyn)default_factorymodel_kwargs
logit_biasNgooseai_api_keyc                   @   s   e Zd ZdZdS )zGooseAI.ConfigignoreN)__name__
__module____qualname__extra r%   r%   D/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/gooseai.pyConfigB   s   r'   T)pre)valuesreturnc              
   C   s   dd t |  D }|di }t|D ]P}||kr*||krNtd| dtd| d| d| d	 ||||< q*||d< |S )
z>Build extra kwargs from additional params that were passed in.c                 S   s   h | ]
}|j qS r%   )alias).0fieldr%   r%   r&   	<setcomp>H   s     z&GooseAI.build_extra.<locals>.<setcomp>r   zFound z supplied twice.z	WARNING! z/ is not default parameter.
                    zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.)r   r)   getlist
ValueErrorloggerwarningpop)clsr)   Zall_required_field_namesr$   
field_namer%   r%   r&   build_extraE   s    zGooseAI.build_extrac                 C   s`   t t|dd}||d< z&ddl}| |_d|_|j|d< W n tk
rZ   tdY nX |S )z?Validate that api key and python package exists in environment.r   ZGOOSEAI_API_KEYr   Nzhttps://api.goose.ai/v1r   zTCould not import openai python package. Please install it with `pip install openai`.)r   r   openaiZget_secret_valueZapi_keyZapi_baseZ
CompletionImportError)r5   r)   r   r8   r%   r%   r&   validate_environmentX   s    


zGooseAI.validate_environment)r*   c              	   C   s0   | j | j| j| j| j| j| j| jd}|| jS )z3Get the default parameters for calling GooseAI API.)r   r   r   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   )selfZnormal_paramsr%   r%   r&   _default_paramsl   s    
zGooseAI._default_paramsc                 C   s   d| j i| jS )zGet the identifying parameters.r   )r   r<   r;   r%   r%   r&   _identifying_params{   s    zGooseAI._identifying_paramsc                 C   s   dS )zReturn type of llm.Zgooseair%   r=   r%   r%   r&   	_llm_type   s    zGooseAI._llm_type)promptstoprun_managerkwargsr*   c                 K   sX   | j }|dk	r&d|krtd||d< ||}| jjf | j|d|}|jd j}|S )zCall the GooseAI API.NrA   z2`stop` found in both the input and default params.)Zenginer@   r   )r<   r1   r   creater   choicestext)r;   r@   rA   rB   rC   paramsresponserF   r%   r%   r&   _call   s    zGooseAI._call)NN)&r!   r"   r#   __doc__r   __annotations__r   strr   floatr   intr   r   r   r   r   r	   dictr   r   r   r   r   r
   r'   r   r7   r   r:   propertyr<   r   r>   r?   r   r   rI   r%   r%   r%   r&   r      sB   
"  
r   )loggingtypingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   r
   r   Zlangchain_core.utilsr   r   r   Zlangchain_core.utils.pydanticr   	getLoggerr!   r2   r   r%   r%   r%   r&   <module>   s   
