SDK Vertex AI per Python
Vertex AI: Google Vertex AI è una suite integrata di strumenti e servizi di machine learning per la creazione e l'utilizzo di modelli ML con AutoML o codice personalizzato. Offre a principianti ed esperti il miglior workbench per l'intero ciclo di vita dello sviluppo del machine learning.
Guida rapida
Per utilizzare questa libreria, devi prima svolgere i seguenti passaggi:
Installazione
Installa questa libreria in un virtualenv utilizzando pip. virtualenv è uno strumento per creare ambienti Python isolati. Il problema fondamentale che risolve è legato alle dipendenze, alle versioni e alle autorizzazioni indirettamente.
Con virtualenv, è possibile installare questa libreria senza dover disporre delle autorizzazioni di installazione del sistema e senza entrare in conflitto con le dipendenze di sistema installate.
Mac/Linux
pip install virtualenv virtualenv <your-env> source <your-env>/bin/activate <your-env>/bin/pip install google-cloud-aiplatform
Windows
pip install virtualenv virtualenv <your-env> <your-env>\Scripts\activate <your-env>\Scripts\pip.exe install google-cloud-aiplatform
Versioni di Python supportate
Python >= 3,8
In fase di importazione
La funzionalità di disponibilità generale (GA) dell'SDK Vertex AI può essere utilizzata importando il seguente spazio dei nomi:
import vertexai
La funzionalità di anteprima dell'SDK Vertex AI può essere utilizzata importando il seguente spazio dei nomi:
from vertexai import preview
Utilizzo
Per istruzioni dettagliate, consulta la quickstart e Introduzione alle classi multimodali nell'SDK Vertex AI.
Importazioni
from vertexai.generative_models import GenerativeModel, Image, Content, Part, Tool, FunctionDeclaration, GenerationConfig
Generazione di base:
from vertexai.generative_models import GenerativeModel model = GenerativeModel("gemini-pro") print(model.generate_content("Why is sky blue?"))
Utilizzo di immagini e video
from vertexai.generative_models import GenerativeModel, Image vision_model = GenerativeModel("gemini-pro-vision") # Local image image = Image.load_from_file("image.jpg") print(vision_model.generate_content(["What is shown in this image?", image])) # Image from Cloud Storage image_part = generative_models.Part.from_uri("gs://download.tensorflow.org/example_images/320px-Felis_catus-cat_on_snow.jpg", mime_type="image/jpeg") print(vision_model.generate_content([image_part, "Describe this image?"])) # Text and video video_part = Part.from_uri("gs://cloud-samples-data/video/animals.mp4", mime_type="video/mp4") print(vision_model.generate_content(["What is in the video? ", video_part]))
Chat
from vertexai.generative_models import GenerativeModel, Image vision_model = GenerativeModel("gemini-ultra-vision") vision_chat = vision_model.start_chat() image = Image.load_from_file("image.jpg") print(vision_chat.send_message(["I like this image.", image])) print(vision_chat.send_message("What things do I like?."))
Istruzioni di sistema
from vertexai.generative_models import GenerativeModel model = GenerativeModel( "gemini-1.0-pro", system_instruction=[ "Talk like a pirate.", "Don't use rude words.", ], ) print(model.generate_content("Why is sky blue?"))
Chiamata di funzione
# First, create tools that the model is can use to answer your questions. # Describe a function by specifying it's schema (JsonSchema format) get_current_weather_func = generative_models.FunctionDeclaration( name="get_current_weather", description="Get the current weather in a given location", parameters={ "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA" }, "unit": { "type": "string", "enum": [ "celsius", "fahrenheit", ] } }, "required": [ "location" ] }, ) # Tool is a collection of related functions weather_tool = generative_models.Tool( function_declarations=[get_current_weather_func], ) # Use tools in chat: model = GenerativeModel( "gemini-pro", # You can specify tools when creating a model to avoid having to send them with every request. tools=[weather_tool], ) chat = model.start_chat() # Send a message to the model. The model will respond with a function call. print(chat.send_message("What is the weather like in Boston?")) # Then send a function response to the model. The model will use it to answer. print(chat.send_message( Part.from_function_response( name="get_current_weather", response={ "content": {"weather": "super nice"}, } ), ))
Chiamate di funzioni automatiche
from vertexai..preview generative_models import GenerativeModel, Tool, FunctionDeclaration, AutomaticFunctionCallingResponder # First, create functions that the model is can use to answer your questions. def get_current_weather(location: str, unit: str = "centigrade"): """Gets weather in the specified location. Args: location: The location for which to get the weather. unit: Optional. Temperature unit. Can be Centigrade or Fahrenheit. Defaults to Centigrade. """ return dict( location=location, unit=unit, weather="Super nice, but maybe a bit hot.", ) # Infer function schema get_current_weather_func = FunctionDeclaration.from_func(get_current_weather) # Tool is a collection of related functions weather_tool = Tool( function_declarations=[get_current_weather_func], ) # Use tools in chat: model = GenerativeModel( "gemini-pro", # You can specify tools when creating a model to avoid having to send them with every request. tools=[weather_tool], ) # Activate automatic function calling: afc_responder = AutomaticFunctionCallingResponder( # Optional: max_automatic_function_calls=5, ) chat = model.start_chat(responder=afc_responder) # Send a message to the model. The model will respond with a function call. # The SDK will automatically call the requested function and respond to the model. # The model will use the function call response to answer the original question. print(chat.send_message("What is the weather like in Boston?"))
Documentazione
Puoi trovare la documentazione completa per gli SDK Vertex AI e il modello Gemini nella documentazione di Google Cloud
Contributo
Per saperne di più su come contribuire all'SDK Python di Vertex AI, consulta la pagina Contributo.
Licenza
I contenuti di questo repository sono concessi in licenza in base alla licenza Apache, versione 2.0.