Skip to content

Commit

Permalink
Project import generated by Copybara. (#24)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707169452

Co-authored-by: Googler <[email protected]>
  • Loading branch information
sasha-gitg and Googler authored Dec 17, 2024
1 parent cf3c476 commit 1d57271
Show file tree
Hide file tree
Showing 32 changed files with 959 additions and 181 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

-----

## Installation

``` cmd
pip install google-genai
```

## Imports

``` python
Expand Down Expand Up @@ -427,6 +433,8 @@ response2.generated_images[0].image.show()

#### Edit Image

Edit image uses a separate model from generate and upscale.

Edit image is not supported in Google AI.

``` python
Expand All @@ -447,7 +455,7 @@ mask_ref_image = MaskReferenceImage(
)

response3 = client.models.edit_image(
model='imagen-3.0-capability-preview-0930',
model='imagen-3.0-capability-001',
prompt='Sunlight and clear sky',
reference_images=[raw_ref_image, mask_ref_image],
config=types.EditImageConfig(
Expand Down Expand Up @@ -514,9 +522,9 @@ cached_content = client.caches.create(
file_uri=file_uris[1],
mime_type='application/pdf',)])
],
system_instruction='What is the sum of the two pdfs?',
config=types.CreateCachedContentConfig(
display_name='test cache',
system_instruction='What is the sum of the two pdfs?',
ttl='3600s',
),
)
Expand Down Expand Up @@ -809,4 +817,4 @@ print(async_pager[0])
delete_job = client.batches.delete(name=job.name)

delete_job
```
```
7 changes: 7 additions & 0 deletions docs/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ integrate Google's generative models into their Python applications. This is an
early release (version 0.1.0). API is subject to change. Please do not use this
SDK in production.

Installation
============

.. code:: console
pip install google-genai
Imports
=======

Expand Down
283 changes: 278 additions & 5 deletions docs/genai.html

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -3461,6 +3461,12 @@ <h2>T</h2>

<ul>
<li><a href="genai.html#genai.types.TuningExampleDict.text_input">(genai.types.TuningExampleDict attribute)</a>
</li>
</ul></li>
<li><a href="genai.html#genai.types.Part.thought">thought (genai.types.Part attribute)</a>

<ul>
<li><a href="genai.html#genai.types.PartDict.thought">(genai.types.PartDict attribute)</a>
</li>
</ul></li>
<li><a href="genai.html#genai.types.SafetySetting.threshold">threshold (genai.types.SafetySetting attribute)</a>
Expand Down
8 changes: 8 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ <h1>Google Gen AI SDK<a class="headerlink" href="#google-gen-ai-sdk" title="Link
early release (version 0.1.0). API is subject to change. Please do not use this
SDK in production.</p>
</section>
<section id="installation">
<h1>Installation<a class="headerlink" href="#installation" title="Link to this heading"></a></h1>
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="go">pip install google-genai</span>
</pre></div>
</div>
</section>
<section id="imports">
<h1>Imports<a class="headerlink" href="#imports" title="Link to this heading"></a></h1>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">google</span> <span class="kn">import</span> <span class="n">genai</span>
Expand Down Expand Up @@ -2583,6 +2589,7 @@ <h1>Reference<a class="headerlink" href="#reference" title="Link to this heading
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.function_response"><code class="docutils literal notranslate"><span class="pre">Part.function_response</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.inline_data"><code class="docutils literal notranslate"><span class="pre">Part.inline_data</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.text"><code class="docutils literal notranslate"><span class="pre">Part.text</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.thought"><code class="docutils literal notranslate"><span class="pre">Part.thought</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.video_metadata"><code class="docutils literal notranslate"><span class="pre">Part.video_metadata</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.from_bytes"><code class="docutils literal notranslate"><span class="pre">Part.from_bytes()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.from_code_execution_result"><code class="docutils literal notranslate"><span class="pre">Part.from_code_execution_result()</span></code></a></li>
Expand All @@ -2602,6 +2609,7 @@ <h1>Reference<a class="headerlink" href="#reference" title="Link to this heading
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.function_response"><code class="docutils literal notranslate"><span class="pre">PartDict.function_response</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.inline_data"><code class="docutils literal notranslate"><span class="pre">PartDict.inline_data</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.text"><code class="docutils literal notranslate"><span class="pre">PartDict.text</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.thought"><code class="docutils literal notranslate"><span class="pre">PartDict.thought</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.video_metadata"><code class="docutils literal notranslate"><span class="pre">PartDict.video_metadata</span></code></a></li>
</ul>
</li>
Expand Down
2 changes: 2 additions & 0 deletions docs/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,7 @@ <h1>google<a class="headerlink" href="#google" title="Link to this heading">¶</
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.function_response"><code class="docutils literal notranslate"><span class="pre">Part.function_response</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.inline_data"><code class="docutils literal notranslate"><span class="pre">Part.inline_data</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.text"><code class="docutils literal notranslate"><span class="pre">Part.text</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.thought"><code class="docutils literal notranslate"><span class="pre">Part.thought</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.video_metadata"><code class="docutils literal notranslate"><span class="pre">Part.video_metadata</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.from_bytes"><code class="docutils literal notranslate"><span class="pre">Part.from_bytes()</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.Part.from_code_execution_result"><code class="docutils literal notranslate"><span class="pre">Part.from_code_execution_result()</span></code></a></li>
Expand All @@ -1871,6 +1872,7 @@ <h1>google<a class="headerlink" href="#google" title="Link to this heading">¶</
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.function_response"><code class="docutils literal notranslate"><span class="pre">PartDict.function_response</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.inline_data"><code class="docutils literal notranslate"><span class="pre">PartDict.inline_data</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.text"><code class="docutils literal notranslate"><span class="pre">PartDict.text</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.thought"><code class="docutils literal notranslate"><span class="pre">PartDict.thought</span></code></a></li>
<li class="toctree-l3"><a class="reference internal" href="genai.html#genai.types.PartDict.video_metadata"><code class="docutils literal notranslate"><span class="pre">PartDict.video_metadata</span></code></a></li>
</ul>
</li>
Expand Down
Binary file modified docs/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/searchindex.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions google/genai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Changelog

