
    g                         d dl Z d dlZd dl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mZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ d dlZddlmZm Z m!Z!  G d d      Z"y)    N)AnyDictListOptionalUnion   )ClassificationClassifications)
Embeddings)CohereError)GenerationsStreamingGenerations)ChatStreamingChat)	Reranking)Summary)Mode   )
lazy_boto3lazy_botocorelazy_sagemakerc            )       N   e Zd Z	 dTdej                  e   fdZdedefdZdeddfdZ	dedefd	Z
	 	 	 	 	 dUdededee   dedededee   ddfdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dVdedee   dee   deeeeef         dee   dee   dee   dee   dee   dee   dee   deeeeef         dee   deeeeef         deeeeef         d ee   d!ee   d"ee   deeef   f&d#Zd$eeef   d"efd%Zd$eeef   defd&Z	 	 	 	 	 	 	 	 	 	 	 	 dWd'edee   dee   d(ededededed)eee      d*ee   d+ee   d"ee   dee   deeef   fd,Zd$eeef   d"efd-Zd$eeef   defd.Z	 	 	 	 dXd/ee   d+ee   d"ee   d0ee   dee   defd1Zd$eeef   d"efd2Zd$eeef   defd3Z	 	 	 	 dXd4edeee   eeeef      f   d5ee   d"ee   d6ee   d7eee      de fd8Z!d9ee   d:ede"fd;Z#ddd
i ddfd:ed<ededee   d=ee   ded>eeef   dee   d?ee   dee   fd@Z$	 	 dYd:edAedBedededee   ddfdCZ%dZdDedEedefdFZ&	 dTded:edGedHee   def
dIZ'di dfd:ed<ededJed=ee   d>eeef   dee   ddfdKZ(	 	 	 	 	 	 d[dLedMee   dNee   dOee   dee   dPee   d"ee   de)fdQZ*d\dRZ+d\dSZ,y)]ClientN
aws_regionc                 d   t               j                  d|      | _        t               j                  d|      | _        t        j
                  j                  d      |t        j
                  d<   t               j                  | j                        | _	        t        j                  | _        y)z
        By default we assume region configured in AWS CLI (`aws configure get region`). You can change the region with
        `aws configure set region us-west-2` or override it with `region_name` parameter.
        zsagemaker-runtime)region_name	sagemakerAWS_DEFAULT_REGIONN)sagemaker_client)r   client_client_service_clientosenvirongetr   Session_sessr   	SAGEMAKERmode)selfr   s     a/var/www/openai/venv/lib/python3.12/site-packages/cohere/manually_maintained/cohere_aws/client.py__init__zClient.__init__   s     "|**+>J*W)|22;J2W::>>./7/9BJJ+,#%--t?S?S-T
NN	    endpoint_namereturnc                 x    	 | j                   j                  |       y# t               j                  $ r Y yw xY w)NEndpointNameFT)r"   describe_endpointr   ClientErrorr*   r.   s     r+   _does_endpoint_existzClient._does_endpoint_exist%   s?    	  222N  ** 		s    99c                 R    | j                  |      st        d| d      || _        y)zConnects to an existing SageMaker endpoint.

        Args:
            endpoint_name (str): The name of the endpoint.

        Raises:
            CohereError: Connection to the endpoint failed.
        	Endpoint z does not exist.N)r6   r   _endpoint_namer5   s     r+   connect_to_endpointzClient.connect_to_endpoint,   s/     ((7	-8HIJJ+r-   s3_models_dirc           
         |j                  d      dz   }t               j                  j                  j	                  || j
                        D cg c]G  }|j                  d      r4|j                  d      d   dk7  r|j                  dd      d   |dd k(  r|I }}t        |      dk(  rt        d	|       t        |      dk(  rt        d
|d           |d   S t        j                         5 }t        j                  j!                  |d      }t        j                  j!                  |d      }|D ]  }t        d|        t               j                  j                  j#                  ||| j
                         t%        j&                  t        j                  j!                  ||j                  d      d               5 }|j)                  |       ddd        t        j                  j!                  |d      }	t%        j&                  |	d      5 }|j+                  |d       ddd       t               j                  j,                  j/                  |	|dd | j
                        }
|dz   t               j                  j                  j	                  || j
                        v sJ 	 ddd       |
