Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a187374

Browse files
Add additional functionalities with files SDK (google-gemini#260)
* Add additional functionalities with files SDK * Apply suggestions from code review * format * Increase default page_size --------- Co-authored-by: Mark Daoust <[email protected]>
1 parent ae8f7cc commit a187374

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

google/generativeai/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from google.generativeai.files import upload_file
5757
from google.generativeai.files import get_file
5858
from google.generativeai.files import list_files
59+
from google.generativeai.files import delete_file
5960

6061
from google.generativeai.generative_models import GenerativeModel
6162
from google.generativeai.generative_models import ChatSession

google/generativeai/client.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
__version__ = "0.0.0"
2828

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

3132

3233
class FileServiceClient(glm.FileServiceClient):
@@ -37,14 +38,12 @@ def __init__(self, *args, **kwargs):
3738
def _setup_discovery_api(self):
3839
api_key = self._client_options.api_key
3940
if api_key is None:
40-
raise ValueError("Uploading to the Files API requires an api key.")
41-
42-
end_point = self.api_endpoint
41+
raise ValueError("Uploading to the File API requires an API key.")
4342

4443
request = googleapiclient.http.HttpRequest(
4544
http=httplib2.Http(),
4645
postproc=lambda resp, content: (resp, content),
47-
uri=f"https://{end_point}/$discovery/rest?version=v1beta&key={api_key}",
46+
uri=f"{GENAI_API_DISCOVERY_URL}?version=v1beta&key={api_key}",
4847
)
4948
response, content = request.execute()
5049

@@ -84,7 +83,7 @@ def create_file(
8483

8584
class FileServiceAsyncClient(glm.FileServiceAsyncClient):
8685
async def create_file(self, *args, **kwargs):
87-
raise NotImplementedError("Create_file is not yet implemented for the async client.")
86+
raise NotImplementedError("`create_file` is not yet implemented for the async client.")
8887

8988

9089
@dataclasses.dataclass

google/generativeai/files.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
import pathlib
1919
import mimetypes
2020
from typing import Iterable
21+
import logging
22+
import google.ai.generativelanguage as glm
23+
from itertools import islice
2124

2225
from google.generativeai.types import file_types
2326

2427
from google.generativeai.client import get_default_file_client
2528

26-
__all__ = ["upload_file", "get_file", "list_files"]
29+
__all__ = ["upload_file", "get_file", "list_files", "delete_file"]
2730

2831

2932
def upload_file(
@@ -52,14 +55,22 @@ def upload_file(
5255
return file_types.File(response)
5356

5457

55-
def list_files(page_size=50) -> Iterable[file_types.File]:
58+
def list_files(page_size=100) -> Iterable[file_types.File]:
5659
client = get_default_file_client()
5760

58-
response = client.list_files(page_size=page_size)
61+
response = client.list_files(glm.ListFilesRequest(page_size=page_size))
5962
for proto in response:
6063
yield file_types.File(proto)
6164

6265

6366
def get_file(name) -> file_types.File:
6467
client = get_default_file_client()
6568
return file_types.File(client.get_file(name=name))
69+
70+
71+
def delete_file(name):
72+
if isinstance(name, (file_types.File, glm.File)):
73+
name = name.name
74+
request = glm.DeleteFileRequest(name=name)
75+
client = get_default_file_client()
76+
client.delete_file(request=request)

google/generativeai/generative_models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,8 @@ def generate_content(
239239
except google.api_core.exceptions.InvalidArgument as e:
240240
if e.message.startswith("Request payload size exceeds the limit:"):
241241
e.message += (
242-
" Please upload your files with the Files api instead."
243-
"`f = genai.create_file(path); m.generate_content(['tell me about this file:', f])`"
242+
" Please upload your files with the File API instead."
243+
"`f = genai.upload_file(path); m.generate_content(['tell me about this file:', f])`"
244244
)
245245
raise
246246

@@ -284,8 +284,8 @@ async def generate_content_async(
284284
except google.api_core.exceptions.InvalidArgument as e:
285285
if e.message.startswith("Request payload size exceeds the limit:"):
286286
e.message += (
287-
" Please upload your files with the Files api instead."
288-
"`f = genai.create_file(path); m.generate_content(['tell me about this file:', f])`"
287+
" Please upload your files with the File API instead."
288+
"`f = genai.upload_file(path); m.generate_content(['tell me about this file:', f])`"
289289
)
290290
raise
291291

google/generativeai/types/file_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ def update_time(self) -> datetime.datetime:
6767
def sha256_hash(self) -> bytes:
6868
return self._proto.sha256_hash
6969

70+
@property
71+
def uri(self) -> str:
72+
return self._proto.uri
73+
7074
def delete(self):
7175
client = get_default_file_client()
7276
client.delete_file(name=self.name)

0 commit comments

Comments
 (0)