This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Pixeltable’s Gemini integration enables you to access the Gemini LLM via
the Google Gemini API.
Prerequisites
Important Notes
- Google AI Studio usage may incur costs based on your plan.
- Be mindful of sensitive data and consider security measures when
integrating with external services.
First you’ll need to install required libraries and enter a Gemini API
key obtained via Google AI Studio.
%pip install -qU pixeltable google-genai
import os
import getpass
if 'GEMINI_API_KEY' not in os.environ:
os.environ['GEMINI_API_KEY'] = getpass.getpass('Google AI Studio API Key:')
Now let’s create a Pixeltable directory to hold the tables for our demo.
import pixeltable as pxt
# Remove the 'gemini_demo' directory and its contents, if it exists
pxt.drop_dir('gemini_demo', force=True)
pxt.create_dir('gemini_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory ‘gemini_demo’.
<pixeltable.catalog.dir.Dir at 0x304985c60>
Generate Content
Create a Table: In Pixeltable, create a table with columns to represent
your input data and the columns where you want to store the results from
Gemini.
from google.genai.types import GenerateContentConfigDict
from pixeltable.functions import gemini
# Create a table in Pixeltable and pick a model hosted on Google AI Studio with some parameters
t = pxt.create_table('gemini_demo.text', {'input': pxt.String})
config = GenerateContentConfigDict(
stop_sequences=['\n'],
max_output_tokens=300,
temperature=1.0,
top_p=0.95,
top_k=40,
)
t.add_computed_column(output=gemini.generate_content(
t.input,
model='gemini-2.0-flash',
config=config
))
Created table `text`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
# Ask Gemini to generate some content based on the input
t.insert([
{'input': 'Write a story about a magic backpack.'},
{'input': 'Tell me a science joke.'}
])
Inserting rows into `text`: 2 rows [00:00, 176.84 rows/s]
Inserted 2 rows with 0 errors.
UpdateStatus(num_rows=2, num_computed_values=4, num_excs=0, updated_cols=[], cols_with_excs=[])
# Parse the response into a new column
t.add_computed_column(response=t.output['candidates'][0]['content']['parts'][0]['text'])
t.select(t.input, t.response).head()
Added 2 column values with 0 errors.
Generate Images with Imagen
from google.genai.types import GenerateImagesConfigDict
images_t = pxt.create_table('gemini_demo.images', {'prompt': pxt.String})
config = GenerateImagesConfigDict(aspect_ratio='16:9')
images_t.add_computed_column(generated_image=gemini.generate_images(
images_t.prompt,
model='imagen-4.0-generate-001',
config=config
))
Created table `images`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
images_t.insert([{'prompt': 'A friendly dinosaur playing tennis in a cornfield'}])
Inserting rows into `images`: 1 rows [00:00, 382.10 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
Generate Video with Veo
videos_t = pxt.create_table('gemini_demo.videos', {'prompt': pxt.String})
videos_t.add_computed_column(generated_video=gemini.generate_videos(
videos_t.prompt,
model='veo-2.0-generate-001',
))
Created table `videos`.
Added 0 column values with 0 errors.
UpdateStatus(num_rows=0, num_computed_values=0, num_excs=0, updated_cols=[], cols_with_excs=[])
videos_t.insert([{'prompt': 'A giant pixel floating over the open ocean in a sea of data'}])
Inserting rows into `videos`: 1 rows [00:00, 65.14 rows/s]
Inserted 1 row with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=2, num_excs=0, updated_cols=[], cols_with_excs=[])
Generate Video from an existing Image
We’ll add an additional computed column to our existing images_t to
animate the generated images.
images_t.add_computed_column(generated_video=gemini.generate_videos(
image=images_t.generated_image,
model='veo-2.0-generate-001',
))
Added 1 column value with 0 errors.
UpdateStatus(num_rows=1, num_computed_values=1, num_excs=0, updated_cols=[], cols_with_excs=[])
Learn More
To learn more about advanced techniques like RAG operations in
Pixeltable, check out the RAG Operations in
Pixeltable
tutorial.
If you have any questions, don’t hesitate to reach out.