S c c}w # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   
S xY w)a  
        Compress an S3 folder which contains one or several fine-tuned models to a tar file.
        If the S3 folder contains only one fine-tuned model, it simply returns the path to that model.
        If the S3 folder contains several fine-tuned models, it download all models, aggregates them into a single
        tar.gz file.

        Args:
            s3_models_dir (str): S3 URI pointing to a folder

        Returns:
            str: S3 URI pointing to the `models.tar.gz` file
        /)sagemaker_session.tar.gzzmodels.tar.gzr   r   NzNo fine-tuned models found in zFound one fine-tuned model: tarmodelszAdding fine-tuned model: zw:gz.)arcname)rstripr   s3S3Downloaderlistr'   endswithsplitrsplitlenr   printtempfileTemporaryDirectoryr#   pathjoindownloadtarfileopen
extractalladd
S3Uploaderupload)r*   r;   s3_paths3_tar_modelstmpdirlocal_tar_models_dirlocal_models_dirs3_tar_modelrA   	model_tarmodel_tar_s3s              r+   _s3_models_dir_to_tarfilez Client._s3_models_dir_to_tarfile9   s    &,,S1C7
 *+..;;@@bfblbl@m
m  +]]3'+>^^C+A.-2DD m 	 
 }" >}oNOO1$0q1A0BCD ## ((*f#%77<<#> !ww||FH= !.1,@A ##0099,H\ptpzpz9{\\"'',,/C\EWEWX[E\]_E`"abfiNN#34 cb !. V_=Ii0C(#6 1
 *+..99@@MZ][]L^rvr|r|@}L !?2n6F6I6I6V6V6[6[\i  ~B  ~H  ~H6[  7I  I  I  I+ +. Q
2 cb
 10 +. sF   AJ;2CKK A KK+BK K
KK	KK#ml.g4dn.xlargearninstance_typen_instancesrecreaterolec           	      6   | j                  |      r6|r"| j                  |       | j                          nt        d| d| d      i }d}	t	               }
d}|||d<   | j                  |      }	n||d<   t	        dd	      }
d
}	 | j                  j                  |       	 | j                  j                  |       |:|rt               j                         }d| d}n	 t               j                         }|@ t               j                   d#||	| j"                  d|}	  |j$                  ||fd|i|
 n| j                  j)                  ||d
d|i       | j                  j+                  |d|||ddg       | j                  j-                  ||       | j                  j/                  d      }	 t        d| d       |j1                  |ddd !       | j                  |       y# t               j                  $ r Y jw xY w# t               j                  $ r Y kw xY w# t        $ r t        d       d}Y Lw xY w# t               j&                  $ r |j%                  |||       Y w xY w# t2        $ r}t        d"|       d}~ww xY w)$a%  Creates and deploys a SageMaker endpoint.

        Args:
            arn (str): The product ARN. Refers to a ready-to-use model (model package) or a fine-tuned model
                (algorithm).
            endpoint_name (str): The name of the endpoint.
            s3_models_dir (str, optional): S3 URI pointing to the folder containing fine-tuned models. Defaults to None.
            instance_type (str, optional): The EC2 instance type to deploy the endpoint to. Defaults to "ml.g4dn.xlarge".
            n_instances (int, optional): Number of endpoint instances. Defaults to 1.
            recreate (bool, optional): Force re-creation of endpoint if it already exists. Defaults to False.
            role (str, optional): The IAM role to use for the endpoint. If not provided, sagemaker.get_execution_role()
                will be used to get the role. This should work when one uses the client inside SageMaker. If this errors
                out, the default role "ServiceRoleSagemaker" will be used, which generally works outside of SageMaker.
        r8   z already exists and recreate=rC   NFalgorithm_arnmodel_package_arni`	  )model_data_download_timeout&container_startup_health_check_timeoutTEndpointConfigName)	ModelNamezarn:aws:iam::z:role/ServiceRoleSagemaker+Using default role: 'ServiceRoleSagemaker'.ServiceRoleSagemaker)rg   
