
    B                         d 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 ddl	m
Z
 d Zd	 Zej                  fd
efdZd Zy)z0Library for interacting with the LLM model APIs.    )absolute_import)division)unicode_literals)const)apis)errorsc                  f    t        j                  t        j                  t        j                        S N)r   GetClientInstancer   LLM_API_NAMELLM_API_VERSION     5lib/googlecloudsdk/api_lib/scc/iac_remediation/llm.py	GetClientr      !    			 2 2E4I4I	JJr   c                  f    t        j                  t        j                  t        j                        S r
   )r   GetMessagesModuler   r   r   r   r   r   GetMessagesr      r   r   returnc                    t               }t               }|j                  |j                  |j	                  |j                  |       gd      g|j                  t        j                  t        j                  t        j                  t        j                              d| d|       }|j                  j                  |      }t        |       |j                  d	   j                   j"                  d	   j$                  S )
zMakes a call to the LLM.

  Args:
    input_text: string of the prompt to be sent to the LLM
    proj_id: project_id of the LLM enabled project
    model_name: name of the LLM model to be used

  Returns:
    LLM response in string
  )textuser)partsrole)temperaturetopKtopPmaxOutputTokens)contentsgenerationConfigz	projects/z0/locations/us-central1/publishers/google/models/)-googleCloudAiplatformV1GenerateContentRequestmodelr   )r   r   :AiplatformProjectsLocationsEndpointsGenerateContentRequest-GoogleCloudAiplatformV1GenerateContentRequestGoogleCloudAiplatformV1ContentGoogleCloudAiplatformV1Part'GoogleCloudAiplatformV1GenerationConfigr   TEMPTOPKTOPPMAX_OUTPUT_TOKENSprojects_locations_endpointsGenerateContentValidateLLMResponse
candidatescontentr   r   )
input_textproj_id
model_nameclientmessagesrequestresps          r   MakeLLMCallr9   !   s     ;&](OO4<4j4j55::
:K 	 6  $KK**::::#55	 L  5k 5  y PQ[P\]# P '& 
	,	,	<	<W	E$d			#	#	)	)!	,	1	11r   c                    | j                   rl| j                   d   j                  rS| j                   d   j                  j                  r0| j                   d   j                  j                  d   j                  st	        j
                         y)zCValidates the LLM response.

  Args:
    response: LLM response.
  r   N)r0   r1   r   r   r   EmptyLLMResponseError)responses    r   r/   r/   J   st     

  #++  #++11  #++11!499

&
&
(( :r   N)__doc__
__future__r   r   r   *googlecloudsdk.api_lib.scc.iac_remediationr   googlecloudsdk.api_lib.utilr   .googlecloudsdk.command_lib.scc.iac_remediationr   r   r   LLM_DEFAULT_MODEL_NAMEstrr9   r/   r   r   r   <module>rD      sH    7 &  ' < , AKK ++&2 		&2R)r   