o
    j6h+>                     @  s  d dl mZ d dlZd dlmZmZ d dlm  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 d	lmZ d d
lmZ d dlmZ d dlmZ ddddSddZddddTddZddddUddZ	dVdWd!d"Zd#ddd$dXd(d)Zd#ddd$dYd+d,Zdddddddddd-d.ddd/dZdBdCZej	dVdddd[dHdIZ ejdddd\dKdIZ 	dVdddd]dNdIZ dOdP Z!		d^d_dQdRZ"dS )`    )annotationsN)AnyLiteral)protos)
operations)get_default_model_client)model_types)helper_types)	operation)protobuf_helpers)field_mask_pb2)flatten_update_pathsclientrequest_optionsnamemodel_types.AnyModelNameOptionsr   &helper_types.RequestOptionsType | Nonereturn*model_types.Model | model_types.TunedModelc                C  sH   t | } | drt| ||dS | drt| ||dS td|  )ao  Calls the API to fetch a model by name.

    ```
    import pprint
    model = genai.get_model('models/gemini-1.5-flash')
    pprint.pprint(model)
    ```

    Args:
        name: The name of the model to fetch. Should start with `models/`
        client: The client to use.
        request_options: Options for the request.

    Returns:
        A `types.Model`
    models/r   tunedModels/zWInvalid model name: Model names must start with `models/` or `tunedModels/`. Received: )r   make_model_name
startswithget_base_modelget_tuned_model
ValueError)r   r   r    r   i/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/generativeai/models.py	get_model!   s   


r    model_types.BaseModelNameOptionsmodel_types.Modelc                C  sn   |du ri }|du rt  }t| } | dstd|  |jdd| i|}t||}tjdi |S )ax  Calls the API to fetch a base model by name.

    ```
    import pprint
    model = genai.get_base_model('models/chat-bison-001')
    pprint.pprint(model)
    ```

    Args:
        name: The name of the model to fetch. Should start with `models/`
        client: The client to use.
        request_options: Options for the request.

    Returns:
        A `types.Model`.
    Nr   zJInvalid model name: Base model names must start with `models/`. Received: r   r   )	r   r   r   r   r   r   typeto_dictModelr   r   r   resultr   r   r   r   B   s   

r   !model_types.TunedModelNameOptionsmodel_types.TunedModelc                C  sZ   |du ri }|du rt  }t| } | dstd|  |jdd| i|}t|S )a  Calls the API to fetch a tuned model by name.

    ```
    import pprint
    model = genai.get_tuned_model('tunedModels/gemini-1.5-flash')
    pprint.pprint(model)
    ```

    Args:
        name: The name of the model to fetch. Should start with `tunedModels/`
        client: The client to use.
        request_options: Options for the request.

    Returns:
        A `types.TunedModel`.
    Nr   zPInvalid model name: Tuned model names must start with `tunedModels/`. Received: r   r   )r   r   r   r   r   r   decode_tuned_modelr%   r   r   r   r   i   s   


r   modelr   glm.ModelServiceClient | Nonec                 C  s   t | tr| drt| |d} | j}|S | }|S t | tjr$| j}|S t | tjr/| j}|S t | t	jr:| j}|S t | t	jrNt
| dd}|sL| jj}|S td|  d)z6Calls the API to fetch the base model name of a model.r   )r   
base_modelNz#Invalid model: The provided model 'z' is not recognized or supported. Supported types are: str, model_types.TunedModel, model_types.Model, protos.Model, and protos.TunedModel.)
isinstancestrr   r   r,   r   
TunedModelr$   r   r   getattrtuned_model_source	TypeError)r*   r   r,   r   r   r   get_base_model_name   s0   


r3   2   )	page_sizer   r   r5   
int | Nonemodel_types.ModelsIterablec                 c  sZ    |du ri }|du rt  }|jdd| i|D ]}t||}tjdi |V  qdS )a  Calls the API to list all available models.

    ```
    import pprint
    for model in genai.list_models():
        pprint.pprint(model)
    ```

    Args:
        page_size: How many `types.Models` to fetch per page (api call).
        client: You may pass a `glm.ModelServiceClient` instead of using the default client.
        request_options: Options for the request.

    Yields:
        `types.Model` objects.

    Nr5   r   )r   list_modelsr"   r#   r   r$   r5   r   r   r*   r   r   r   r8      s   r8   model_types.TunedModelsIterablec                 c  sT    |du ri }|du rt  }|jdd| i|D ]}t||}t|V  qdS )a  Calls the API to list all tuned models.

    ```
    import pprint
    for model in genai.list_tuned_models():
        pprint.pprint(model)
    ```

    Args:
        page_size: How many `types.Models` to fetch per page (api call).
        client: You may pass a `glm.ModelServiceClient` instead of using the default client.
        request_options: Options for the request.

    Yields:
        `types.TunedModel` objects.
    Nr5   r   )r   list_tuned_modelsr"   r#   r   r)   r9   r   r   r   r;      s   
r;   
text_inputoutput)iddisplay_namedescriptiontemperaturetop_ptop_kepoch_count
batch_sizelearning_rate	input_key
output_keyr   r   source_modeltraining_datamodel_types.TuningDataOptionsr>   
str | Noner?   r@   rA   float | NonerB   rC   rD   rE   rF   rG   r.   rH   $operations.CreateTunedModelOperationc             
   C  s   |du ri }|du rt  }t| }t| }|dr d|i} n|dr-d||di} ntd|  dtj|||d	}tj||	|
d
}tj	||d}tj
di | ||||||d}|jt||dfi |}tj|S )aB  Calls the API to initiate a tuning process that optimizes a model for specific data, returning an operation object to track and manage the tuning progress.

    Since tuning a model can take significant time, this API doesn't wait for the tuning to complete.
    Instead, it returns a `google.api_core.operation.Operation` object that lets you check on the
    status of the tuning job, or wait for it to complete, and check the result.

    After the job completes you can either find the resulting `TunedModel` object in
    `Operation.result()` or `palm.list_tuned_models` or `palm.get_tuned_model(model_id)`.

    ```
    my_id = "my-tuned-model-id"
    operation = palm.create_tuned_model(
      id = my_id,
      source_model="models/text-bison-001",
      training_data=[{'text_input': 'example input', 'output': 'example output'},...]
    )
    tuned_model=operation.result()      # Wait for tuning to finish

    palm.generate_text(f"tunedModels/{my_id}", prompt="...")
    ```

    Args:
        source_model: The name of the model to tune.
        training_data: The dataset to tune the model on. This must be either:
          * A `protos.Dataset`, or
          * An `Iterable` of:
            *`protos.TuningExample`,
            * `{'text_input': text_input, 'output': output}` dicts
            * `(text_input, output)` tuples.
          * A `Mapping` of `Iterable[str]` - use `input_key` and `output_key` to choose which
            columns to use as the input/output
          * A csv file (will be read with `pd.read_csv` and handles as a `Mapping`
            above). This can be:
            * A local path as a `str` or `pathlib.Path`.
            * A url for a csv file.
            * The url of a Google Sheets file.
          * A JSON file - Its contents will be handled either as an `Iterable` or `Mapping`
            above. This can be:
            * A local path as a `str` or `pathlib.Path`.
        id: The model identifier, used to refer to the model in the API
          `tunedModels/{id}`. Must be unique.
        display_name: A human-readable name for display.
        description: A description of the tuned model.
        temperature: The default temperature for the tuned model, see `types.Model` for details.
        top_p: The default `top_p` for the model, see `types.Model` for details.
        top_k: The default `top_k` for the model, see `types.Model` for details.
        epoch_count: The number of tuning epochs to run. An epoch is a pass over the whole dataset.
        batch_size: The number of examples to use in each training batch.
        learning_rate: The step size multiplier for the gradient updates.
        client: Which client to use.
        request_options: Options for the request.

    Returns:
        A [`google.api_core.operation.Operation`](https://googleapis.dev/python/google-api-core/latest/operation.html)
    Nr   r,   r   r1   )tuned_modelr,   z(Invalid model name: The provided model 'zM' does not match any known model patterns such as 'models/' or 'tunedModels/')rG   rH   )rD   rE   rF   )rJ   hyperparameters)r?   r@   rA   rB   rC   tuning_task)tuned_model_idrO   r   )r   r   r   r3   r   r   encode_tuning_datar   Hyperparameters
TuningTaskr/   create_tuned_modeldictr   CreateTunedModelOperationfrom_core_operation)rI   rJ   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   r   r   source_model_namebase_model_namerP   rQ   rO   r
   r   r   r   rV      sZ   I







rV   rO   protos.TunedModelupdatesNonec                C     d S Nr   rO   r]   r   r   r   r   r   update_tuned_models     rb   dict[str, Any]c                C  r_   r`   r   ra   r   r   r   rb   ~  rc   str | protos.TunedModeldict[str, Any] | Nonec          
      C  s"  |du ri }|du rt  }t| trS| }t|ts$tdt|j d|jd	d|i|} t|}t	
 }| D ]}|j| q:| D ]
