Skip to content

Commit

Permalink
chore: update docs
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 720733411
  • Loading branch information
sasha-gitg authored and copybara-github committed Jan 28, 2025
1 parent 4f7b3f3 commit ed7f356
Show file tree
Hide file tree
Showing 7 changed files with 4,754 additions and 1,655 deletions.
194 changes: 122 additions & 72 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ with text content
.. code:: python
response = client.models.generate_content(
model="gemini-2.0-flash-exp", contents="What is your name?"
model="gemini-2.0-flash-exp", contents="Why is the sky blue?"
)
print(response.text)
with uploaded file (Google AI only)
with uploaded file (Gemini API only)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

download the file in console.
Expand All @@ -85,9 +85,10 @@ python code.

.. code:: python
file = client.files.upload(path='a11.text')
file = client.files.upload(path='a11.txt')
response = client.models.generate_content(
model='gemini-2.0-flash-exp', contents=['Summarize this file', file]
model='gemini-2.0-flash-exp',
contents=['Could you summarize this file?', file]
)
print(response.text)
Expand Down Expand Up @@ -117,7 +118,7 @@ dictionaries. You can get the type from ``google.genai.types``.
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=types.Part.from_text("Why is the sky blue?"),
contents=types.Part.from_text(text="Why is the sky blue?"),
config=types.GenerateContentConfig(
temperature=0,
top_p=0.95,
Expand All @@ -131,7 +132,46 @@ dictionaries. You can get the type from ``google.genai.types``.
),
)
response
print(response.text)
Thinking Model
---------------

The Gemini 2.0 Flash Thinking model is an experimental model that could return
"thoughts" as part of its response.

Gemini Developer API
~~~~~~~~~~~~~~~~~~~~

Thinking config is only available in v1alpha for Gemini AI API.

.. code:: python
response = client.models.generate_content(
model='gemini-2.0-flash-thinking-exp',
contents='What is the sum of natural numbers from 1 to 100?',
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(include_thoughts=True),
http_options=types.HttpOptions(api_version='v1alpha'),
)
)
for part in response.candidates[0].content.parts:
print(part)
Vertex AI API
~~~~~~~~~~~~~~

