google_vertex.md
1 --- 2 title: "Google Vertex" 3 id: integrations-google-vertex 4 description: "Google Vertex integration for Haystack" 5 slug: "/integrations-google-vertex" 6 --- 7 8 <a id="haystack_integrations.components.generators.google_vertex.gemini"></a> 9 10 ## Module haystack\_integrations.components.generators.google\_vertex.gemini 11 12 <a id="haystack_integrations.components.generators.google_vertex.gemini.VertexAIGeminiGenerator"></a> 13 14 ### VertexAIGeminiGenerator 15 16 `VertexAIGeminiGenerator` enables text generation using Google Gemini models. 17 18 Usage example: 19 ```python 20 from haystack_integrations.components.generators.google_vertex import VertexAIGeminiGenerator 21 22 23 gemini = VertexAIGeminiGenerator() 24 result = gemini.run(parts = ["What is the most interesting thing you know?"]) 25 for answer in result["replies"]: 26 print(answer) 27 28 >>> 1. **The Origin of Life:** How and where did life begin? The answers to this ... 29 >>> 2. **The Unseen Universe:** The vast majority of the universe is ... 30 >>> 3. **Quantum Entanglement:** This eerie phenomenon in quantum mechanics allows ... 31 >>> 4. **Time Dilation:** Einstein's theory of relativity revealed that time can ... 32 >>> 5. **The Fermi Paradox:** Despite the vastness of the universe and the ... 33 >>> 6. **Biological Evolution:** The idea that life evolves over time through natural ... 34 >>> 7. **Neuroplasticity:** The brain's ability to adapt and change throughout life, ... 35 >>> 8. **The Goldilocks Zone:** The concept of the habitable zone, or the Goldilocks zone, ... 36 >>> 9. **String Theory:** This theoretical framework in physics aims to unify all ... 37 >>> 10. **Consciousness:** The nature of human consciousness and how it arises ... 38 ``` 39 40 <a id="haystack_integrations.components.generators.google_vertex.gemini.VertexAIGeminiGenerator.__init__"></a> 41 42 #### VertexAIGeminiGenerator.\_\_init\_\_ 43 44 ```python 45 def __init__(*, 46 model: str = "gemini-2.0-flash", 47 project_id: Optional[str] = None, 48 location: Optional[str] = None, 49 generation_config: Optional[Union[GenerationConfig, 50 dict[str, Any]]] = None, 51 safety_settings: Optional[dict[HarmCategory, 52 HarmBlockThreshold]] = None, 53 system_instruction: Optional[Union[str, ByteStream, Part]] = None, 54 streaming_callback: Optional[Callable[[StreamingChunk], 55 None]] = None) 56 ``` 57 58 Multi-modal generator using Gemini model via Google Vertex AI. 59 60 Authenticates using Google Cloud Application Default Credentials (ADCs). 61 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 62 63 **Arguments**: 64 65 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 66 - `model`: Name of the model to use. For available models, see https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models. 67 - `location`: The default location to use when making API calls, if not set uses us-central-1. 68 - `generation_config`: The generation config to use. 69 Can either be a [`GenerationConfig`](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.GenerationConfig) 70 object or a dictionary of parameters. 71 Accepted fields are: 72 - temperature 73 - top_p 74 - top_k 75 - candidate_count 76 - max_output_tokens 77 - stop_sequences 78 - `safety_settings`: The safety settings to use. See the documentation 79 for [HarmBlockThreshold](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.HarmBlockThreshold) 80 and [HarmCategory](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.HarmCategory) 81 for more details. 82 - `system_instruction`: Default system instruction to use for generating content. 83 - `streaming_callback`: A callback function that is called when a new token is received from the stream. 84 The callback function accepts StreamingChunk as an argument. 85 86 <a id="haystack_integrations.components.generators.google_vertex.gemini.VertexAIGeminiGenerator.to_dict"></a> 87 88 #### VertexAIGeminiGenerator.to\_dict 89 90 ```python 91 def to_dict() -> dict[str, Any] 92 ``` 93 94 Serializes the component to a dictionary. 95 96 **Returns**: 97 98 Dictionary with serialized data. 99 100 <a id="haystack_integrations.components.generators.google_vertex.gemini.VertexAIGeminiGenerator.from_dict"></a> 101 102 #### VertexAIGeminiGenerator.from\_dict 103 104 ```python 105 @classmethod 106 def from_dict(cls, data: dict[str, Any]) -> "VertexAIGeminiGenerator" 107 ``` 108 109 Deserializes the component from a dictionary. 110 111 **Arguments**: 112 113 - `data`: Dictionary to deserialize from. 114 115 **Returns**: 116 117 Deserialized component. 118 119 <a id="haystack_integrations.components.generators.google_vertex.gemini.VertexAIGeminiGenerator.run"></a> 120 121 #### VertexAIGeminiGenerator.run 122 123 ```python 124 @component.output_types(replies=list[str]) 125 def run(parts: Variadic[Union[str, ByteStream, Part]], 126 streaming_callback: Optional[Callable[[StreamingChunk], None]] = None) 127 ``` 128 129 Generates content using the Gemini model. 130 131 **Arguments**: 132 133 - `parts`: Prompt for the model. 134 - `streaming_callback`: A callback function that is called when a new token is received from the stream. 135 136 **Returns**: 137 138 A dictionary with the following keys: 139 - `replies`: A list of generated content. 140 141 <a id="haystack_integrations.components.generators.google_vertex.captioner"></a> 142 143 ## Module haystack\_integrations.components.generators.google\_vertex.captioner 144 145 <a id="haystack_integrations.components.generators.google_vertex.captioner.VertexAIImageCaptioner"></a> 146 147 ### VertexAIImageCaptioner 148 149 `VertexAIImageCaptioner` enables text generation using Google Vertex AI imagetext generative model. 150 151 Authenticates using Google Cloud Application Default Credentials (ADCs). 152 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 153 154 Usage example: 155 ```python 156 import requests 157 158 from haystack.dataclasses.byte_stream import ByteStream 159 from haystack_integrations.components.generators.google_vertex import VertexAIImageCaptioner 160 161 captioner = VertexAIImageCaptioner() 162 163 image = ByteStream( 164 data=requests.get( 165 "https://raw.githubusercontent.com/deepset-ai/haystack-core-integrations/main/integrations/google_vertex/example_assets/robot1.jpg" 166 ).content 167 ) 168 result = captioner.run(image=image) 169 170 for caption in result["captions"]: 171 print(caption) 172 173 >>> two gold robots are standing next to each other in the desert 174 ``` 175 176 <a id="haystack_integrations.components.generators.google_vertex.captioner.VertexAIImageCaptioner.__init__"></a> 177 178 #### VertexAIImageCaptioner.\_\_init\_\_ 179 180 ```python 181 def __init__(*, 182 model: str = "imagetext", 183 project_id: Optional[str] = None, 184 location: Optional[str] = None, 185 **kwargs) 186 ``` 187 188 Generate image captions using a Google Vertex AI model. 189 190 Authenticates using Google Cloud Application Default Credentials (ADCs). 191 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 192 193 **Arguments**: 194 195 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 196 - `model`: Name of the model to use. 197 - `location`: The default location to use when making API calls, if not set uses us-central-1. 198 Defaults to None. 199 - `kwargs`: Additional keyword arguments to pass to the model. 200 For a list of supported arguments see the `ImageTextModel.get_captions()` documentation. 201 202 <a id="haystack_integrations.components.generators.google_vertex.captioner.VertexAIImageCaptioner.to_dict"></a> 203 204 #### VertexAIImageCaptioner.to\_dict 205 206 ```python 207 def to_dict() -> dict[str, Any] 208 ``` 209 210 Serializes the component to a dictionary. 211 212 **Returns**: 213 214 Dictionary with serialized data. 215 216 <a id="haystack_integrations.components.generators.google_vertex.captioner.VertexAIImageCaptioner.from_dict"></a> 217 218 #### VertexAIImageCaptioner.from\_dict 219 220 ```python 221 @classmethod 222 def from_dict(cls, data: dict[str, Any]) -> "VertexAIImageCaptioner" 223 ``` 224 225 Deserializes the component from a dictionary. 226 227 **Arguments**: 228 229 - `data`: Dictionary to deserialize from. 230 231 **Returns**: 232 233 Deserialized component. 234 235 <a id="haystack_integrations.components.generators.google_vertex.captioner.VertexAIImageCaptioner.run"></a> 236 237 #### VertexAIImageCaptioner.run 238 239 ```python 240 @component.output_types(captions=list[str]) 241 def run(image: ByteStream) 242 ``` 243 244 Prompts the model to generate captions for the given image. 245 246 **Arguments**: 247 248 - `image`: The image to generate captions for. 249 250 **Returns**: 251 252 A dictionary with the following keys: 253 - `captions`: A list of captions generated by the model. 254 255 <a id="haystack_integrations.components.generators.google_vertex.code_generator"></a> 256 257 ## Module haystack\_integrations.components.generators.google\_vertex.code\_generator 258 259 <a id="haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator"></a> 260 261 ### VertexAICodeGenerator 262 263 This component enables code generation using Google Vertex AI generative model. 264 265 `VertexAICodeGenerator` supports `code-bison`, `code-bison-32k`, and `code-gecko`. 266 267 Usage example: 268 ```python 269 from haystack_integrations.components.generators.google_vertex import VertexAICodeGenerator 270 271 generator = VertexAICodeGenerator() 272 273 result = generator.run(prefix="def to_json(data):") 274 275 for answer in result["replies"]: 276 print(answer) 277 278 >>> ```python 279 >>> import json 280 >>> 281 >>> def to_json(data): 282 >>> """Converts a Python object to a JSON string. 283 >>> 284 >>> Args: 285 >>> data: The Python object to convert. 286 >>> 287 >>> Returns: 288 >>> A JSON string representing the Python object. 289 >>> """ 290 >>> 291 >>> return json.dumps(data) 292 >>> ``` 293 ``` 294 295 <a id="haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator.__init__"></a> 296 297 #### VertexAICodeGenerator.\_\_init\_\_ 298 299 ```python 300 def __init__(*, 301 model: str = "code-bison", 302 project_id: Optional[str] = None, 303 location: Optional[str] = None, 304 **kwargs) 305 ``` 306 307 Generate code using a Google Vertex AI model. 308 309 Authenticates using Google Cloud Application Default Credentials (ADCs). 310 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 311 312 **Arguments**: 313 314 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 315 - `model`: Name of the model to use. 316 - `location`: The default location to use when making API calls, if not set uses us-central-1. 317 - `kwargs`: Additional keyword arguments to pass to the model. 318 For a list of supported arguments see the `TextGenerationModel.predict()` documentation. 319 320 <a id="haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator.to_dict"></a> 321 322 #### VertexAICodeGenerator.to\_dict 323 324 ```python 325 def to_dict() -> dict[str, Any] 326 ``` 327 328 Serializes the component to a dictionary. 329 330 **Returns**: 331 332 Dictionary with serialized data. 333 334 <a id="haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator.from_dict"></a> 335 336 #### VertexAICodeGenerator.from\_dict 337 338 ```python 339 @classmethod 340 def from_dict(cls, data: dict[str, Any]) -> "VertexAICodeGenerator" 341 ``` 342 343 Deserializes the component from a dictionary. 344 345 **Arguments**: 346 347 - `data`: Dictionary to deserialize from. 348 349 **Returns**: 350 351 Deserialized component. 352 353 <a id="haystack_integrations.components.generators.google_vertex.code_generator.VertexAICodeGenerator.run"></a> 354 355 #### VertexAICodeGenerator.run 356 357 ```python 358 @component.output_types(replies=list[str]) 359 def run(prefix: str, suffix: Optional[str] = None) 360 ``` 361 362 Generate code using a Google Vertex AI model. 363 364 **Arguments**: 365 366 - `prefix`: Code before the current point. 367 - `suffix`: Code after the current point. 368 369 **Returns**: 370 371 A dictionary with the following keys: 372 - `replies`: A list of generated code snippets. 373 374 <a id="haystack_integrations.components.generators.google_vertex.image_generator"></a> 375 376 ## Module haystack\_integrations.components.generators.google\_vertex.image\_generator 377 378 <a id="haystack_integrations.components.generators.google_vertex.image_generator.VertexAIImageGenerator"></a> 379 380 ### VertexAIImageGenerator 381 382 This component enables image generation using Google Vertex AI generative model. 383 384 Authenticates using Google Cloud Application Default Credentials (ADCs). 385 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 386 387 Usage example: 388 ```python 389 from pathlib import Path 390 391 from haystack_integrations.components.generators.google_vertex import VertexAIImageGenerator 392 393 generator = VertexAIImageGenerator() 394 result = generator.run(prompt="Generate an image of a cute cat") 395 result["images"][0].to_file(Path("my_image.png")) 396 ``` 397 398 <a id="haystack_integrations.components.generators.google_vertex.image_generator.VertexAIImageGenerator.__init__"></a> 399 400 #### VertexAIImageGenerator.\_\_init\_\_ 401 402 ```python 403 def __init__(*, 404 model: str = "imagegeneration", 405 project_id: Optional[str] = None, 406 location: Optional[str] = None, 407 **kwargs) 408 ``` 409 410 Generates images using a Google Vertex AI model. 411 412 Authenticates using Google Cloud Application Default Credentials (ADCs). 413 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 414 415 **Arguments**: 416 417 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 418 - `model`: Name of the model to use. 419 - `location`: The default location to use when making API calls, if not set uses us-central-1. 420 - `kwargs`: Additional keyword arguments to pass to the model. 421 For a list of supported arguments see the `ImageGenerationModel.generate_images()` documentation. 422 423 <a id="haystack_integrations.components.generators.google_vertex.image_generator.VertexAIImageGenerator.to_dict"></a> 424 425 #### VertexAIImageGenerator.to\_dict 426 427 ```python 428 def to_dict() -> dict[str, Any] 429 ``` 430 431 Serializes the component to a dictionary. 432 433 **Returns**: 434 435 Dictionary with serialized data. 436 437 <a id="haystack_integrations.components.generators.google_vertex.image_generator.VertexAIImageGenerator.from_dict"></a> 438 439 #### VertexAIImageGenerator.from\_dict 440 441 ```python 442 @classmethod 443 def from_dict(cls, data: dict[str, Any]) -> "VertexAIImageGenerator" 444 ``` 445 446 Deserializes the component from a dictionary. 447 448 **Arguments**: 449 450 - `data`: Dictionary to deserialize from. 451 452 **Returns**: 453 454 Deserialized component. 455 456 <a id="haystack_integrations.components.generators.google_vertex.image_generator.VertexAIImageGenerator.run"></a> 457 458 #### VertexAIImageGenerator.run 459 460 ```python 461 @component.output_types(images=list[ByteStream]) 462 def run(prompt: str, negative_prompt: Optional[str] = None) 463 ``` 464 465 Produces images based on the given prompt. 466 467 **Arguments**: 468 469 - `prompt`: The prompt to generate images from. 470 - `negative_prompt`: A description of what you want to omit in 471 the generated images. 472 473 **Returns**: 474 475 A dictionary with the following keys: 476 - `images`: A list of ByteStream objects, each containing an image. 477 478 <a id="haystack_integrations.components.generators.google_vertex.question_answering"></a> 479 480 ## Module haystack\_integrations.components.generators.google\_vertex.question\_answering 481 482 <a id="haystack_integrations.components.generators.google_vertex.question_answering.VertexAIImageQA"></a> 483 484 ### VertexAIImageQA 485 486 This component enables text generation (image captioning) using Google Vertex AI generative models. 487 488 Authenticates using Google Cloud Application Default Credentials (ADCs). 489 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 490 491 Usage example: 492 ```python 493 from haystack.dataclasses.byte_stream import ByteStream 494 from haystack_integrations.components.generators.google_vertex import VertexAIImageQA 495 496 qa = VertexAIImageQA() 497 498 image = ByteStream.from_file_path("dog.jpg") 499 500 res = qa.run(image=image, question="What color is this dog") 501 502 print(res["replies"][0]) 503 504 >>> white 505 ``` 506 507 <a id="haystack_integrations.components.generators.google_vertex.question_answering.VertexAIImageQA.__init__"></a> 508 509 #### VertexAIImageQA.\_\_init\_\_ 510 511 ```python 512 def __init__(*, 513 model: str = "imagetext", 514 project_id: Optional[str] = None, 515 location: Optional[str] = None, 516 **kwargs) 517 ``` 518 519 Answers questions about an image using a Google Vertex AI model. 520 521 Authenticates using Google Cloud Application Default Credentials (ADCs). 522 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 523 524 **Arguments**: 525 526 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 527 - `model`: Name of the model to use. 528 - `location`: The default location to use when making API calls, if not set uses us-central-1. 529 - `kwargs`: Additional keyword arguments to pass to the model. 530 For a list of supported arguments see the `ImageTextModel.ask_question()` documentation. 531 532 <a id="haystack_integrations.components.generators.google_vertex.question_answering.VertexAIImageQA.to_dict"></a> 533 534 #### VertexAIImageQA.to\_dict 535 536 ```python 537 def to_dict() -> dict[str, Any] 538 ``` 539 540 Serializes the component to a dictionary. 541 542 **Returns**: 543 544 Dictionary with serialized data. 545 546 <a id="haystack_integrations.components.generators.google_vertex.question_answering.VertexAIImageQA.from_dict"></a> 547 548 #### VertexAIImageQA.from\_dict 549 550 ```python 551 @classmethod 552 def from_dict(cls, data: dict[str, Any]) -> "VertexAIImageQA" 553 ``` 554 555 Deserializes the component from a dictionary. 556 557 **Arguments**: 558 559 - `data`: Dictionary to deserialize from. 560 561 **Returns**: 562 563 Deserialized component. 564 565 <a id="haystack_integrations.components.generators.google_vertex.question_answering.VertexAIImageQA.run"></a> 566 567 #### VertexAIImageQA.run 568 569 ```python 570 @component.output_types(replies=list[str]) 571 def run(image: ByteStream, question: str) 572 ``` 573 574 Prompts model to answer a question about an image. 575 576 **Arguments**: 577 578 - `image`: The image to ask the question about. 579 - `question`: The question to ask. 580 581 **Returns**: 582 583 A dictionary with the following keys: 584 - `replies`: A list of answers to the question. 585 586 <a id="haystack_integrations.components.generators.google_vertex.text_generator"></a> 587 588 ## Module haystack\_integrations.components.generators.google\_vertex.text\_generator 589 590 <a id="haystack_integrations.components.generators.google_vertex.text_generator.VertexAITextGenerator"></a> 591 592 ### VertexAITextGenerator 593 594 This component enables text generation using Google Vertex AI generative models. 595 596 `VertexAITextGenerator` supports `text-bison`, `text-unicorn` and `text-bison-32k` models. 597 598 Authenticates using Google Cloud Application Default Credentials (ADCs). 599 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 600 601 Usage example: 602 ```python 603 from haystack_integrations.components.generators.google_vertex import VertexAITextGenerator 604 605 generator = VertexAITextGenerator() 606 res = generator.run("Tell me a good interview question for a software engineer.") 607 608 print(res["replies"][0]) 609 610 >>> **Question:** 611 >>> You are given a list of integers and a target sum. 612 >>> Find all unique combinations of numbers in the list that add up to the target sum. 613 >>> 614 >>> **Example:** 615 >>> 616 >>> ``` 617 >>> Input: [1, 2, 3, 4, 5], target = 7 618 >>> Output: [[1, 2, 4], [3, 4]] 619 >>> ``` 620 >>> 621 >>> **Follow-up:** What if the list contains duplicate numbers? 622 ``` 623 624 <a id="haystack_integrations.components.generators.google_vertex.text_generator.VertexAITextGenerator.__init__"></a> 625 626 #### VertexAITextGenerator.\_\_init\_\_ 627 628 ```python 629 def __init__(*, 630 model: str = "text-bison", 631 project_id: Optional[str] = None, 632 location: Optional[str] = None, 633 **kwargs) 634 ``` 635 636 Generate text using a Google Vertex AI model. 637 638 Authenticates using Google Cloud Application Default Credentials (ADCs). 639 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 640 641 **Arguments**: 642 643 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 644 - `model`: Name of the model to use. 645 - `location`: The default location to use when making API calls, if not set uses us-central-1. 646 - `kwargs`: Additional keyword arguments to pass to the model. 647 For a list of supported arguments see the `TextGenerationModel.predict()` documentation. 648 649 <a id="haystack_integrations.components.generators.google_vertex.text_generator.VertexAITextGenerator.to_dict"></a> 650 651 #### VertexAITextGenerator.to\_dict 652 653 ```python 654 def to_dict() -> dict[str, Any] 655 ``` 656 657 Serializes the component to a dictionary. 658 659 **Returns**: 660 661 Dictionary with serialized data. 662 663 <a id="haystack_integrations.components.generators.google_vertex.text_generator.VertexAITextGenerator.from_dict"></a> 664 665 #### VertexAITextGenerator.from\_dict 666 667 ```python 668 @classmethod 669 def from_dict(cls, data: dict[str, Any]) -> "VertexAITextGenerator" 670 ``` 671 672 Deserializes the component from a dictionary. 673 674 **Arguments**: 675 676 - `data`: Dictionary to deserialize from. 677 678 **Returns**: 679 680 Deserialized component. 681 682 <a id="haystack_integrations.components.generators.google_vertex.text_generator.VertexAITextGenerator.run"></a> 683 684 #### VertexAITextGenerator.run 685 686 ```python 687 @component.output_types(replies=list[str], 688 safety_attributes=dict[str, float], 689 citations=list[dict[str, Any]]) 690 def run(prompt: str) 691 ``` 692 693 Prompts the model to generate text. 694 695 **Arguments**: 696 697 - `prompt`: The prompt to use for text generation. 698 699 **Returns**: 700 701 A dictionary with the following keys: 702 - `replies`: A list of generated replies. 703 - `safety_attributes`: A dictionary with the [safety scores](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/responsible-ai#safety_attribute_descriptions) 704 of each answer. 705 - `citations`: A list of citations for each answer. 706 707 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini"></a> 708 709 ## Module haystack\_integrations.components.generators.google\_vertex.chat.gemini 710 711 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator"></a> 712 713 ### VertexAIGeminiChatGenerator 714 715 `VertexAIGeminiChatGenerator` enables chat completion using Google Gemini models. 716 717 Authenticates using Google Cloud Application Default Credentials (ADCs). 718 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 719 720 ### Usage example 721 ```python 722 from haystack.dataclasses import ChatMessage 723 from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator 724 725 gemini_chat = VertexAIGeminiChatGenerator() 726 727 messages = [ChatMessage.from_user("Tell me the name of a movie")] 728 res = gemini_chat.run(messages) 729 730 print(res["replies"][0].text) 731 >>> The Shawshank Redemption 732 733 #### With Tool calling: 734 735 ```python 736 from typing import Annotated 737 from haystack.utils import Secret 738 from haystack.dataclasses.chat_message import ChatMessage 739 from haystack.components.tools import ToolInvoker 740 from haystack.tools import create_tool_from_function 741 742 from haystack_integrations.components.generators.google_vertex import VertexAIGeminiChatGenerator 743 744 __example function to get the current weather__ 745 746 def get_current_weather( 747 location: Annotated[str, "The city for which to get the weather, e.g. 'San Francisco'"] = "Munich", 748 unit: Annotated[str, "The unit for the temperature, e.g. 'celsius'"] = "celsius", 749 ) -> str: 750 return f"The weather in {location} is sunny. The temperature is 20 {unit}." 751 752 tool = create_tool_from_function(get_current_weather) 753 tool_invoker = ToolInvoker(tools=[tool]) 754 755 gemini_chat = VertexAIGeminiChatGenerator( 756 model="gemini-2.0-flash-exp", 757 tools=[tool], 758 ) 759 user_message = [ChatMessage.from_user("What is the temperature in celsius in Berlin?")] 760 replies = gemini_chat.run(messages=user_message)["replies"] 761 print(replies[0].tool_calls) 762 763 __actually invoke the tool__ 764 765 tool_messages = tool_invoker.run(messages=replies)["tool_messages"] 766 messages = user_message + replies + tool_messages 767 768 __transform the tool call result into a human readable message__ 769 770 final_replies = gemini_chat.run(messages=messages)["replies"] 771 print(final_replies[0].text) 772 ``` 773 774 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator.__init__"></a> 775 776 #### VertexAIGeminiChatGenerator.\_\_init\_\_ 777 778 ```python 779 def __init__(*, 780 model: str = "gemini-1.5-flash", 781 project_id: Optional[str] = None, 782 location: Optional[str] = None, 783 generation_config: Optional[Union[GenerationConfig, 784 dict[str, Any]]] = None, 785 safety_settings: Optional[dict[HarmCategory, 786 HarmBlockThreshold]] = None, 787 tools: Optional[list[Tool]] = None, 788 tool_config: Optional[ToolConfig] = None, 789 streaming_callback: Optional[StreamingCallbackT] = None) 790 ``` 791 792 `VertexAIGeminiChatGenerator` enables chat completion using Google Gemini models. 793 794 Authenticates using Google Cloud Application Default Credentials (ADCs). 795 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 796 797 **Arguments**: 798 799 - `model`: Name of the model to use. For available models, see https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models. 800 - `project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 801 - `location`: The default location to use when making API calls, if not set uses us-central-1. 802 Defaults to None. 803 - `generation_config`: Configuration for the generation process. 804 See the [GenerationConfig documentation](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.GenerationConfig 805 for a list of supported arguments. 806 - `safety_settings`: Safety settings to use when generating content. See the documentation 807 for [HarmBlockThreshold](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.HarmBlockThreshold) 808 and [HarmCategory](https://cloud.google.com/python/docs/reference/aiplatform/latest/vertexai.generative_models.HarmCategory) 809 for more details. 810 - `tools`: A list of tools for which the model can prepare calls. 811 - `tool_config`: The tool config to use. See the documentation for [ToolConfig] 812 (https://cloud.google.com/vertex-ai/generative-ai/docs/reference/python/latest/vertexai.generative_models.ToolConfig) 813 - `streaming_callback`: A callback function that is called when a new token is received from 814 the stream. The callback function accepts StreamingChunk as an argument. 815 816 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator.to_dict"></a> 817 818 #### VertexAIGeminiChatGenerator.to\_dict 819 820 ```python 821 def to_dict() -> dict[str, Any] 822 ``` 823 824 Serializes the component to a dictionary. 825 826 **Returns**: 827 828 Dictionary with serialized data. 829 830 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator.from_dict"></a> 831 832 #### VertexAIGeminiChatGenerator.from\_dict 833 834 ```python 835 @classmethod 836 def from_dict(cls, data: dict[str, Any]) -> "VertexAIGeminiChatGenerator" 837 ``` 838 839 Deserializes the component from a dictionary. 840 841 **Arguments**: 842 843 - `data`: Dictionary to deserialize from. 844 845 **Returns**: 846 847 Deserialized component. 848 849 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator.run"></a> 850 851 #### VertexAIGeminiChatGenerator.run 852 853 ```python 854 @component.output_types(replies=list[ChatMessage]) 855 def run(messages: list[ChatMessage], 856 streaming_callback: Optional[StreamingCallbackT] = None, 857 *, 858 tools: Optional[list[Tool]] = None) 859 ``` 860 861 **Arguments**: 862 863 - `messages`: A list of `ChatMessage` instances, representing the input messages. 864 - `streaming_callback`: A callback function that is called when a new token is received from the stream. 865 - `tools`: A list of tools for which the model can prepare calls. If set, it will override the `tools` parameter set 866 during component initialization. 867 868 **Returns**: 869 870 A dictionary containing the following key: 871 - `replies`: A list containing the generated responses as `ChatMessage` instances. 872 873 <a id="haystack_integrations.components.generators.google_vertex.chat.gemini.VertexAIGeminiChatGenerator.run_async"></a> 874 875 #### VertexAIGeminiChatGenerator.run\_async 876 877 ```python 878 @component.output_types(replies=list[ChatMessage]) 879 async def run_async(messages: list[ChatMessage], 880 streaming_callback: Optional[StreamingCallbackT] = None, 881 *, 882 tools: Optional[list[Tool]] = None) 883 ``` 884 885 Async version of the run method. Generates text based on the provided messages. 886 887 **Arguments**: 888 889 - `messages`: A list of `ChatMessage` instances, representing the input messages. 890 - `streaming_callback`: A callback function that is called when a new token is received from the stream. 891 - `tools`: A list of tools for which the model can prepare calls. If set, it will override the `tools` parameter set 892 during component initialization. 893 894 **Returns**: 895 896 A dictionary containing the following key: 897 - `replies`: A list containing the generated responses as `ChatMessage` instances. 898 899 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder"></a> 900 901 ## Module haystack\_integrations.components.embedders.google\_vertex.document\_embedder 902 903 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder"></a> 904 905 ### VertexAIDocumentEmbedder 906 907 Embed text using Vertex AI Embeddings API. 908 909 See available models in the official 910 [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#syntax). 911 912 Usage example: 913 ```python 914 from haystack import Document 915 from haystack_integrations.components.embedders.google_vertex import VertexAIDocumentEmbedder 916 917 doc = Document(content="I love pizza!") 918 919 document_embedder = VertexAIDocumentEmbedder(model="text-embedding-005") 920 921 result = document_embedder.run([doc]) 922 print(result['documents'][0].embedding) 923 # [-0.044606007635593414, 0.02857724390923977, -0.03549133986234665, 924 ``` 925 926 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.__init__"></a> 927 928 #### VertexAIDocumentEmbedder.\_\_init\_\_ 929 930 ```python 931 def __init__(model: Literal[ 932 "text-embedding-004", 933 "text-embedding-005", 934 "textembedding-gecko-multilingual@001", 935 "text-multilingual-embedding-002", 936 "text-embedding-large-exp-03-07", 937 ], 938 task_type: Literal[ 939 "RETRIEVAL_DOCUMENT", 940 "RETRIEVAL_QUERY", 941 "SEMANTIC_SIMILARITY", 942 "CLASSIFICATION", 943 "CLUSTERING", 944 "QUESTION_ANSWERING", 945 "FACT_VERIFICATION", 946 "CODE_RETRIEVAL_QUERY", 947 ] = "RETRIEVAL_DOCUMENT", 948 gcp_region_name: Optional[Secret] = Secret.from_env_var( 949 "GCP_DEFAULT_REGION", strict=False), 950 gcp_project_id: Optional[Secret] = Secret.from_env_var( 951 "GCP_PROJECT_ID", strict=False), 952 batch_size: int = 32, 953 max_tokens_total: int = 20000, 954 time_sleep: int = 30, 955 retries: int = 3, 956 progress_bar: bool = True, 957 truncate_dim: Optional[int] = None, 958 meta_fields_to_embed: Optional[list[str]] = None, 959 embedding_separator: str = "\n") -> None 960 ``` 961 962 Generate Document Embedder using a Google Vertex AI model. 963 964 Authenticates using Google Cloud Application Default Credentials (ADCs). 965 For more information see the official [Google documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc). 966 967 **Arguments**: 968 969 - `model`: Name of the model to use. 970 - `task_type`: The type of task for which the embeddings are being generated. 971 For more information see the official [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#tasktype). 972 - `gcp_region_name`: The default location to use when making API calls, if not set uses us-central-1. 973 - `gcp_project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 974 - `batch_size`: The number of documents to process in a single batch. 975 - `max_tokens_total`: The maximum number of tokens to process in total. 976 - `time_sleep`: The time to sleep between retries in seconds. 977 - `retries`: The number of retries in case of failure. 978 - `progress_bar`: Whether to display a progress bar during processing. 979 - `truncate_dim`: The dimension to truncate the embeddings to, if specified. 980 - `meta_fields_to_embed`: A list of metadata fields to include in the embeddings. 981 - `embedding_separator`: The separator to use between different embeddings. 982 983 **Raises**: 984 985 - `ValueError`: If the provided model is not in the list of supported models. 986 987 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.get_text_embedding_input"></a> 988 989 #### VertexAIDocumentEmbedder.get\_text\_embedding\_input 990 991 ```python 992 def get_text_embedding_input( 993 batch: list[Document]) -> list[TextEmbeddingInput] 994 ``` 995 996 Converts a batch of Document objects into a list of TextEmbeddingInput objects. 997 998 **Arguments**: 999 1000 - `batch` _List[Document]_ - A list of Document objects to be converted. 1001 1002 1003 **Returns**: 1004 1005 - `List[TextEmbeddingInput]` - A list of TextEmbeddingInput objects created from the input documents. 1006 1007 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.embed_batch_by_smaller_batches"></a> 1008 1009 #### VertexAIDocumentEmbedder.embed\_batch\_by\_smaller\_batches 1010 1011 ```python 1012 def embed_batch_by_smaller_batches(batch: list[str], 1013 subbatch=1) -> list[list[float]] 1014 ``` 1015 1016 Embeds a batch of text strings by dividing them into smaller sub-batches. 1017 1018 **Arguments**: 1019 1020 - `batch` _List[str]_ - A list of text strings to be embedded. 1021 - `subbatch` _int, optional_ - The size of the smaller sub-batches. Defaults to 1. 1022 1023 **Returns**: 1024 1025 - `List[List[float]]` - A list of embeddings, where each embedding is a list of floats. 1026 1027 **Raises**: 1028 1029 - `Exception` - If embedding fails at the item level, an exception is raised with the error details. 1030 1031 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.embed_batch"></a> 1032 1033 #### VertexAIDocumentEmbedder.embed\_batch 1034 1035 ```python 1036 def embed_batch(batch: list[str]) -> list[list[float]] 1037 ``` 1038 1039 Generate embeddings for a batch of text strings. 1040 1041 **Arguments**: 1042 1043 - `batch` _List[str]_ - A list of text strings to be embedded. 1044 1045 1046 **Returns**: 1047 1048 - `List[List[float]]` - A list of embeddings, where each embedding is a list of floats. 1049 1050 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.run"></a> 1051 1052 #### VertexAIDocumentEmbedder.run 1053 1054 ```python 1055 @component.output_types(documents=list[Document]) 1056 def run(documents: list[Document]) 1057 ``` 1058 1059 Processes all documents in batches while adhering to the API's token limit per request. 1060 1061 **Arguments**: 1062 1063 - `documents`: A list of documents to embed. 1064 1065 **Returns**: 1066 1067 A dictionary with the following keys: 1068 - `documents`: A list of documents with embeddings. 1069 1070 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.to_dict"></a> 1071 1072 #### VertexAIDocumentEmbedder.to\_dict 1073 1074 ```python 1075 def to_dict() -> dict[str, Any] 1076 ``` 1077 1078 Serializes the component to a dictionary. 1079 1080 **Returns**: 1081 1082 Dictionary with serialized data. 1083 1084 <a id="haystack_integrations.components.embedders.google_vertex.document_embedder.VertexAIDocumentEmbedder.from_dict"></a> 1085 1086 #### VertexAIDocumentEmbedder.from\_dict 1087 1088 ```python 1089 @classmethod 1090 def from_dict(cls, data: dict[str, Any]) -> "VertexAIDocumentEmbedder" 1091 ``` 1092 1093 Deserializes the component from a dictionary. 1094 1095 **Arguments**: 1096 1097 - `data`: Dictionary to deserialize from. 1098 1099 **Returns**: 1100 1101 Deserialized component. 1102 1103 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder"></a> 1104 1105 ## Module haystack\_integrations.components.embedders.google\_vertex.text\_embedder 1106 1107 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder.VertexAITextEmbedder"></a> 1108 1109 ### VertexAITextEmbedder 1110 1111 Embed text using VertexAI Text Embeddings API. 1112 1113 See available models in the official 1114 [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#syntax). 1115 1116 Usage example: 1117 ```python 1118 from haystack_integrations.components.embedders.google_vertex import VertexAITextEmbedder 1119 1120 text_to_embed = "I love pizza!" 1121 1122 text_embedder = VertexAITextEmbedder(model="text-embedding-005") 1123 1124 print(text_embedder.run(text_to_embed)) 1125 # {'embedding': [-0.08127457648515701, 0.03399784862995148, -0.05116401985287666, ...] 1126 ``` 1127 1128 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder.VertexAITextEmbedder.__init__"></a> 1129 1130 #### VertexAITextEmbedder.\_\_init\_\_ 1131 1132 ```python 1133 def __init__(model: Literal[ 1134 "text-embedding-004", 1135 "text-embedding-005", 1136 "textembedding-gecko-multilingual@001", 1137 "text-multilingual-embedding-002", 1138 "text-embedding-large-exp-03-07", 1139 ], 1140 task_type: Literal[ 1141 "RETRIEVAL_DOCUMENT", 1142 "RETRIEVAL_QUERY", 1143 "SEMANTIC_SIMILARITY", 1144 "CLASSIFICATION", 1145 "CLUSTERING", 1146 "QUESTION_ANSWERING", 1147 "FACT_VERIFICATION", 1148 "CODE_RETRIEVAL_QUERY", 1149 ] = "RETRIEVAL_QUERY", 1150 gcp_region_name: Optional[Secret] = Secret.from_env_var( 1151 "GCP_DEFAULT_REGION", strict=False), 1152 gcp_project_id: Optional[Secret] = Secret.from_env_var( 1153 "GCP_PROJECT_ID", strict=False), 1154 progress_bar: bool = True, 1155 truncate_dim: Optional[int] = None) -> None 1156 ``` 1157 1158 Initializes the TextEmbedder with the specified model, task type, and GCP configuration. 1159 1160 **Arguments**: 1161 1162 - `model`: Name of the model to use. 1163 - `task_type`: The type of task for which the embeddings are being generated. 1164 For more information see the official [Google documentation](https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/text-embeddings-api#tasktype). 1165 - `gcp_region_name`: The default location to use when making API calls, if not set uses us-central-1. 1166 - `gcp_project_id`: ID of the GCP project to use. By default, it is set during Google Cloud authentication. 1167 - `progress_bar`: Whether to display a progress bar during processing. 1168 - `truncate_dim`: The dimension to truncate the embeddings to, if specified. 1169 1170 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder.VertexAITextEmbedder.run"></a> 1171 1172 #### VertexAITextEmbedder.run 1173 1174 ```python 1175 @component.output_types(embedding=list[float]) 1176 def run(text: Union[list[Document], list[str], str]) 1177 ``` 1178 1179 Processes text in batches while adhering to the API's token limit per request. 1180 1181 **Arguments**: 1182 1183 - `text`: The text to embed. 1184 1185 **Returns**: 1186 1187 A dictionary with the following keys: 1188 - `embedding`: The embedding of the input text. 1189 1190 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder.VertexAITextEmbedder.to_dict"></a> 1191 1192 #### VertexAITextEmbedder.to\_dict 1193 1194 ```python 1195 def to_dict() -> dict[str, Any] 1196 ``` 1197 1198 Serializes the component to a dictionary. 1199 1200 **Returns**: 1201 1202 Dictionary with serialized data. 1203 1204 <a id="haystack_integrations.components.embedders.google_vertex.text_embedder.VertexAITextEmbedder.from_dict"></a> 1205 1206 #### VertexAITextEmbedder.from\_dict 1207 1208 ```python 1209 @classmethod 1210 def from_dict(cls, data: dict[str, Any]) -> "VertexAITextEmbedder" 1211 ``` 1212 1213 Deserializes the component from a dictionary. 1214 1215 **Arguments**: 1216 1217 - `data`: Dictionary to deserialize from. 1218 1219 **Returns**: 1220 1221 Deserialized component.