-
Notifications
You must be signed in to change notification settings - Fork 67
File API functions #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
File API functions #160
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
659b4e9
File API functions
squeakymouse 6167411
Merge branch 'main' into katiewu/file-api-functions
squeakymouse e4a62e5
upload file, not path
squeakymouse 31000ca
address comments
squeakymouse 69c02e6
Merge branch 'main' into katiewu/file-api-functions
squeakymouse c951a1b
rename to DownloadFileResponse
squeakymouse e6a531b
bump version to 0.0.0.b8
squeakymouse 3bb0f39
is this the right place to add CNAME
squeakymouse c100be7
change name back
squeakymouse 5fb6768
Merge branch 'main' into katiewu/file-api-functions
squeakymouse a2d9dd6
typecheck
squeakymouse 69c0c4c
black reformat
squeakymouse b0c2761
isort
squeakymouse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| from io import BufferedReader | ||
|
|
||
| from llmengine.api_engine import DEFAULT_TIMEOUT, APIEngine | ||
| from llmengine.data_types import ( | ||
| DeleteFileResponse, | ||
| GetFileContentResponse, | ||
| GetFileResponse, | ||
| ListFilesResponse, | ||
| UploadFileResponse, | ||
| ) | ||
|
|
||
|
|
||
| class File(APIEngine): | ||
| """ | ||
| File API. This API is used to upload private files to LLM engine so that fine-tunes can access them for training and validation data. | ||
|
|
||
| Functions are provided to upload, get, list, and delete files, as well as to get the contents of a file. | ||
| """ | ||
|
|
||
| @classmethod | ||
| def upload(cls, file: BufferedReader) -> UploadFileResponse: | ||
| """ | ||
| Uploads a file to LLM engine. | ||
|
|
||
| Args: | ||
| file (`BufferedReader`): | ||
| A file opened with open(file_path, "r") | ||
|
|
||
| Returns: | ||
| UploadFileResponse: an object that contains the ID of the uploaded file | ||
|
|
||
| === "Uploading file in Python" | ||
| ```python | ||
| from llmengine import File | ||
|
|
||
| response = File.upload(open("training_dataset.csv", "r")) | ||
|
|
||
| print(response.json()) | ||
| ``` | ||
|
|
||
| === "Response in JSON" | ||
| ```json | ||
| { | ||
| "id": "file-abc123" | ||
| } | ||
| ``` | ||
| """ | ||
| files = {"file": file} | ||
| response = cls.post_file( | ||
| resource_name="v1/files", | ||
| files=files, | ||
| timeout=DEFAULT_TIMEOUT, | ||
| ) | ||
| return UploadFileResponse.parse_obj(response) | ||
|
|
||
| @classmethod | ||
| def get(cls, file_id: str) -> GetFileResponse: | ||
| """ | ||
| Get file metadata, including filename and size. | ||
|
|
||
| Args: | ||
| file_id (`str`): | ||
| ID of the file | ||
|
|
||
| Returns: | ||
| GetFileResponse: an object that contains the ID, filename, and size of the requested file | ||
|
|
||
| === "Getting metadata about file in Python" | ||
| ```python | ||
| from llmengine import File | ||
|
|
||
| response = File.get( | ||
| file_id="file-abc123", | ||
| ) | ||
|
|
||
| print(response.json()) | ||
| ``` | ||
|
|
||
| === "Response in JSON" | ||
| ```json | ||
| { | ||
| "id": "file-abc123", | ||
| "filename": "training_dataset.csv", | ||
| "size": 100 | ||
| } | ||
| ``` | ||
| """ | ||
| response = cls._get(f"v1/files/{file_id}", timeout=DEFAULT_TIMEOUT) | ||
| return GetFileResponse.parse_obj(response) | ||
|
|
||
| @classmethod | ||
| def list(cls) -> ListFilesResponse: | ||
| """ | ||
| List metadata about all files, e.g. their filenames and sizes. | ||
|
|
||
| Returns: | ||
| ListFilesResponse: an object that contains a list of all files and their filenames and sizes | ||
|
|
||
| === "Listing files in Python" | ||
| ```python | ||
| from llmengine import File | ||
|
|
||
| response = File.list() | ||
| print(response.json()) | ||
| ``` | ||
|
|
||
| === "Response in JSON" | ||
| ```json | ||
| { | ||
| "files": [ | ||
| { | ||
| "id": "file-abc123", | ||
| "filename": "training_dataset.csv", | ||
| "size": 100 | ||
| }, | ||
| { | ||
| "id": "file-def456", | ||
| "filename": "validation_dataset.csv", | ||
| "size": 50 | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
| """ | ||
| response = cls._get("v1/files", timeout=30) | ||
| return ListFilesResponse.parse_obj(response) | ||
|
|
||
| @classmethod | ||
| def delete(cls, file_id: str) -> DeleteFileResponse: | ||
| """ | ||
| Deletes a file. | ||
|
|
||
| Args: | ||
| file_id (`str`): | ||
| ID of the file | ||
|
|
||
| Returns: | ||
| DeleteFileResponse: an object that contains whether the deletion was successful | ||
|
|
||
| === "Deleting file in Python" | ||
| ```python | ||
| from llmengine import File | ||
|
|
||
| response = File.delete(file_id="file-abc123") | ||
| print(response.json()) | ||
| ``` | ||
|
|
||
| === "Response in JSON" | ||
| ```json | ||
| { | ||
| "deleted": true | ||
| } | ||
| ``` | ||
| """ | ||
| response = cls._delete( | ||
| f"v1/files/{file_id}", | ||
| timeout=DEFAULT_TIMEOUT, | ||
| ) | ||
| return DeleteFileResponse.parse_obj(response) | ||
|
|
||
| @classmethod | ||
| def download(cls, file_id: str) -> GetFileContentResponse: | ||
| """ | ||
| Get contents of a file, as a string. (If the uploaded file is in binary, a string encoding will be returned.) | ||
|
|
||
| Args: | ||
| file_id (`str`): | ||
| ID of the file | ||
|
|
||
| Returns: | ||
| GetFileContentResponse: an object that contains the ID and content of the file | ||
|
|
||
| === "Getting file content in Python" | ||
| ```python | ||
| from llmengine import File | ||
|
|
||
| response = File.get_content(file_id="file-abc123") | ||
| print(response.json()) | ||
| ``` | ||
|
|
||
| === "Response in JSON" | ||
| ```json | ||
| { | ||
| "id": "file-abc123", | ||
| "content": "Hello world!" | ||
| } | ||
| ``` | ||
| """ | ||
| response = cls._get( | ||
| f"v1/files/{file_id}/content", | ||
| timeout=DEFAULT_TIMEOUT, | ||
| ) | ||
| return GetFileContentResponse.parse_obj(response) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually maybe it is fine to have this always be a string - we just need to document the expectations, e.g. if you uploaded text, then it'll ofc be that text; else if it's binary, we'll return it as a string subject to some encoding. |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,12 @@ | |
| - get | ||
| - list | ||
| - delete | ||
|
|
||
| ::: llmengine.File | ||
| selection: | ||
| members: | ||
| - upload | ||
| - get | ||
| - get_content | ||
| - list | ||
| - delete | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: newline spacing to let things breathe a bit?