Gemini Handler API¶
auris_tools.geminiHandler.GoogleGeminiHandler ¶
A handler class for interacting with Google's Gemini AI models.
This class provides a convenient interface for generating content using Google's Gemini generative AI models. It handles authentication, model configuration, and content generation with automatic error handling and logging.
Attributes:
| Name | Type | Description |
|---|---|---|
api_key |
str
|
The Google AI API key used for authentication. |
model_name |
str
|
The name of the Gemini model to use. |
temperature |
float
|
Controls randomness in generation (0.0 to 1.0). |
response_schema |
dict
|
Optional schema for structured responses. |
response_mime_type |
str
|
MIME type for response format. |
generation_config |
GenerationConfig
|
Configuration for content generation. |
model |
GenerativeModel
|
The configured Gemini model instance. |
Example
Basic usage with environment variable API key:
handler = GoogleGeminiHandler() response = handler.generate_output("What is artificial intelligence?") text = handler.get_text(response)
Usage with custom parameters:
handler = GoogleGeminiHandler( ... api_key="your-api-key", ... model="gemini-2.0-flash-exp", ... temperature=0.7, ... response_mime_type="text/plain" ... )
Source code in auris_tools/geminiHandler.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
__init__ ¶
Initialize the Google Gemini handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key |
str
|
Google AI API key. If not provided, will attempt to load from GEMINI_API_KEY environment variable. Defaults to None. |
None
|
model |
str
|
Name of the Gemini model to use. Defaults to 'gemini-2.5-flash'. |
'gemini-2.5-flash'
|
**kwargs |
Additional configuration parameters: - temperature (float): Controls randomness (0.0-1.0). Defaults to 0.5. - response_schema (dict): Schema for structured responses. Defaults to None. - response_mime_type (str): Response MIME type. Defaults to 'application/json'. |
{}
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If the specified model is not available. |
Example
handler = GoogleGeminiHandler( ... api_key="your-api-key", ... model="gemini-2.0-flash-exp", ... temperature=0.7 ... )
Source code in auris_tools/geminiHandler.py
generate_output ¶
Generate content using the configured Gemini model.
This method sends a prompt to the Gemini model and returns the generated response. It supports both text-only prompts and multimodal inputs with additional data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt |
str
|
The text prompt to send to the model. This is the main instruction or question for the AI to respond to. |
required |
input_data |
str
|
Additional input data to include with the prompt. This could be text content, encoded media, or other data. Requires input_mime_type to be specified. Defaults to None. |
None
|
input_mime_type |
str
|
MIME type of the input_data. Required if input_data is provided. Examples: 'text/plain', 'image/jpeg', 'application/pdf'. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
|
genai.types.GenerateContentResponse or str: The response from the Gemini model |
|
|
if successful, or an empty string if an error occurred. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If input_data is provided without input_mime_type or vice versa. |
Example
Text-only generation:
response = handler.generate_output("Explain quantum computing")
Multimodal generation with additional data:
response = handler.generate_output( ... prompt="Describe this image", ... input_data=base64_encoded_image, ... input_mime_type="image/jpeg" ... )
Source code in auris_tools/geminiHandler.py
get_text ¶
Extract text content from a Gemini model response.
This method parses the response object returned by the Gemini model and extracts the generated text content. It handles the response structure safely and provides fallbacks for various response formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response |
GenerateContentResponse or dict
|
The response object returned from the generate_output method. This can be either a GenerateContentResponse object or a dictionary representation. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The extracted text content from the response. Returns an empty |
str
|
string if no content is found or if an error occurs during extraction. |
Example
response = handler.generate_output("What is AI?") text_content = handler.get_text(response) print(text_content) "Artificial Intelligence (AI) refers to..."
Handle case with no candidates¶
empty_response = {'candidates': []} text = handler.get_text(empty_response) print(text) # Returns empty string ""