
    Fd                        d dl mZ d dlmZ d dlmZmZmZ d dlm	Z	 d dl
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 ddlmZmZmZ ddlT 	 	 d+d,dZd-d Z	 	 	 	 d.d/d&Zd0d*ZdS )1    )annotations)asdict)ListLiteralOptional)Fore)Config)logger   )
ApiManager)ChatModelResponseChatSequenceMessage)openai)OPEN_AI_CHAT_MODELSOpenAIFunctionCallOpenAIFunctionSpec   )*NfunctionstrargslistdescriptionmodelOptional[str]configOptional[Config]returnc                    ||j         }d |D             }d                    |          }t          j        |t	          dd| d|  d          t	          d|          g          }t          |d	|
          j        S )a  Call an AI function

    This is a magic function that can do anything with no-code. See
    https://github.com/Torantulino/AI-Functions for more info.

    Args:
        function (str): The function to call
        args (list): The arguments to pass to the function
        description (str): The description of the function
        model (str, optional): The model to use. Defaults to None.

    Returns:
        str: The response from the function
    Nc                4    g | ]}|t          |          ndS )NNone)r   ).0args     NC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\llm\utils\__init__.py
<listcomp>z$call_ai_function.<locals>.<listcomp>.   s&    DDDCHHHVDDD    z, systemz0You are now the following python function: ```# 
z+```

Only respond with your `return` value.userr   )prompttemperaturer   )smart_llm_modeljoinr   	for_modelr   create_chat_completioncontent)r   r   r   r   r   arg_strr+   s          r%   call_ai_functionr3      s    * }&DDtDDDD99T??G#M; M MM M M 
 FG$$	

 
F "QvNNNVVr'   r+   r	   r,   Optional[float]max_output_tokensOptional[int]c                    ||j         }||j        }|j        rd|                    |          i}nd|i}t	          j        dd| i||||j        d}t          j        d|            |j	        d         j
        S )Ndeployment_idr   r+   )r,   
max_tokensapi_key
Response: r    )fast_llm_modelr,   	use_azure!get_azure_deployment_id_for_modeliopenaicreate_text_completionopenai_api_keyr
   debugchoicestext)r+   r   r   r,   r5   kwargsresponses          r%   rA   rA   @   s     }%( "!6#K#KE#R#RS5!-  
  $%   H L(h(()))A##r'   r   	functions"Optional[List[OpenAIFunctionSpec]]r9   r   c           
        || j         j        }||j        }t          j        t
          j         d| d| d| t
          j                    |||d}|j        D ]J} |j	        dd| 
                                i|r' |j        dd| 
                                i|}||c S K|j        |d<   |j        r|                    |          |d<   |r,d	 |D             |d
<   t          j        d|d
                     t          j        dd| 
                                i|}	t          j        d|	            t#          |	d          r-t          j        |	j                   t'          |	j                  |	j        d         j        }
|
                    d          }|
                    d          }|j        D ],}|                                s|                    |          }-t3          t4          |         ||          S )a  Create a chat completion using the OpenAI API

    Args:
        messages (List[Message]): The messages to send to the chat completion
        model (str, optional): The model to use. Defaults to None.
        temperature (float, optional): The temperature to use. Defaults to 0.9.
        max_tokens (int, optional): The max tokens to use. Defaults to None.

    Returns:
        str: The response from the chat completion
    Nz$Creating chat completion with model z, temperature z, max_tokens )r   r,   r9   messagesr:   r8   c                    g | ]	}|j         
S r<   )__dict__)r#   r   s     r%   r&   z*create_chat_completion.<locals>.<listcomp>   s(     /
 /
 /
"*H/
 /
 /
r'   rH   zFunction dicts: r;   errorr   r1   function_call)
model_infor1   rO   r<   )r   namer,   r
   rC   r   GREENRESETpluginscan_handle_chat_completionrawhandle_chat_completionrB   r>   r?   r@   r0   hasattrrN   RuntimeErrorrD   messagegetcan_handle_on_responseon_responser   r   )r+   r   rH   r   r,   r9   chat_completion_kwargspluginrZ   rG   first_messager1   rO   s                r%   r0   r0   ^   s   ( }!(
L:  	C  	C5  	C  	CP[  	C  	Cjt  	Cvz  wA  	C  	C   "   . 
 
,6, 
 
ZZ\\
$
 
 		 4f3  ( G "(.(=9% < 44U;; 		
  O/
 /
.7/
 /
 /
{+ 	M(>{(KMMNNN-  
  H L(h(()))x!! +X^$$$8>***$Q'/M'++I66G/</@/@/Q/QM. . .,,.. 	$$W--&u-#   r'   
model_name
model_type,Literal['smart_llm_model', 'fast_llm_model']c           	          t                      }|                                }t           fd|D                       r S t          j        dt
          j        d  d| d           dS )zBCheck if model is available for use. If not, return gpt-3.5-turbo.c              3  ,   K   | ]}|d          v V  dS )idNr<   )r#   mra   s     r%   	<genexpr>zcheck_model.<locals>.<genexpr>   s,      
1
1Q:4 
1
1
1
1
1
1r'   z	WARNING: zYou do not have access to z
. Setting z to gpt-3.5-turbo.zgpt-3.5-turbo)r   
get_modelsanyr
   typewriter_logr   YELLOW)ra   rb   api_managermodelss   `   r%   check_modelro      s     ,,K##%%F

1
1
1
1&
1
1
111 
	Z 	 	: 	 	 	   ?r'   )NN)r   r   r   r   r   r   r   r   r   r   r   r   )r+   r   r   r	   r   r   r,   r4   r5   r6   r   r   )NNNN)r+   r   r   r	   rH   rI   r   r   r,   r4   r9   r6   r   r   )ra   r   rb   rc   r   r   ) 
__future__r   dataclassesr   typingr   r   r   coloramar   autogpt.configr	   autogpt.logsr
   rm   r   baser   r   r   	providersr   r@   providers.openair   r   r   token_counterr3   rA   r0   ro   r<   r'   r%   <module>rz      s   " " " " " "       * * * * * * * * * *       ! ! ! ! ! !       $ $ $ $ $ $ ; ; ; ; ; ; ; ; ; ; ) ) ) ) ) )         
      #'W 'W 'W 'W 'WT$ $ $ $B 59#' $P P P P Pf     r'   