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]
- 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, *, min_batch_size: int | None = None, max_batch_size: int | None = None, max_batch_duration_secs: 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.
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.
- 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.