GoogleAIGeminiGenerator
This component enables text generation using the Google Gemini models.
Name | GoogleAIGeminiGenerator |
Source | https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/google_ai |
Most common position in a pipeline | After a PromptBuilder |
Mandatory input variables | “parts”: A variadic list containing a mix of images, audio, video, and text to prompt Gemini |
Output variables | “replies”: A list of strings or dictionaries with all the replies generated by the model |
GoogleAIGeminiGenerator
supports gemini-pro
, gemini-ultra
, and gemini-pro-vision
models.
Prompting with images requires gemini-pro-vision
. Function calling, instead, requires gemini-pro
.
Parameters Overview
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).
To get an API key, visit the Google AI Studio website.
Usage
Start by installing the google-ai-haystack
package to use the GoogleAIGeminiGenerator
:
pip install google-ai-haystack
On its own
Basic usage:
import os
from haystack_integrations.components.generators.google_ai import GoogleAIGeminiGenerator
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
gemini = GoogleAIGeminiGenerator(model="gemini-pro")
res = gemini.run(parts = ["What is the most interesting thing you know?"])
for answer in res["answers"]:
print(answer)
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
>>> 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.
This is a more advanced usage that also uses text and images as input:
import requests
import os
from haystack.dataclasses.byte_stream import ByteStream
from haystack_integrations.components.generators.google_ai import GoogleAIGeminiGenerator
URLS = [
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot1.jpg",
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot2.jpg",
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot3.jpg",
"https://raw.githubusercontent.com/silvanocerza/robots/main/robot4.jpg"
]
images = [
ByteStream(data=requests.get(url).content, mime_type="image/jpeg")
for url in URLS
]
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
gemini = GoogleAIGeminiGenerator(model="gemini-pro-vision")
result = gemini.run(parts = ["What can you tell me about this robots?", *images])
for answer in result["answers"]:
print(answer)
>>> 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.
>>> 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.
>>> 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.
>>> 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.
In a pipeline
In a RAG pipeline:
import os
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.builders import PromptBuilder
from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.generators.google_ai import GoogleAIGeminiGenerator
os.environ["GOOGLE_API_KEY"] = "<MY_API_KEY>"
docstore = InMemoryDocumentStore()
template = """
Given the following information, answer the question.
Context:
{% for document in documents %}
{{ document.content }}
{% endfor %}
Question: What's the official language of {{ country }}?
"""
pipe = Pipeline()
pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("gemini", GoogleAIGeminiGenerator(model="gemini-pro")
pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "gemini")
pipe.run({
"prompt_builder": {
"country": "France"
}
})
Updated 7 months ago