deprecated-generative-ai-python
This SDK is now deprecated, use the new unified GenAI SDK.
Top Related Projects
The official Python library for the OpenAI API
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
Ongoing research training transformer models at scale
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
TensorFlow code and pre-trained models for BERT
Quick Overview
The google-gemini/generative-ai-python repository is an official Python library for interacting with Google's Gemini AI models. It provides a simple and efficient way to access Gemini's capabilities, including text generation, image analysis, and multimodal tasks, through a Python API.
Pros
- Easy integration with Google's state-of-the-art Gemini AI models
- Supports various tasks including text generation, image analysis, and multimodal interactions
- Well-documented with clear examples and explanations
- Regular updates and maintenance from Google's team
Cons
- Requires a Google Cloud account and API key for usage
- Limited to Google's Gemini models, not applicable for other AI providers
- Potential costs associated with API usage, depending on the scale of implementation
- May have usage limitations or quotas based on Google Cloud policies
Code Examples
- Text generation:
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Explain the theory of relativity in simple terms.")
print(response.text)
- Image analysis:
import google.generativeai as genai
from PIL import Image
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro-vision')
image = Image.open('path/to/your/image.jpg')
response = model.generate_content(["Describe this image in detail", image])
print(response.text)
- Multimodal interaction:
import google.generativeai as genai
from PIL import Image
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro-vision')
image = Image.open('path/to/your/image.jpg')
response = model.generate_content([
"What's unusual about this image?",
image,
"Provide a creative story based on what you see."
])
print(response.text)
Getting Started
To get started with the google-gemini/generative-ai-python library:
-
Install the library:
pip install google-generativeai
-
Set up a Google Cloud account and obtain an API key.
-
Configure the library with your API key:
import google.generativeai as genai genai.configure(api_key="YOUR_API_KEY")
-
Create a model instance and start generating content:
model = genai.GenerativeModel('gemini-pro') response = model.generate_content("Your prompt here") print(response.text)
Remember to replace "YOUR_API_KEY" with your actual Google Cloud API key.
Competitor Comparisons
The official Python library for the OpenAI API
Pros of openai-python
- More mature and widely adopted library with extensive documentation
- Supports a broader range of OpenAI models and services
- Active community and frequent updates
Cons of openai-python
- Limited to OpenAI's ecosystem and pricing model
- May require more setup and configuration for basic usage
Code Comparison
openai-python:
import openai
openai.api_key = "your-api-key"
response = openai.Completion.create(
engine="text-davinci-002",
prompt="Translate the following English text to French: '{}'",
max_tokens=60
)
generative-ai-python:
import google.generativeai as genai
genai.configure(api_key="your-api-key")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Translate the following English text to French: '{}'")
Both libraries offer similar functionality for generating content, but generative-ai-python has a more streamlined API for basic usage. openai-python provides more granular control over model parameters, while generative-ai-python focuses on simplicity and ease of use for Google's Gemini models.
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
Pros of transformers
- Extensive model support: Covers a wide range of transformer-based models
- Active community: Large user base and frequent updates
- Comprehensive documentation: Detailed guides and examples
Cons of transformers
- Steeper learning curve: More complex API due to broader scope
- Larger package size: Includes many features, which may be unnecessary for simple tasks
Code Comparison
transformers:
from transformers import pipeline
generator = pipeline('text-generation', model='gpt2')
result = generator("Hello, I'm a language model,", max_length=30)
print(result[0]['generated_text'])
generative-ai-python:
import google.generativeai as genai
genai.configure(api_key='YOUR_API_KEY')
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Hello, I'm a language model,")
print(response.text)
Both libraries provide easy-to-use interfaces for generating text, but transformers offers more flexibility in model selection, while generative-ai-python focuses on Google's Gemini models with a simpler API.
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
Pros of DeepSpeed
- Highly optimized for distributed training and large-scale models
- Offers advanced memory optimization techniques like ZeRO
- Supports a wide range of hardware configurations and cloud platforms
Cons of DeepSpeed
- Steeper learning curve due to its complexity and advanced features
- Requires more setup and configuration compared to simpler libraries
- May be overkill for smaller projects or single-GPU training
Code Comparison
DeepSpeed:
import deepspeed
model_engine, optimizer, _, _ = deepspeed.initialize(args=args,
model=model,
model_parameters=params)
for step, batch in enumerate(data_loader):
loss = model_engine(batch)
model_engine.backward(loss)
model_engine.step()
generative-ai-python:
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Tell me a joke")
print(response.text)
The DeepSpeed code focuses on distributed training optimization, while generative-ai-python provides a simpler interface for generating content using pre-trained models. DeepSpeed is more suitable for training large models, whereas generative-ai-python is designed for easy integration of Gemini's capabilities into applications.
Ongoing research training transformer models at scale
Pros of Megatron-LM
- Designed for large-scale distributed training of transformer models
- Supports model parallelism for efficient use of multiple GPUs
- Optimized for NVIDIA hardware, potentially offering better performance
Cons of Megatron-LM
- More complex setup and configuration required
- Primarily focused on training, less emphasis on inference and deployment
- Steeper learning curve for beginners
Code Comparison
Megatron-LM (model initialization):
model = get_model(
model_provider_func,
wrap_with_ddp=True,
virtual_pipeline_model_parallel_size=None
)
generative-ai-python (model usage):
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Hello, Gemini!")
The Megatron-LM code focuses on model initialization for distributed training, while the generative-ai-python code demonstrates simple API usage for generating content with pre-trained models. Megatron-LM is more suited for researchers and advanced users working on large-scale model training, whereas generative-ai-python provides an accessible interface for developers to integrate Gemini models into their applications.
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
Pros of gpt-neox
- Open-source implementation of GPT-3-like models
- Supports distributed training across multiple GPUs and nodes
- Highly customizable architecture and training parameters
Cons of gpt-neox
- More complex setup and configuration compared to generative-ai-python
- Requires significant computational resources for training large models
- Less user-friendly for beginners or those unfamiliar with deep learning frameworks
Code Comparison
gpt-neox:
from megatron.neox_arguments import NeoXArgs
from megatron.global_vars import set_global_variables, get_tokenizer
from megatron.neox_model import GPTNeoX
args = NeoXArgs.from_ymls("configs/your_config.yml")
model = GPTNeoX(args)
generative-ai-python:
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Hello, Gemini!")
The gpt-neox code snippet demonstrates the setup for a custom GPT-like model, while the generative-ai-python code shows a simpler API-based approach for using pre-trained Gemini models. gpt-neox offers more flexibility and control over the model architecture, but requires more setup and understanding of the underlying framework. generative-ai-python provides an easier-to-use interface for quick integration of AI capabilities into applications, but with less customization options.
TensorFlow code and pre-trained models for BERT
Pros of BERT
- Established and widely adopted in NLP tasks
- Extensive documentation and community support
- Pre-trained models available for various languages
Cons of BERT
- Focused primarily on text understanding, not generation
- Requires more computational resources for fine-tuning
- Less suitable for multi-modal or creative AI tasks
Code Comparison
BERT example:
from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertModel.from_pretrained('bert-base-uncased')
inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs)
Generative AI Python example:
import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content("Write a short poem about AI")
print(response.text)
The BERT code focuses on tokenization and model initialization for text understanding, while the Generative AI Python code demonstrates a simpler interface for generating creative content using Gemini models.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
[Deprecated] Google AI Python SDK for the Gemini API
With Gemini 2.0, we took the chance to create a single unified SDK for all developers who want to use Google's GenAI models (Gemini, Veo, Imagen, etc). As part of that process, we took all of the feedback from this SDK and what developers like about other SDKs in the ecosystem to create the Google Gen AI SDK.
The full migration guide from the old SDK to new SDK is available in the Gemini API docs.
We won't be adding anything to this SDK or making any further changes. The Gemini API docs are fully updated to show examples of the new Google Gen AI SDK. We know how disruptive an SDK change can be and don't take this change lightly, but our goal is to create an extremely simple and clear path for developers to build with our models so it felt necessary to make this change.
Thank you for building with Gemini and let us know if you need any help!
Top Related Projects
The official Python library for the OpenAI API
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
DeepSpeed is a deep learning optimization library that makes distributed training and inference easy, efficient, and effective.
Ongoing research training transformer models at scale
An implementation of model parallel autoregressive transformers on GPUs, based on the Megatron and DeepSpeed libraries
TensorFlow code and pre-trained models for BERT
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot