/ docs-website / versioned_docs / version-2.27 / pipeline-components / generators / googleaigeminigenerator.mdx
googleaigeminigenerator.mdx
1 --- 2 title: "GoogleAIGeminiGenerator" 3 id: googleaigeminigenerator 4 slug: "/googleaigeminigenerator" 5 description: "This component enables text generation using the Google Gemini models." 6 --- 7 8 # GoogleAIGeminiGenerator 9 10 This component enables text generation using the Google Gemini models. 11 12 :::warning[Deprecation Notice] 13 14 This integration uses the deprecated google-generativeai SDK, which will lose support after August 2025. 15 16 We recommend switching to the new [GoogleGenAIChatGenerator](googlegenaichatgenerator.mdx) integration instead. 17 ::: 18 19 <div className="key-value-table"> 20 21 | | | 22 | --- | --- | 23 | **Most common position in a pipeline** | After a [`PromptBuilder`](../builders/promptbuilder.mdx) | 24 | **Mandatory init variables** | `api_key`: A Google AI Studio API key. Can be set with `GOOGLE_API_KEY` env var. | 25 | **Mandatory run variables** | `parts`: A variadic list containing a mix of images, audio, video, and text to prompt Gemini | 26 | **Output variables** | `replies`: A list of strings or dictionaries with all the replies generated by the model | 27 | **API reference** | [Google AI](/reference/integrations-google-ai) | 28 | **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_ai | 29 30 </div> 31 32 `GoogleAIGeminiGenerator` supports `gemini-2.5-pro-exp-03-25`, `gemini-2.0-flash`, `gemini-1.5-pro`, and `gemini-1.5-flash` models. 33 34 For available models, see https://ai.google.dev/gemini-api/docs/models/gemini. 35 36 ### Parameters Overview 37 38 `GoogleAIGeminiGenerator` uses a Google AI Studio API key for authentication. You can write this key in an `api_key` parameter or as a `GOOGLE_API_KEY` environment variable (recommended). 39 40 To get an API key, visit the [Google AI Studio](https://ai.google.dev/gemini-api/docs/api-key) website. 41 42 ### Streaming 43 44 This Generator supports [streaming](guides-to-generators/choosing-the-right-generator.mdx#streaming-support) the tokens from the LLM directly in output. To do so, pass a function to the `streaming_callback` init parameter. 45 46 ## Usage 47 48 Start by installing the `google-ai-haystack` package to use the `GoogleAIGeminiGenerator`: 49 50 ```shell 51 pip install google-ai-haystack 52 ``` 53 54 ### On its own 55 56 Basic usage: 57 58 ```python 59 import os 60 from haystack_integrations.components.generators.google_ai import GoogleAIGeminiGenerator 61 62 os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>" 63 64 gemini = GoogleAIGeminiGenerator(model="gemini-1.5-pro") 65 res = gemini.run(parts = ["What is the most interesting thing you know?"]) 66 for answer in res["replies"]: 67 print(answer) 68 69 >>> 1. **The Fermi Paradox:** This paradox questions why we haven't found any signs of extraterrestrial life, despite the vastness of the universe and the high probability of life existing elsewhere. 70 >>> 2. **The Goldilocks Enigma:** This conundrum explores why Earth has such favorable conditions for life, despite the extreme conditions found in most of the universe. It raises questions about the rarity or commonality of Earth-like planets. 71 >>> 3. **The Quantum Enigma:** Quantum mechanics, the study of the behavior of matter and energy at the atomic and subatomic level, presents many counterintuitive phenomena that challenge our understanding of reality. Questions about the nature of quantum entanglement, superposition, and the origin of quantum mechanics remain unsolved. 72 >>> 4. **The Origin of Consciousness:** The emergence of consciousness from non-conscious matter is one of the biggest mysteries in science. How and why subjective experiences arise from physical processes in the brain remains a perplexing question. 73 >>> 5. **The Nature of Dark Matter and Dark Energy:** Dark matter and dark energy are mysterious substances that make up most of the universe, but their exact nature and properties are still unknown. Understanding their role in the universe's expansion and evolution is a major cosmological challenge. 74 >>> 6. **The Future of Artificial Intelligence:** The rapid development of Artificial Intelligence (AI) raises fundamental questions about the potential consequences and implications for society, including ethical issues, job displacement, and the long-term impact on human civilization. 75 >>> 7. **The Search for Life Beyond Earth:** As we continue to explore our solar system and beyond, the search for life on other planets or moons is a captivating and ongoing endeavor. Discovering extraterrestrial life would have profound implications for our understanding of the universe and our place in it. 76 >>> 8. **Time Travel:** The concept of time travel, whether forward or backward, remains a theoretical possibility that challenges our understanding of causality and the laws of physics. The implications and paradoxes associated with time travel have fascinated scientists and philosophers alike. 77 >>> 9. **The Multiverse Theory:** The multiverse theory suggests the existence of multiple universes, each with its own set of physical laws and properties. This idea raises questions about the nature of reality, the role of chance and necessity, and the possibility of parallel universes. 78 >>> 10. **The Fate of the Universe:** The ultimate fate of the universe is a subject of ongoing debate among cosmologists. Various theories, such as the Big Crunch, the Big Freeze, or the Big Rip, attempt to explain how the universe will end or evolve over time. Understanding the universe's destiny is a profound and awe-inspiring pursuit. 79 ``` 80 81 This is a more advanced usage that also uses text and images as input: 82 83 ```python 84 import requests 85 import os 86 from haystack.dataclasses.byte_stream import ByteStream 87 from haystack_integrations.components.generators.google_ai import GoogleAIGeminiGenerator 88 89 URLS = [ 90 "https://raw.githubusercontent.com/silvanocerza/robots/main/robot1.jpg", 91 "https://raw.githubusercontent.com/silvanocerza/robots/main/robot2.jpg", 92 "https://raw.githubusercontent.com/silvanocerza/robots/main/robot3.jpg", 93 "https://raw.githubusercontent.com/silvanocerza/robots/main/robot4.jpg" 94 ] 95 images = [ 96 ByteStream(data=requests.get(url).content, mime_type="image/jpeg") 97 for url in URLS 98 ] 99 100 os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>" 101 102 gemini = GoogleAIGeminiGenerator(model="gemini-1.5-pro") 103 result = gemini.run(parts = ["What can you tell me about this robots?", *images]) 104 for answer in result["replies"]: 105 print(answer) 106 107 >>> The first image is of C-3PO and R2-D2 from the Star Wars franchise. C-3PO is a protocol droid, while R2-D2 is an astromech droid. They are both loyal companions to the heroes of the Star Wars saga. 108 >>> The second image is of Maria from the 1927 film Metropolis. Maria is a robot who is created to be the perfect woman. She is beautiful, intelligent, and obedient. However, she is also soulless and lacks any real emotions. 109 >>> The third image is of Gort from the 1951 film The Day the Earth Stood Still. Gort is a robot who is sent to Earth to warn humanity about the dangers of nuclear war. He is a powerful and intelligent robot, but he is also compassionate and understanding. 110 >>> The fourth image is of Marvin from the 1977 film The Hitchhiker's Guide to the Galaxy. Marvin is a robot who is depressed and pessimistic. He is constantly complaining about everything, but he is also very intelligent and has a dry sense of humor. 111 ``` 112 113 ### In a pipeline 114 115 In a RAG pipeline: 116 117 ```python 118 import os 119 from haystack.components.retrievers.in_memory import InMemoryBM25Retriever 120 from haystack.components.builders import PromptBuilder 121 from haystack import Pipeline 122 from haystack.document_stores.in_memory import InMemoryDocumentStore 123 from haystack_integrations.components.generators.google_ai import ( 124 GoogleAIGeminiGenerator, 125 ) 126 127 os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>" 128 129 docstore = InMemoryDocumentStore() 130 131 template = """ 132 Given the following information, answer the question. 133 134 Context: 135 {% for document in documents %} 136 {{ document.content }} 137 {% endfor %} 138 139 Question: What's the official language of {{ country }}? 140 """ 141 pipe = Pipeline() 142 143 pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore)) 144 pipe.add_component("prompt_builder", PromptBuilder(template=template)) 145 pipe.add_component("gemini", GoogleAIGeminiGenerator(model="gemini-pro")) 146 pipe.connect("retriever", "prompt_builder.documents") 147 pipe.connect("prompt_builder", "gemini") 148 149 pipe.run({"prompt_builder": {"country": "France"}}) 150 ```