Skip to content

Commit

Permalink
Merge branch 'google-gemini:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Daehyun-Bigbread authored May 16, 2024
2 parents e9a8ba8 + efead6b commit 9ed919c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 6 additions & 2 deletions google/generativeai/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
__version__ = "0.0.0"

USER_AGENT = "genai-py"
GENAI_API_DISCOVERY_URL = "https://generativelanguage.googleapis.com/$discovery/rest"


class FileServiceClient(glm.FileServiceClient):
Expand All @@ -42,7 +43,7 @@ def _setup_discovery_api(self):
request = googleapiclient.http.HttpRequest(
http=httplib2.Http(),
postproc=lambda resp, content: (resp, content),
uri=f"https://{self.api_endpoint}/$discovery/rest?version=v1beta&key={api_key}",
uri=f"{GENAI_API_DISCOVERY_URL}?version=v1beta&key={api_key}",
)
response, content = request.execute()

Expand All @@ -58,6 +59,7 @@ def create_file(
mime_type: str | None = None,
name: str | None = None,
display_name: str | None = None,
resumable: bool = True,
) -> glm.File:
if self._discovery_api is None:
self._setup_discovery_api()
Expand All @@ -68,7 +70,9 @@ def create_file(
if display_name is not None:
file["displayName"] = display_name

media = googleapiclient.http.MediaFileUpload(filename=path, mimetype=mime_type)
media = googleapiclient.http.MediaFileUpload(
filename=path, mimetype=mime_type, resumable=resumable
)
request = self._discovery_api.media().upload(body={"file": file}, media_body=media)
result = request.execute()

Expand Down
19 changes: 18 additions & 1 deletion google/generativeai/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,24 @@ def upload_file(
mime_type: str | None = None,
name: str | None = None,
display_name: str | None = None,
resumable: bool = True,
) -> file_types.File:
"""Uploads a file using a supported file service.
Args:
path: The path to the file to be uploaded.
mime_type: The MIME type of the file. If not provided, it will be
inferred from the file extension.
name: The name of the file in the destination (e.g., 'files/sample-image').
If not provided, a system generated ID will be created.
display_name: Optional display name of the file.
resumable: Whether to use the resumable upload protocol. By default, this is enabled.
See details at
https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.http.MediaFileUpload-class.html#resumable
Returns:
file_types.File: The response of the uploaded file.
"""
client = get_default_file_client()

path = pathlib.Path(os.fspath(path))
Expand All @@ -50,7 +67,7 @@ def upload_file(
display_name = path.name

response = client.create_file(
path=path, mime_type=mime_type, name=name, display_name=display_name
path=path, mime_type=mime_type, name=name, display_name=display_name, resumable=resumable
)
return file_types.File(response)

Expand Down
2 changes: 1 addition & 1 deletion google/generativeai/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
# limitations under the License.
from __future__ import annotations

__version__ = "0.5.2"
__version__ = "0.5.3"

0 comments on commit 9ed919c

Please sign in to comment.