## [0.3.0](https://github.com/googleapis/python-genai/compare/v0.2.2...v0.3.0) (2024-12-17)


### BREAKING CHANGES
* contents must be passed to CreateCachedContentConfig instead as a parameter to create_cached_content.

### Features

* Add support for Pydantic default value in Automatic Function Calling.
* Add support for thought.
* Add support for streaming chat.
2 changes: 1 addition & 1 deletion google/genai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

from .client import Client

__version__ = '0.2.2'
__version__ = '0.3.0'

__all__ = ['Client']
10 changes: 7 additions & 3 deletions google/genai/_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HttpOptions(TypedDict):
def _append_library_version_headers(headers: dict[str, str]) -> None:
"""Appends the telemetry header to the headers dict."""
# TODO: Automate revisions to the SDK library version.
library_label = f'google-genai-sdk/0.2.2'
library_label = f'google-genai-sdk/0.3.0'
language_label = 'gl-python/' + sys.version.split()[0]
version_header_value = f'{library_label} {language_label}'
if (
Expand Down Expand Up @@ -241,7 +241,9 @@ def _request(
) -> HttpResponse:
if self.vertexai:
if not self._credentials:
self._credentials, _ = google.auth.default()
self._credentials, _ = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
authed_session = AuthorizedSession(self._credentials)
authed_session.stream = stream
response = authed_session.request(
Expand Down Expand Up @@ -290,7 +292,9 @@ async def _async_request(
):
if self.vertexai:
if not self._credentials:
self._credentials, _ = google.auth.default()
self._credentials, _ = google.auth.default(
scopes=["https://www.googleapis.com/auth/cloud-platform"],
)
return await asyncio.to_thread(
self._request,
http_request,
Expand Down
13 changes: 5 additions & 8 deletions google/genai/_automatic_function_calling_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ def _parse_schema_from_parameter(
):
if not _is_default_value_compatible(param.default, param.annotation):
raise ValueError(default_value_error_msg)
# TODO: b/379715133 - handle pydantic model default value
schema.default = param.default
_raise_if_schema_unsupported(client, schema)
return schema
Expand Down Expand Up @@ -258,12 +257,11 @@ def _parse_schema_from_parameter(
# for user defined class, we only support pydantic model
and issubclass(param.annotation, pydantic.BaseModel)
):
if param.default is not inspect.Parameter.empty:
# TODO: b/379715133 - handle pydantic model default value
raise ValueError(
f'Default value {param.default} of Pydantic model{param} of function'
f' {func_name} is not supported.'
)
if (
param.default is not inspect.Parameter.empty
and param.default is not None
):
schema.default = param.default
schema.type = 'OBJECT'
schema.properties = {}
for field_name, field_info in param.annotation.model_fields.items():
Expand Down Expand Up @@ -294,4 +292,3 @@ def _get_required_fields(schema: types.Schema) -> list[str]:
for field_name, field_schema in schema.properties.items()
if not field_schema.nullable and field_schema.default is None
]

2 changes: 1 addition & 1 deletion google/genai/_replay_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def close(self):
with open(replay_file_path, 'w') as f:
f.write(
json.dumps(
self.replay_session.model_dump(), indent=2, cls=RequestJsonEncoder
self.replay_session.model_dump(), indent=2, cls=ResponseJsonEncoder
)
)
self.replay_session = None
Expand Down
15 changes: 15 additions & 0 deletions google/genai/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from typing import Any, Optional, Union

import PIL.Image
import PIL.PngImagePlugin

from . import _api_client
from . import types
Expand Down Expand Up @@ -298,6 +299,20 @@ def t_speech_config(
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name=origin)
)
)
if (
isinstance(origin, dict)
and 'voice_config' in origin
and 'prebuilt_voice_config' in origin['voice_config']
):
return types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(
voice_name=origin['voice_config']['prebuilt_voice_config'].get(
'voice_name'
)
)
)
)
raise ValueError(f'Unsupported speechConfig type: {type(origin)}')