model_datar>   r.   )r.   ModelPackageName)ro   ExecutionRoleArnEnableNetworkIsolationPrimaryContainer
AllTrafficz!al2-ami-sagemaker-inference-gpu-2)VariantNamero   InstanceTypeInitialInstanceCountInferenceAmiVersion)rn   ProductionVariants)r2   rn   endpoint_in_servicezWaiting for endpoint z to be in service...   P   )DelayMaxAttempts)r2   WaiterConfigzFailed to create endpoint:  )r6   r:   delete_endpointr   dictra   r"   delete_endpoint_configr   r4   delete_modelr   
account_idget_execution_role
ValueErrorrM   ModelPackager'   deployParamValidationErrorcreate_modelcreate_endpoint_configcreate_endpoint
get_waiterwait	Exception)r*   rc   r.   r;   rd   re   rf   rg   kwargsrr   validation_paramsuseBoto	accountIDmodelwaiteres                   r+   r   zClient.create_endpointt   s   2 $$]3((7$$&!Im_<YZbYccd"eff
 F$&)F?#77FJ +.F&' !%,07;! G	  77=7Y	  ---F <*,779	&yk1KL2)+>>@D $1N$11 %"&** 	E	V! #0 (	   --'!%'+&"	 .    77#0 (4%2(50;/R$ 8    00*#0 1 
 ))445JKF
