The py/genai_client/text_generation/google_clients/vertex_generative_model.py module provides the VertexGenerativeModelClient class, designed to interact with Google's generative models (like Gemini) hosted on Google Cloud Vertex AI.
- Purpose: This class serves as a client for text generation tasks using models deployed on Vertex AI, particularly focusing on the
GenerativeModelclass from thevertexai.preview.generative_modelsSDK, which is often used for Gemini models. It handles chat-based interactions, including history management and streaming. - Relationship to Framework: It extends
AbstractVertextAiTextGeneration(which in turn likely extendsAbstractTextGenerationClient), inheriting common functionalities for Google Cloud authentication, template management, and model limit considerations. It implements the coreask_call()method to make requests to the Vertex AI model.
The client is initialized via its parent class AbstractVertextAiTextGeneration and ultimately AbstractTextGenerationClient. Key parameters relevant at this level or passed through **kwargs include:
model_name(str): The identifier for the specific Vertex AI model (e.g., "gemini-1.0-pro").project_id(str): Your Google Cloud Project ID.location(str): The Google Cloud region where the model is hosted (e.g., "us-central1").creds(Optional[google.auth.credentials.Credentials]): Pre-configured Google Cloud credentials. If not provided, the client attempts to use Application Default Credentials.template(Optional[Dict]): A dictionary of prompt templates.template_name(Optional[str]): The default template name.safety_settings(Optional[Dict]): A dictionary to configure safety settings for blocking harmful content. Keys areHarmCategory(e.g., "HARM_CATEGORY_SEXUALLY_EXPLICIT") and values areHarmBlockThreshold(e.g., "BLOCK_LOW_AND_ABOVE").- Model limit parameters (e.g.,
max_tokens,max_input_tokens) are passed to the parent for tokenizer initialization.
The constructor initializes the underlying Vertex AI client by calling self._get_client().
-
_get_client(self):- Returns an instance of
vertexai.preview.generative_models.GenerativeModel(self.model_name). This is the actual client object used to interact with the specified Vertex AI model.
- Returns an instance of
-
ask_call(self, question: str = None, context: Optional[str] = None, history: Optional[List] = [], max_new_tokens: Optional[int] = 500, temperature: Optional[float] = None, top_p: Optional[float] = None, top_k: Optional[int] = None, candidate_count: Optional[int] = None, stop_sequences: Optional[List[str]] = None, prefix: str = "", stream: Optional[bool] = True, use_history: Optional[bool] = True, **kwargs):- This is the core method for making requests to the Vertex AI model.
- Full Prompt Handling: If
FULL_PROMPTis inkwargs, it expects a list where the last item is the current question, and preceding items form the history. - History Management: If
use_historyis true, it converts the providedhistory(a list of dictionaries with "role"/"author" and "content") into a list of Vertex AIContentobjects. - Context Handling: If
contextis provided, it's prepended to the chat history as a user message, followed by a model message "Understood." to simulate setting a system context (as Gemini models viastart_chatprimarily use user/model turns). - Chat Initialization: Calls
self.client.start_chat(history=historyChat)to create a chat session object. - Generation Configuration: Prepares a
generation_configdictionary with parameters liketemperature,max_output_tokens(frommax_new_tokens),top_p,top_k,stop_sequences, andcandidate_count. - Safety Settings: Converts the
self.safety_settingsdictionary (string keys/values) into the format expected by the Vertex AI SDK, usinggapic_content_types.HarmCategoryandgapic_content_types.SafetySetting.HarmBlockThresholdenums. - API Call:
- Calls
chat.send_message(content=question, generation_config=..., stream=stream, safety_settings=...). - If
streamis true, it iterates through the response stream, concatenatesresponse.textchunks, prints them with the givenprefix, and extracts token counts fromresponse._raw_response.usage_metadata. - If
streamis false, it gets the complete response directly.
- Calls
- Response Packaging: Returns an
AskModelEngineResponsecontaining:response: The concatenated text response.prompt_tokens: Fromusage_metadata.prompt_token_count.response_tokens: Fromusage_metadata.candidates_token_count.
- The client primarily uses
vertexai.preview.generative_models.GenerativeModelto get a model instance. - For chat interactions, it uses
model.start_chat()to get aChatSessionobject. - Messages are sent using
chat.send_message(). - History messages are converted into
vertexai.preview.generative_models.Contentobjects, withPart.from_text()used for the content. - Safety settings are mapped from string configurations to
gapic_content_types.HarmCategoryandHarmBlockThresholdenums.
The client supports common LLM parameters:
max_new_tokens(maps tomax_output_tokens)temperaturetop_ptop_kcandidate_countstop_sequences
These are passed within the generation_config object to the send_message method.
- Safety Settings: Explicitly supports configuring Vertex AI's safety settings.
- Streaming: Supports streaming responses.
- Context Simulation: Simulates a system prompt by prepending user/model turns for context, as the
start_chatmethod for Gemini doesn't directly accept a system prompt in the same way some other APIs do. - Error Handling: Includes a
try-except KeyErrorfor history formatting and logs errors for invalid safety setting categories/thresholds. Other API errors would typically be raised by the Vertex AI SDK.
- Uses
vertexai.preview.generative_models.ContentandPartfor structuring messages sent to the model. - Returns
AskModelEngineResponse(frompy/genai_client/constants.py) to standardize the output.
The VertexGenerativeModelClient provides a SEMOSS-friendly interface to Google's Gemini models on Vertex AI, adapting the AbstractTextGenerationClient framework to the specifics of the Vertex AI SDK.