\}}t| || qGn+t| tjrs|duratd| j}|j|d}t|j| j}ntdt| j d|jtj| |dfi |}	t|	S )
zeCalls the API to push updates to a specified tuned model where only certain attributes are updatable.NzInvalid argument type: In the function `update_tuned_model(name:str, updates: dict)`, the `updates` argument must be of type `dict`. Received type: .r   zInvalid argument: When calling `update_tuned_model(tuned_model:protos.TunedModel, updates=None)`, the `updates` argument must not be set.)r   zInvalid argument type: In the function `update_tuned_model(tuned_model:dict|protos.TunedModel)`, the `tuned_model` argument must be of type `dict` or `protos.TunedModel`. Received type: )rO   update_maskr   )r   r-   r.   rW   r2   r"   __name__r   r   r   	FieldMaskkeyspathsappenditems_apply_updater   r/   r   r   r   
field_mask_pbrb   UpdateTunedModelRequestr   r)   )
rO   r]   r   r   r   rp   pathvaluewasr&   r   r   r   rb     sJ   	


c                 C  s:   | d}|d d D ]}t| |} qt| |d | d S )Nrg   )splitr0   setattr)thingrs   rt   partspartr   r   r   ro     s   
ro   c                 C  s<   |du ri }|du rt  }t| }|jdd|i| dS )z/Calls the API to delete a specified tuned modelNr   r   )r   r   r   delete_tuned_model)rO   r   r   r   r   r   r   r|     s   
r|   )r   r   r   r   r   r   )r   r    r   r   r   r!   )r   r'   r   r   r   r(   r`   )r*   r   r   r+   )r5   r6   r   r+   r   r   r   r7   )r5   r6   r   r+   r   r   r   r:   ) rI   r   rJ   rK   r>   rL   r?   rL   r@   rL   rA   rM   rB   rM   rC   r6   rD   r6   rE   r6   rF   rM   rG   r.   rH   r.   r   r+   r   r   r   rN   )
rO   r\   r]   r^   r   r+   r   r   r   r(   )
rO   r.   r]   rd   r   r+   r   r   r   r(   )
rO   re   r]   rf   r   r+   r   r   r   r(   )NN)rO   r'   r   r+   r   r   r   r^   )#
__future__r   typingr   r   google.ai.generativelanguageaigenerativelanguageglmgoogle.generativeair   r   google.generativeai.clientr   google.generativeai.typesr   r	   google.api_corer
   r   google.protobufr   google.generativeai.utilsr   r   r   r   r3   r8   r;   rV   overloadrb   ro   r|   r   r   r   r   <module>   s   $*) $(~
5	