Expand Down
58 changes: 30 additions & 28 deletions google/genai/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def _Part_to_mldev(
if getv(from_object, ['video_metadata']):
raise ValueError('video_metadata parameter is not supported in Google AI.')

if getv(from_object, ['thought']) is not None:
setv(to_object, ['thought'], getv(from_object, ['thought']))

if getv(from_object, ['code_execution_result']) is not None:
setv(
to_object,
Expand Down Expand Up @@ -74,6 +77,9 @@ def _Part_to_vertex(
if getv(from_object, ['video_metadata']) is not None:
setv(to_object, ['videoMetadata'], getv(from_object, ['video_metadata']))

if getv(from_object, ['thought']) is not None:
setv(to_object, ['thought'], getv(from_object, ['thought']))

if getv(from_object, ['code_execution_result']) is not None:
setv(
to_object,
Expand Down Expand Up @@ -638,6 +644,18 @@ def _CreateCachedContentConfig_to_mldev(
if getv(from_object, ['display_name']) is not None:
setv(parent_object, ['displayName'], getv(from_object, ['display_name']))

if getv(from_object, ['contents']) is not None:
setv(
parent_object,
['contents'],
[
_Content_to_mldev(api_client, item, to_object)
for item in t.t_contents(
api_client, getv(from_object, ['contents'])
)
],
)

if getv(from_object, ['system_instruction']) is not None:
setv(
parent_object,
Expand Down Expand Up @@ -689,6 +707,18 @@ def _CreateCachedContentConfig_to_vertex(
if getv(from_object, ['display_name']) is not None:
setv(parent_object, ['displayName'], getv(from_object, ['display_name']))

if getv(from_object, ['contents']) is not None:
setv(
parent_object,
['contents'],
[
_Content_to_vertex(api_client, item, to_object)
for item in t.t_contents(
api_client, getv(from_object, ['contents'])
)
],
)

if getv(from_object, ['system_instruction']) is not None:
setv(
parent_object,
Expand Down Expand Up @@ -735,18 +765,6 @@ def _CreateCachedContentParameters_to_mldev(
t.t_caches_model(api_client, getv(from_object, ['model'])),
)

if getv(from_object, ['contents']) is not None:
setv(
to_object,
['contents'],
[
_Content_to_mldev(api_client, item, to_object)
for item in t.t_contents(
api_client, getv(from_object, ['contents'])
)
],
)

if getv(from_object, ['config']) is not None:
setv(
to_object,
Expand All @@ -772,18 +790,6 @@ def _CreateCachedContentParameters_to_vertex(
t.t_caches_model(api_client, getv(from_object, ['model'])),
)

if getv(from_object, ['contents']) is not None:
setv(
to_object,
['contents'],
[
_Content_to_vertex(api_client, item, to_object)
for item in t.t_contents(
api_client, getv(from_object, ['contents'])
)
],
)

if getv(from_object, ['config']) is not None:
setv(
to_object,
Expand Down Expand Up @@ -1238,7 +1244,6 @@ def create(
self,
*,
model: str,
contents: Union[types.ContentListUnion, types.ContentListUnionDict],
config: Optional[types.CreateCachedContentConfigOrDict] = None,
) -> types.CachedContent:
"""Creates cached content, this call will initialize the cached
Expand All @@ -1264,7 +1269,6 @@ def create(

parameter_model = types._CreateCachedContentParameters(
model=model,
contents=contents,
config=config,
)

Expand Down Expand Up @@ -1542,7 +1546,6 @@ async def create(
self,
*,
model: str,
contents: Union[types.ContentListUnion, types.ContentListUnionDict],
config: Optional[types.CreateCachedContentConfigOrDict] = None,
) -> types.CachedContent:
"""Creates cached content, this call will initialize the cached
Expand All @@ -1568,7 +1571,6 @@ async def create(

parameter_model = types._CreateCachedContentParameters(
model=model,
contents=contents,
config=config,
)

Expand Down
Loading

0 comments on commit 1d57271

Please sign in to comment.