E-m_<PQR!.!#')"   	  /[ ** 		
 ** 		 " 2GH1D2( !?77 V[-}UVP  E!$?s"CDDEs`   G2 #H #H. &I ;%I< 2HHH+*H+.II+I98I9<	JJJmessagestreampreamblechat_historyr   model_idtemperaturepk
max_tokenssearch_queries_only	documentsprompt_truncationtoolstool_resultsraw_promptingreturn_promptvariantc                    | j                   t        j                  k(  r| j                  t	        d      i d|d|d|d|d|d|
d|d	|d
|	d|d|d|d|d|d|d|}t        |j                               D ]  \  }}|	||=  | j                   t        j                  k(  r| j                  ||      S | j                   t        j                  k(  r| j                  ||      S t	        d      )a  Returns a Chat object with the query reply.

        Args:
            message (str): The message to send to the chatbot.

            stream (bool): Return streaming tokens.

            preamble (str): (Optional) A string to override the preamble.
            chat_history (List[Dict[str, str]]): (Optional) A list of entries used to construct the conversation. If provided, these messages will be used to build the prompt and the conversation_id will be ignored so no data will be stored to maintain state.

            model (str): (Optional) The model to use for generating the response. Should only be passed for stacked finetune deployment.
            model_id (str): (Optional) The model to use for generating the response. Should only be passed for Bedrock mode; ignored otherwise.
            temperature (float): (Optional) The temperature to use for the response. The higher the temperature, the more random the response.
            p (float): (Optional) The nucleus sampling probability.
            k (float): (Optional) The top-k sampling probability.
            max_tokens (int): (Optional) The max tokens generated for the next reply.

            search_queries_only (bool): (Optional) When true, the response will only contain a list of generated `search_queries`, no reply from the model to the user's message will be generated.
            documents (List[Dict[str, str]]): (Optional) Documents to use to generate grounded response with citations. Example:
                documents=[
                    {
                        "id": "national_geographic_everest",
                        "title": "Height of Mount Everest",
                        "snippet": "The height of Mount Everest is 29,035 feet",
                        "url": "https://education.nationalgeographic.org/resource/mount-everest/",
                    },
                    {
                        "id": "national_geographic_mariana",
                        "title": "Depth of the Mariana Trench",
                        "snippet": "The depth of the Mariana Trench is 36,070 feet",
                        "url": "https://www.nationalgeographic.org/activity/mariana-trench-deepest-place-earth",
                    },
                ],
            prompt_truncation (str) (Optional): Defaults to `OFF`. Dictates how the prompt will be constructed. With `prompt_truncation` set to "AUTO_PRESERVE_ORDER", some elements from `chat_history` and `documents` will be dropped in an attempt to construct a prompt that fits within the model's context length limit. During this process the order of the documents and chat history will be preserved as they are inputted into the API. With `prompt_truncation` set to "OFF", no elements will be dropped. If the sum of the inputs exceeds the model's context length limit, a `TooManyTokens` error will be raised.
        Returns:
            a Chat object if stream=False, or a StreamingChat object if stream=True

        Examples:
            A simple chat message:
                >>> res = co.chat(message="Hey! How are you doing today?")
                >>> print(res.text)
            Streaming chat:
                >>> res = co.chat(
                >>>     message="Hey! How are you doing today?",
                >>>     stream=True)
                >>> for token in res:
                >>>     print(token)
            Stateless chat with chat history:
                >>> res = co.chat(
                >>>     chat_history=[
                >>>         {'role': 'User', message': 'Hey! How are you doing today?'},
                >>>         {'role': 'Chatbot', message': 'I am doing great! How can I help you?'},
                >>>     message="Tell me a joke!",
                >>>     ])
                >>> print(res.text)
            Chat message with documents to use to generate the response:
                >>> res = co.chat(
                >>>     "How deep in the Mariana Trench",
                >>>     documents=[
                >>>         {
                >>>            "id": "national_geographic_everest",
                >>>            "title": "Height of Mount Everest",
                >>>            "snippet": "The height of Mount Everest is 29,035 feet",
                >>>            "url": "https://education.nationalgeographic.org/resource/mount-everest/",
                >>>         },
                >>>         {
                >>>             "id": "national_geographic_mariana",
                >>>             "title": "Depth of the Mariana Trench",
                >>>             "snippet": "The depth of the Mariana Trench is 36,070 feet",
                >>>             "url": "https://www.nationalgeographic.org/activity/mariana-trench-deepest-place-earth",
                >>>         },
                >>>       ])
                >>> print(res.text)
                >>> print(res.citations)
                >>> print(res.documents)
            Generate search queries for fetching documents to use in chat:
                >>> res = co.chat(
                >>>     "What is the height of Mount Everest?",
                >>>      search_queries_only=True)
                >>> if res.is_search_required:
                >>>      print(res.search_queries)
        7No endpoint connected. Run connect_to_endpoint() first.r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Unsupported mode)
r)   r   r(   r9   r   rH   items_sagemaker_chatBEDROCK_bedrock_chat)r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   json_paramskeyvalues                         r+   chatzClient.chat   sy   T 99&4+>+>+F A B B
U
w
 L
 	

 ;
 *
 f
 
 
 U
 L
 "#6
 
 ]
 ]
   !2!
& {0023JC}$ 4 99&''W==YY$,,&%%k8<<011r-   r   c                 <   t        j                  |      }| j                  d|d}|r||d<   	 |d   r5 | j                  j                  di |}t        |d   | j                        S  | j                  j                  di |}t        j                  t        j                  |d   j                         j                                     S # t               j                  $ r}t        t!        |            d }~wt"        $ r}t        t!        |            d }~ww xY w)Napplication/jsonr2   ContentTypeBodyTargetVariantr   r   r   )jsondumpsr9   r!   $invoke_endpoint_with_response_streamr   r)   invoke_endpointr   	from_dictloadsreaddecoder   EndpointConnectionErrorr   strr   r*   r   r   	json_bodyparamsresultr   s          r+   r   zClient._sagemaker_chat  s    JJ{+	 //-

 &-F?#	&8$JJJ $VF^TYY??555??~~djj1D1D1F1M1M1O&PQQ66 	&c!f%% 	& c!f%%	&s*   9C (A"C D"C66DDDc                 L   |st        d      |d   r|d   }nd}|d= t        j                  |      }||d}	 |r5 | j                  j                  di |}t        |d   | j                        S  | j                  j                  di |}t        j                  t        j                  |d   j                         j                                     S # t               j                  $ r}t        t        |            d }~wt         $ r}t        t        |            d }~ww xY w)N-must supply model_id arg when calling bedrockr   FbodymodelIdr   r   )r   r   r   r!   !invoke_model_with_response_streamr   r)   invoke_modelr   r   r   r   r   r   r   r   r   )r*   r   r   r   r   r   r   r   s           r+   r   zClient._bedrock_chat  s   MNNx  *FF!JJ{+	

	&GGG $VF^TYY??222<V<~~JJvf~224;;=>@ @66 	&c!f%% 	& c!f%%	&s*   6C 0A"C D#*C>>D#
DD#promptnum_generationsstop_sequencesreturn_likelihoodstruncatec                    | j                   t        j                  k(  r| j                  t	        d      |||||||	|
||d
}t        |j                               D ]  \  }}|	||=  | j                   t        j                  k(  r||d<   | j                  ||      S | j                   t        j                  k(  r| j                  ||      S t	        d      )Nr   )
r   r   r   r   r   r   r   r   r   r   r   r   )
r)   r   r(   r9   r   rH   r   _sagemaker_generationsr   _bedrock_generations)r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                    r+   generatezClient.generate  s    ( 99&4+>+>+F A B B $&,"4 
 {0023JC}$ 4 99&-<K)*..{GDDYY$,,&,,[(CC011r-   c                 .   t        j                  |      }| j                  d|d}|r||d<   	 |d   r5 | j                  j                  di |}t        |d   | j                        S  | j                  j                  di |}t        t        j                  |d   j                         j                               d         S # t               j                  $ r}t        t        |            d }~wt         $ r}t        t        |            d }~ww xY w)Nr   r   r   r   r   generationsr   )r   r   r9   r!   r   r   r)   r   r   r   r   r   r   r   r   r   r   r   s          r+   r   zClient._sagemaker_generations  s   JJ{+	 //-

 &-F?#	&8$JJJ +F6NDIIFF555??"JJvf~224;;=>}MO O66 	&c!f%% 	& c!f%%	&s*   9C (AC DC//D;DDc                 $   |st        d      t        j                  |      }||d}	 |d   r5 | j                  j                  di |}t        |d   | j                        S  | j                  j                  di |}t        t        j                  |d   j                         j                               d         S # t               j                  $ r}t        t        |            d }~wt        $ r}t        t        |            d }~ww xY w)Nr   r   r   r   r   r   )r   r   r   r!   r   r   r)   r   r   r   r   r   r   r   r   r   )r*   r   r   r   r   r   r   s          r+   r   zClient._bedrock_generations	  s    MNNJJ{+	

	&8$GGG +F6NDIIFF222<V<"JJvf~224;;=>}MO O66 	&c!f%% 	& c!f%%	&s*   9B? #AB? ?DC**D6D

Dtexts
input_typec                 .   |||d}t        |j                               D ]  \  }}|	||=  | j                  t        j                  k(  r| j                  ||      S | j                  t        j                  k(  r| j                  ||      S t        d      )N)r   r   r   r   )	rH   r   r)   r   r(   _sagemaker_embedr   _bedrock_embedr   )	r*   r   r   r   r   r   r   r   r   s	            r+   embedzClient.embed"  s      $

 {0023JC}$ 4 99&((g>>YY$,,&&&{H==011r-   c                    | j                   t        d      t        j                  |      }| j                   d|d}|r||d<   	  | j                  j
                  di |}t        j                  |d   j                         j                               }t        |d         S # t               j                  $ r}t        t        |            d }~wt        $ r}t        t        |            d }~ww xY w)Nr   r   r   r   r   
embeddingsr   )r9   r   r   r   r!   r   r   r   r   r   r   r   r   r   )r*   r   r   r   r   r   responser   s           r+   r   zClient._sagemaker_embed:  s    & A B B JJ{+	 //-

 &-F?#	&1T\\11;F;Fzz&."5"5"7">">"@AH (<011 66 	&c!f%% 	& c!f%%	&s%   AB# #C3:CC3C..C3c                    |st        d      t        j                  |      }||d}	  | j                  j                  di |}t        j
                  |d   j                         j                               }t        |d         S # t               j                  $ r}t        t        |            d }~wt        $ r}t        t        |            d }~ww xY w)Nr   r   r   r   r   )r   r   r   r!   r   r   r   r   r   r   r   r   r   )r*   r   r   r   r   r   r   r   s           r+   r   zClient._bedrock_embedT  s    MNNJJ{+	

	&.T\\..88Fzz&."5"5"7">">"@AH (<011 66 	&c!f%% 	& c!f%%	&s$   AB CB22C>CCquerytop_nmax_chunks_per_docrank_fieldsc                    | j                   t        d      g }|D ]S  }t        |t              r|j	                  d|i       't        |t
              r|j	                  |       It        d       |||d||d}	t        j                  |	      }
| j                   d|
d	}|||d
<   	  | j                  j                  di |}t        j                  |d   j                         j                               }t        |      }|j                  D ]  }||j                     |_         	 |S # t#               j$                  $ r}t        t        |            d}~wt&        $ r}t        t        |            d}~ww xY w)a-  Returns an ordered list of documents oridered by their relevance to the provided query
        Args:
            query (str): The search query
            documents (list[str], list[dict]): The documents to rerank
            top_n (int): (optional) The number of results to return, defaults to return all results
            max_chunks_per_doc (int): (optional) The maximum number of chunks derived from a document
            rank_fields (list[str]): (optional) The fields used for reranking. This parameter is only supported for rerank v3 models
        Nr   textz@invalid format for documents, must be a list of strings or dicts)r   F)r   r   r   return_documentsr   r   r   r   r   r   r   )r9   r   
isinstancer   appendr   r   r   r!   r   r   r   r   r   resultsindexdocumentr   r   r   )r*   r   r   r   r   r   r   parsed_docsdocr   r   r   r   r   	rerankingrankr   s                    r+   rerankzClient.rerankj  s     & A B B C#s#""FC=1C&""3'!^` `  $ %#5&
 JJ{+	 !//-

 &-F?#	&1T\\11;F;Fzz&."5"5"7">">"@AH!(+I!)) +DJJ 7 *  66 	&c!f%% 	& c!f%%	&s%   (B D+ +E;EE;"E66E;inputnamec                    | j                   t        d      ||d}t        j                  |      }| j                   d|d}	  | j                  j
                  di |}t        j                  |d   j                         j                               }t        |D 	cg c]  }	t        |	       c}	      S # t               j                  $ r}t        t        |            d }~wt        $ r}t        t        |            d }~ww xY wc c}	w )Nr   )r   r   r   r   r   r   )r9   r   r   r   r!   r   r   r   r   r   r   r   r   r
   r	   )
r*   r   r   r   r   r   r   r   r   classifications
             r+   classifyzClient.classify  s   & A B B !&48JJ{+	 !//-
	&1T\\11;F;Fzz&."5"5"7">">"@AH U]^U]>~ >U]^__ 66 	&c!f%% 	& c!f%%	&
  _s+   AB5 D5DC  D,D  D
train_data	eval_datatraining_parametersbase_model_idc
           	      x   |dk7  sJ d       | j                   t        j                  k(  r| j                  ||||	|||      S |j	                  d      dz   }|	 t               j                         }|j                  d|i       t               j                  j                  ||d	|| j                  ||
      }
i }|j                  d      st        d      ||d<   |!|j                  d      st        d      ||d<   |
j                  |       |
j                  j                   }| | d}t#               j%                  d      }t               j&                  j)                  |      \  }}t               j&                  j)                  | | d      \  }}|j+                  ||      j-                  ||d       t               j&                  j)                  ||z         \  }}|j/                  |      j0                  j3                  |      j5                          y# t        $ r t        d       d}Y w xY w)a  Creates a fine-tuning job and returns an optional fintune job ID.

        Args:
            name (str): The name to give to the fine-tuned model.
            train_data (str): An S3 path pointing to the training data.
            s3_models_dir (str): An S3 path pointing to the directory where the fine-tuned model will be saved.
            arn (str, optional): The product ARN of the fine-tuning package. Required in Sagemaker mode and ignored otherwise
            eval_data (str, optional): An S3 path pointing to the eval data. Defaults to None.
            instance_type (str, optional): The EC2 instance type to use for training. Defaults to "ml.g4dn.xlarge".
            training_parameters (Dict[str, Any], optional): Additional training parameters. Defaults to {}.
            role (str, optional): The IAM role to use for the endpoint. 
                In Bedrock this mode is required and is used to access s3 input and output data.
                If not provided in sagemaker, sagemaker.get_execution_role()will be used to get the role.
                This should work when one uses the client inside SageMaker. If this errors
                out, the default role "ServiceRoleSagemaker" will be used, which generally works outside of SageMaker.
            base_model_id (str, optional): The ID of the Bedrock base model to finetune with. Required in Bedrock mode and ignored otherwise.
        r   name cannot be 'model')r   r   r;   
base_modelr   r   rg   r=   Nrp   rq   r   r   ri   rg   instance_countrd   r>   output_pathhyperparameterss3:(train_data must point to an S3 location.training'eval_data must point to an S3 location.
evaluationinputs/output/model.tar.gzrF   r?   BucketKey
CopySourcePrefix)r)   r   r   _bedrock_create_finetunerE   r   r   r   rM   update	algorithmAlgorithmEstimatorr'   
startswithfitlatest_training_jobr   r   resourcerF   parse_s3_urlObjectcopyr  objectsfilterdelete)r*   r   r   r;   rc   r   rd   r   rg   r  	estimatorr  job_namecurrent_filepaths3_resourcebucketold_key_new_keyold_short_keys                       r+   create_finetunezClient.create_finetune  sR   : w8 8899$00dzan  |I  U^  tG  NR0  S  S%,,S1C7<.%'::<
 	""FD>2"$..AA'"jj%/ B 
	 $$U+GHH'z ''. !JKK#,F< V$0055+_XJ6JK l++D1 )*--::;KL#%((55vW6UV
767+00vV]<^0_ !/ 0 3 3 @ @QYAY Z6"**111GNNPK  .CD-.s   H H98H9s3_checkpoint_dirs3_output_dirc           	         |dk(  rt        d      |j                  d      dz   }|	 t               j                         }d|i}t               j
                  j                  ||d|| j                  ||	      }|j                  d
      st        d      d|i}	|j                  |	       |j                  j                  }
| |
 d}t               j                  d      }t               j                  j                  |      \  }}t               j                  j                  | | d      \  }}|j!                  ||      j#                  ||d       t               j                  j                  | |
       \  }}|j%                  |      j&                  j)                  |      j+                          y# t         $ r t	        d       d}Y w xY w)a  Export the merged weights to the TensorRT-LLM inference engine.

        Args:
        name (str): The name used while writing the exported model to the output directory.
        s3_checkpoint_dir (str): An S3 path pointing to the directory of the model checkpoint (merged weights).
        s3_output_dir (str): An S3 path pointing to the directory where the TensorRT-LLM engine will be saved.
        arn (str): The product ARN of the bring your own finetuning algorithm.
        instance_type (str, optional): The EC2 instance type to use for export. Defaults to "ml.p4de.24xlarge".
        role (str, optional): The IAM role to use for export.
            If not provided, sagemaker.get_execution_role() will be used to get the role.
            This should work when one uses the client inside SageMaker. If this errors out,
            the default role "ServiceRoleSagemaker" will be used, which generally works outside SageMaker.
        r   r  r=   Nrp   rq   r   r   r  r	  z/s3_checkpoint_dir must point to an S3 location.
checkpointr  r  rF   r?   r  r  r  )r   rE   r   r   rM   r  r  r'   r  r  r  r   r   r  rF   r   r!  r"  r  r#  r$  r%  )r*   r   r0  r1  rc   rd   rg   export_parametersr&  r  r'  r(  r)  r*  r+  r,  r-  r.  s                     r+   export_finetunezClient.export_finetune  s   , 7?566%,,S1C7<.%'::<
 $TN"$..AA'"jj%- B 
	 !++E2NOO 12V$0055+_XJ6JK l++D1 )*--::;KL#%((55vW6UV
767+00vV]<^0_ !/ 0 3 3 @ @M?S[R\A] ^6"**111GNNPE  .CD-.s   F: :GGjob_idtimeoutc                     t        j                          |z   }	 | j                  j                  |      }|d   }|dv rn8t        j                          |kD  rt        d      t        j                  d       ^|dk7  rt        d| d      |d	   S )
a'  Waits for a finetune job to complete and returns a model arn if complete. Throws an exception if timeout occurs or if job does not complete successfully
        Args:
            job_id (str): The arn of the model customization job
            timeout(int, optional): Timeout in seconds
        )jobIdentifierstatus)	CompletedFailedStoppedz*could not complete finetune within timeout
   r;  z0finetune did not finish successfuly, ended with z statusoutputModelArn)timer"   get_model_customization_jobr   sleep)r*   r6  r7  endcustomization_job
job_statuss         r+   wait_for_finetune_jobzClient.wait_for_finetune_jobT  s     iikG# $ 4 4 P P_e P f*84J??yy{S !"NOOJJrN  $ PQ[P\\cdee !122r-   model_unitscommitment_durationc                     | j                   t        j                  k7  rt        d      i }|r||d<    | j                  j
                  d|||d|}|d   S )a  Returns the provisined model arn
        Args:
            model_id (str): The ID or ARN of the model to provision
            name (str): Name of the provisioned throughput model
            model_units (int): Number of units to provision
            commitment_duration (str, optional): Commitment duration, one of ("OneMonth", "SixMonths"), defaults to no commitment if unspecified
        z(can only provision throughput in bedrockcommitmentDuration)provisionedModelNamer   
modelUnitsprovisionedModelArnr   )r)   r   r   r   r"   #create_provisioned_model_throughput)r*   r   r   rG  rH  r   r   s          r+   provision_throughputzClient.provision_throughputh  sq     99$GHH+>F'(K4''KK 
!%"
 	
 -..r-   r  c           
         |st        d      |st        d      |j                  d      st        d      |r#|j                  d      st        d      dd|igi}| d}	| j                  j                  |	|||d|id|i|	      }
|
d
   S )Nzname must not be emptyzmust provide a role ARN for bedrock finetuning (https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-iam-role.html)r	  r
  r  
validatorss3Uriz-job)jobNamecustomModelNameroleArnbaseModelIdentifiertrainingDataConfigvalidationDataConfigoutputDataConfighyperParametersjobArn)r   r  r"   create_model_customization_job)r*   r   r   r;   r  r   r   rg   rX  r'  rD  s              r+   r  zClient._bedrock_create_finetune  s     566  g  h  h$$U+GHH''. !JKKY  $  V4= 00OO  * '4!5%}5/ P 	
 !**r-   r   lengthformat_extractivenessadditional_commandc                 J   | j                   t        d      ||||||d}t        |j                               D ]  \  }	}
|
	||	=  t	        j
                  |      }| j                   d|d}|||d<   	  | j                  j                  di |}t	        j                  |d   j                         j                               }t        |      }|S # t               j                  $ r}t        t        |            d }~wt        $ r}t        t        |            d }~ww xY w)Nr   )r   r]  formatr_  r   r`  r   r   r   r   r   )r9   r   rH   r   r   r   r!   r   r   r   r   r   r   r   r   r   )r*   r   r]  r^  r_  r   r`  r   r   r   r   r   r   r   r   summaryr   s                    r+   	summarizezClient.summarize  s;    & A B B ,&"4
 {0023JC}$ 4 JJ{+	 !//-

 &-F?#		&1T\\11;F;Fzz&."5"5"7">">"@AHh'G  66 	&c!f%% 	& c!f%%	&s%   5AC D")C==D"	DD"c                    | j                   t        d      	 | j                  j                  | j                          	 | j                  j                  | j                          y #  t	        d       Y 7xY w#  t	        d       Y y xY w)NzNo endpoint connected.r1   z&Endpoint not found, skipping deletion.rm   z-Endpoint config not found, skipping deletion.)r9   r   r"   r   rM   r   r*   s    r+   r   zClient.delete_endpoint  s    &677	<  00d>Q>Q0R	C  774K^K^7_		<:;	CABs   &A'  &A9 'A69Bc                     	 | j                   j                          | j                  j                          y # t        $ r t	        d        w xY w)NzfSageMaker client could not be closed. This might be because you are using an old version of SageMaker.)r!   closer"   AttributeErrorrM   rf  s    r+   rh  zClient.close  sD    	LL   &&( 	z{	s	   47 A)N)Nrb   r   FN)FNNNNNNNNNNNNNFFN)NNr   i  g      ?r   g      ?NNNNT)NNNN)zml.p4de.24xlargeN)i   )autorj  rj  g333333? N)r/   N)-__name__
__module____qualname__typingr   r   r,   boolr6   r:   ra   intr   r   r   r   floatr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r/  r5  rF  rO  r  r   rd  r   rh  r   r-   r+   r   r      sY    04#,#"# $ , , ,9s 9s 9~ (,-"D0D0 D0  }	D0
 D0 D0 D0 smD0 
D0R "'"&7;#"&'+!!$(.248+/047;(-(-!%+I2I2 I2 3-	I2
 tDcN34I2 }I2 3-I2 e_I2 E?I2 E?I2 SMI2 &d^I2 Dc3h01I2  $C=!I2" T#s(^,-#I2$ tDcN34%I2&  ~'I2(  ~)I2* #+I2, 
t]"	#-I2V&4S> &C &2&c3h &3 &H  $"&  ! .2,0"&!%!%%/2/2 }	/2 3-/2 /2 /2 /2 /2 /2 !c+/2 %SM/2  3-!/2" ##/2$ %/2& 
{00	1'/2b&$sCx. &3 &4&S#X &# &8 #'!%$("&2Cy2 3-2 #	2
 SM2 3-2 
202DcN 2S 242$sCx. 2C 22 '+(,3726==S	4S#X+? ?@= sm= !	=
 $,C== %T#Y/= <E=~`d3i `s ` `@ "#'-.0"'+LQLQ LQ 	LQ
 c]LQ C=LQ LQ "#s(^LQ smLQ  }LQ 
#LQh 0"@Q@Q @Q 	@Q
 @Q @Q sm@Q 
@QD3C 3# 3C 32 .2// / 	/
 &c]/ 
/D $(.0"$+$+ $+ 	$+
 $+ C=$+ "#s(^$+ sm$+ 
$+T !'!' )/'*,.!%11 1 #	1 !1 e_1 %SM1 #1 
1hCr-   r   )#r   r#   rS   rN   r@  ro  r   r   r   r   r   r   r	   r
   r   r   errorr   
generationr   r   r   r   r   r   r   rc  r   r)   r   lazy_aws_depsr   r   r   r   r   r-   r+   <module>rv     sG     	    3 3 ; "  9 %     E E_ _r-   