apache_beam.ml.inference.gemini_inference module
- apache_beam.ml.inference.gemini_inference.generate_from_string(model_name: str, batch: Sequence[str], model: Client, inference_args: dict[str, Any])[source]
Request function that expects inputs to be composed of strings, then sends requests to Gemini to generate text responses based on the text prompts.
- Parameters:
model_name – the Gemini model to use for the request. This model should be a text generation model.
batch – the string inputs to be send to Gemini for text generation.
model – the genai Client
inference_args – any additional arguments passed to the generate_content call.
- apache_beam.ml.inference.gemini_inference.generate_image_from_strings_and_images(model_name: str, batch: Sequence[list[str | Image | Part]], model: Client, inference_args: dict[str, Any])[source]
Request function that expects inputs to be composed of lists of strings and PIL Image instances, then sends requests to Gemini to generate images based on the text prompts and contextual images. This is currently intended to be used with the gemini-2.5-flash-image model (AKA Nano Banana.)
- Parameters:
model_name – the Gemini model to use for the request. This model should be an image generation model such as gemini-2.5-flash-image.
batch – the inputs to be send to Gemini for image generation as prompts. Composed of text prompts and contextual pillow Images.
model – the genai Client
inference_args – any additional arguments passed to the generate_content call.
- class apache_beam.ml.inference.gemini_inference.GeminiModelHandler(model_name: str, request_fn: Callable[[str, Sequence[Any], Client, dict[str, Any]], Any], api_key: str | None = None, project: str | None = None, location: str | None = None, use_vertex_flex_api: bool | None = False, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: int | None = None, max_batch_weight: int | None = None, element_size_fn: Callable[[Any], int] | None = None, **kwargs)[source]
Bases:
RemoteModelHandler[Any,PredictionResult,Client]Implementation of the ModelHandler interface for Google Gemini. NOTE: This API and its implementation are under development and do not provide backward compatibility guarantees. Gemini can be accessed through either the Vertex AI API or the Gemini Developer API, and this handler chooses which to connect to based upon the arguments provided. As written, this model handler operates solely on string input.
- Parameters:
model_name – the Gemini model to send the request to
request_fn – the function to use to send the request. Should take the model name and the parameters from request() and return the responses from Gemini. The class will handle bundling the inputs and responses together.
api_key – the Gemini Developer API key to use for the requests. Setting this parameter sends requests for this job to the Gemini Developer API. If this paramter is provided, do not set the project or location parameters.
project – the GCP project to use for Vertex AI requests. Setting this parameter routes requests to Vertex AI. If this paramter is provided, location must also be provided and api_key should not be set.
location – the GCP project to use for Vertex AI requests. Setting this parameter routes requests to Vertex AI. If this paramter is provided, project must also be provided and api_key should not be set.
use_vertex_flex_api – if true, use the Vertex Flex API. This is a cost-effective option for accessing Gemini models if you can tolerate longer response times and throttling. This is often beneficial for data processing workloads which usually have higher latency tolerance than live serving paths. See https://docs.cloud.google.com/vertex-ai/generative-ai/docs/flex-paygo for more details.
min_batch_size – optional. the minimum batch size to use when batching inputs.
max_batch_size – optional. the maximum batch size to use when batching inputs.
max_batch_duration_secs – optional. the maximum amount of time to buffer a batch before emitting; used in streaming contexts.
max_batch_weight – optional. the maximum total weight of a batch.
element_size_fn – optional. a function that returns the size (weight) of an element.
- create_client() Client[source]
Creates the GenAI client used to send requests. Creates a version for the Vertex AI API or the Gemini Developer API based on the arguments provided when the GeminiModelHandler class is instantiated.
- request(batch: Sequence[Any], model: Client, inference_args: dict[str, Any] | None = None) Iterable[PredictionResult][source]
Sends a prediction request to a Gemini service containing a batch of inputs and matches that input with the prediction response from the endpoint as an iterable of PredictionResults.
- Parameters:
batch – a sequence of any values to be passed to the Gemini service. Should be inputs accepted by the provided inference function.
model – a genai.Client object configured to access the desired service.
inference_args – any additional arguments to send as part of the prediction request.
- Returns:
An iterable of Predictions.