-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add uploads.upload_file helper
- Loading branch information
1 parent
b143c16
commit 3bbb271
Showing
3 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,10 @@ | ||
# generate a text file with random data for testing file uploads | ||
wanted_size=$((1024*2048*512)) | ||
file_size=$(( ((wanted_size/12)+1)*12 )) | ||
read_size=$((file_size*3/4)) | ||
|
||
echo "wanted=$wanted_size file=$file_size read=$read_size" | ||
|
||
dd if=/dev/urandom bs=$read_size count=1 | base64 > /tmp/small_test_file.txt | ||
|
||
truncate -s "$wanted_size" /tmp/big_test_file.txt |
This file contains 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,46 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import rich | ||
|
||
from openai import OpenAI | ||
|
||
# generate this file using `./generate_file.sh` | ||
file = Path("/tmp/big_test_file.txt") | ||
|
||
client = OpenAI() | ||
|
||
|
||
def from_disk() -> None: | ||
print("uploading file from disk") | ||
|
||
upload = client.uploads.upload_file_chunked( | ||
file=file, | ||
mime_type="txt", | ||
purpose="batch", | ||
) | ||
rich.print(upload) | ||
|
||
|
||
def from_in_memory() -> None: | ||
print("uploading file from memory") | ||
|
||
# read the data into memory ourselves to simulate | ||
# it coming from somewhere else | ||
data = file.read_bytes() | ||
filename = "my_file.txt" | ||
|
||
upload = client.uploads.upload_file_chunked( | ||
file=data, | ||
filename=filename, | ||
bytes=len(data), | ||
mime_type="txt", | ||
purpose="batch", | ||
) | ||
rich.print(upload) | ||
|
||
|
||
if "memory" in sys.argv: | ||
from_in_memory() | ||
else: | ||
from_disk() |
This file contains 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