main
1import sys
2from pathlib import Path
3
4import rich
5
6from openai import OpenAI
7
8# generate this file using `./generate_file.sh`
9file = Path("/tmp/big_test_file.txt")
10
11client = OpenAI()
12
13
14def from_disk() -> None:
15 print("uploading file from disk")
16
17 upload = client.uploads.upload_file_chunked(
18 file=file,
19 mime_type="txt",
20 purpose="batch",
21 )
22 rich.print(upload)
23
24
25def from_in_memory() -> None:
26 print("uploading file from memory")
27
28 # read the data into memory ourselves to simulate
29 # it coming from somewhere else
30 data = file.read_bytes()
31 filename = "my_file.txt"
32
33 upload = client.uploads.upload_file_chunked(
34 file=data,
35 filename=filename,
36 bytes=len(data),
37 mime_type="txt",
38 purpose="batch",
39 )
40 rich.print(upload)
41
42
43if "memory" in sys.argv:
44 from_in_memory()
45else:
46 from_disk()