.. code:: python
response = client.models.generate_content(
model='gemini-2.0-flash-thinking-exp-01-21',
contents='What is the sum of natural numbers from 1 to 100?',
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(include_thoughts=True),
)
)
for part in response.candidates[0].content.parts:
print(part)
List Base Models
----------------
Expand Down Expand Up @@ -230,10 +270,10 @@ Then you will receive a function call part in the response.
function = types.FunctionDeclaration(
name="get_current_weather",
description="Get the current weather in a given location",
parameters=types.FunctionParameters(
parameters=types.Schema(
type="OBJECT",
properties={
"location": types.ParameterType(
"location": types.Schema(
type="STRING",
description="The city and state, e.g. San Francisco, CA",
),
Expand Down Expand Up @@ -262,10 +302,9 @@ The following example shows how to do it for a simple function invocation.
user_prompt_content = types.Content(
role="user",
parts=[types.Part.from_text("What is the weather like in Boston?")],
parts=[types.Part.from_text(text="What is the weather like in Boston?")],
)
function_call_content = response.candidates[0].content
function_call_part = function_call_content.parts[0]
function_call_part = response.function_calls[0]
try:
Expand Down Expand Up @@ -366,6 +405,60 @@ Schemas can be provided as Pydantic Models.
)
print(response.text)
Enum Response Schema
--------------------

Text Response
~~~~~~~~~~~~~

You can set response_mime_type to 'text/x.enum' to return one of those enum
values as the response.

.. code:: python
from enum import Enum
class InstrumentEnum(Enum):
PERCUSSION = 'Percussion'
STRING = 'String'
WOODWIND = 'Woodwind'
BRASS = 'Brass'
KEYBOARD = 'Keyboard'
response = client.models.generate_content(
model='gemini-2.0-flash-exp',
contents='What instrument plays multiple notes at once?',
config={
'response_mime_type': 'text/x.enum',
'response_schema': InstrumentEnum,
},
)
print(response.text)
JSON Response
~~~~~~~~~~~~~

You can also set response_mime_type to 'application/json', the response will be
identical but in quotes.

.. code:: python
class InstrumentEnum(Enum):
PERCUSSION = 'Percussion'
STRING = 'String'
WOODWIND = 'Woodwind'
BRASS = 'Brass'
KEYBOARD = 'Keyboard'
response = client.models.generate_content(
model='gemini-2.0-flash-exp',
contents='What instrument plays multiple notes at once?',
config={
'response_mime_type': 'application/json',
'response_schema': InstrumentEnum,
},
)
print(response.text)
Streaming
---------
Expand Down Expand Up @@ -439,10 +532,10 @@ Streaming

.. code:: python
async for response in client.aio.models.generate_content_stream(
async for chunk in await client.aio.models.generate_content_stream(
model="gemini-2.0-flash-exp", contents="Tell me a story in 300 words."
):
print(response.text, end="")
print(chunk.text, end="")
Count Tokens and Compute Tokens
-------------------------------
Expand All @@ -451,7 +544,7 @@ Count Tokens and Compute Tokens
response = client.models.count_tokens(
model="gemini-2.0-flash-exp",
contents="What is your name?",
contents="why is the sky blue?",
)
print(response)
Expand All @@ -464,7 +557,7 @@ Compute tokens is only supported in Vertex AI.
response = client.models.compute_tokens(
model="gemini-2.0-flash-exp",
contents="What is your name?",
contents="why is the sky blue?",
)
print(response)
Expand All @@ -475,7 +568,7 @@ Async
response = await client.aio.models.count_tokens(
model="gemini-2.0-flash-exp",
contents="What is your name?",
contents="why is the sky blue?",
)
print(response)
Expand All @@ -486,7 +579,7 @@ Embed Content
response = client.models.embed_content(
model="text-embedding-004",
contents="What is your name?",
contents="why is the sky blue?",
)
print(response)
Expand All @@ -495,7 +588,7 @@ Embed Content
# multiple contents with config
response = client.models.embed_content(
model="text-embedding-004",
contents=["What is your name?", "What is your age?"],
contents=["why is the sky blue?", "What is your age?"],
config=types.EmbedContentConfig(output_dimensionality=10),
)
Expand All @@ -512,10 +605,10 @@ Support for generate image in Gemini Developer API is behind an allowlist
.. code:: python
# Generate Image
response1 = client.models.generate_image(
model="imagen-3.0-generate-001",
response1 = client.models.generate_images(
model="imagen-3.0-generate-002",
prompt="An umbrella in the foreground, and a rainy night sky in the background",
config=types.GenerateImageConfig(
config=types.GenerateImagesConfig(
negative_prompt="human",
number_of_images=1,
include_rai_reason=True,
Expand All @@ -533,7 +626,7 @@ Upscale image is only supported in Vertex AI.
# Upscale the generated image from above
response2 = client.models.upscale_image(
model="imagen-3.0-generate-001",
model="imagen-3.0-generate-002",
image=response1.generated_images[0].image,
upscale_factor="x2",
config=types.UpscaleImageConfig(
Expand Down Expand Up @@ -621,7 +714,7 @@ Async Streaming
.. code:: python
chat = client.aio.chats.create(model="gemini-2.0-flash-exp")
async for chunk in chat.send_message_stream("tell me a story"):
async for chunk in await chat.send_message_stream("tell me a story"):
print(chunk.text, end="")
Files
Expand Down Expand Up @@ -720,7 +813,7 @@ Tunings
=======

``client.tunings`` contains tuning job APIs and supports supervised fine
tuning through ``tune`` and distillation through ``distill``
tuning through ``tune``.

Tune
----
Expand Down Expand Up @@ -789,7 +882,7 @@ Use Tuned Model
response = client.models.generate_content(
model=tuning_job.tuned_model.endpoint,
contents="What is your name?",
contents="why is the sky blue?",
)
print(response.text)
Expand Down Expand Up @@ -852,49 +945,6 @@ Update Tuned Model
print(model)
Distillation
------------

Only supported in Vertex AI. Requires allowlist.

.. code:: python
distillation_job = client.tunings.distill(
student_model="gemma-2b-1.1-it",
teacher_model="gemini-1.5-pro-002",
training_dataset=genai.types.DistillationDataset(
gcs_uri="gs://cloud-samples-data/ai-platform/generative_ai/gemini-1_5/text/sft_train_data.jsonl",
),
config=genai.types.CreateDistillationJobConfig(
epoch_count=1,
pipeline_root_directory=(
"gs://vertex-sdk-dev-staging-us-central1/tmp/distillation_pipeline_root"
),
),
)
print(distillation_job)
.. code:: python
completed_states = set(
[
"JOB_STATE_SUCCEEDED",
"JOB_STATE_FAILED",
"JOB_STATE_CANCELLED",
"JOB_STATE_PAUSED",
]
)
while distillation_job.state not in completed_states:
print(distillation_job.state)
distillation_job = client.tunings.get(name=distillation_job.name)
time.sleep(10)
print(distillation_job)
.. code:: python
distillation_job
List Tuning Jobs
----------------
Expand Down Expand Up @@ -976,12 +1026,12 @@ List

.. code:: python
for job in client.batches.list(config=types.ListBatchJobConfig(page_size=10)):
for job in client.batches.list(config=types.ListBatchJobsConfig(page_size=10)):
print(job)
.. code:: python
pager = client.batches.list(config=types.ListBatchJobConfig(page_size=10))
pager = client.batches.list(config=types.ListBatchJobsConfig(page_size=10))
print(pager.page_size)
print(pager[0])
pager.next_page()
Expand All @@ -993,14 +1043,14 @@ Async
.. code:: python
async for job in await client.aio.batches.list(
config=types.ListBatchJobConfig(page_size=10)
config=types.ListBatchJobsConfig(page_size=10)
):
print(job)
.. code:: python
async_pager = await client.aio.batches.list(
config=types.ListBatchJobConfig(page_size=10)
config=types.ListBatchJobsConfig(page_size=10)
)
print(async_pager.page_size)
print(async_pager[0])
Expand Down
Loading

0 comments on commit ed7f356

Please sign in to comment.