File Module¶
erniebot_agent.file
¶
The file module provides a series of classes for managing files, which facilitate user interaction with the agent, including the File base class and its subclasses, FileManager, GlobalFileManagerHandler, and RemoteFileClient, which interacts with remote file servers.
It is recommended to use the GlobalFileManagerHandler to initialize the FileManager and obtain the global FileManager at the beginning of the event loop. Afterwards, you can simply use this global FileManager to perform operations such as adding, deleting, and querying files, as well as obtaining files generated by the agent.
A few notes about this submodule:
-
If you do not set environment variable
AISTUDIO_ACCESS_TOKEN, it will be under default setting. -
Method
GlobalFileManagerHandler().configure()can only be called once at the beginning. -
When you want to get a file manger, you can use method
GlobalFileManagerHandler().get(). -
The lifecycle of the
FileManagerclass is synchronized with the event loop. -
FileManagerclass is Noncopyable. -
If you want to get the content of
Fileobject, you can useread_contentsand usewrite_contents_tocreate the file to location you want. -
We do not recommend you to create
Fileobject yourself.
Examples:
>>> from erniebot_agent.file import GlobalFileManagerHandler
>>> async def demo_function():
>>> # need to use at the beginning of event loop
>>> await GlobalFileManagerHandler().configure(save_dir='your_path')
>>> file_manager = await GlobalFileManagerHandler().get()
>>> local_file = await file_manager.create_file_from_path(file_path='your_path', file_type='local')
>>> file = file_manager.look_up_file_by_id(file_id='your_file_id')
>>> file_content = await file.read_contents() # get file content(bytes)
>>> await local_file.write_contents_to('your_willing_path') # save to location you want
erniebot_agent.file.base ¶
File ¶
Abstract base class representing a generic file.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for the file. |
filename |
str
|
File name. |
byte_size |
int
|
Size of the file in bytes. |
created_at |
str
|
Timestamp indicating the file creation time. |
purpose |
str
|
Purpose or use case of the file. |
metadata |
Dict[str, Any]
|
Additional metadata associated with the file. |
Methods:
| Name | Description |
|---|---|
read_contents |
Abstract method to asynchronously read the file contents. |
write_contents_to |
Asynchronously write the file contents to a local path. |
get_file_repr |
Return a string representation for use in specific contexts. |
to_dict |
Convert the File object to a dictionary. |
Source code in erniebot-agent/src/erniebot_agent/file/base.py
__init__ ¶
__init__(*, id: str, filename: str, byte_size: int, created_at: str, purpose: str, metadata: Dict[str, Any]) -> None
Init method for the File class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id |
str
|
Unique identifier for the file. |
required |
filename |
str
|
File name. |
required |
byte_size |
int
|
Size of the file in bytes. |
required |
created_at |
str
|
Timestamp indicating the file creation time. |
required |
purpose |
str
|
Purpose or use case of the file. |
required |
metadata |
Dict[str, Any]
|
Additional metadata associated with the file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in erniebot-agent/src/erniebot_agent/file/base.py
write_contents_to
async
¶
Create a file to the location you want.
erniebot_agent.file.local_file ¶
LocalFile ¶
Represents a local file.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for the file. |
filename |
str
|
File name. |
byte_size |
int
|
Size of the file in bytes. |
created_at |
str
|
Timestamp indicating the file creation time. |
purpose |
str
|
Purpose or use case of the file, including "assistants" and "assistants_output". |
metadata |
Dict[str, Any]
|
Additional metadata associated with the file. |
path |
Path
|
The path to the local file. |
Methods:
| Name | Description |
|---|---|
read_contents |
Asynchronously read the contents of the local file. |
write_contents_to |
Asynchronously write the file contents to a local path. |
get_file_repr |
Return a string representation for use in specific contexts. |
Source code in erniebot-agent/src/erniebot_agent/file/local_file.py
__init__ ¶
__init__(*, id: str, filename: str, byte_size: int, created_at: str, purpose: protocol.FilePurpose, metadata: Dict[str, Any], path: pathlib.Path, validate_file_id: bool = True) -> None
Initialize a LocalFile object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id |
str
|
The unique identifier for the file. |
required |
filename |
str
|
The name of the file. |
required |
byte_size |
int
|
The size of the file in bytes. |
required |
created_at |
str
|
The timestamp indicating the file creation time. |
required |
purpose |
FilePurpose
|
The purpose or use case of the file. |
required |
metadata |
Dict[str, Any]
|
Additional metadata associated with the file. |
required |
path |
Path
|
The path to the local file. |
required |
validate_file_id |
bool
|
Flag to validate the file ID. Default is True. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file ID is invalid. |
Source code in erniebot-agent/src/erniebot_agent/file/local_file.py
read_contents
async
¶
erniebot_agent.file.remote_file ¶
RemoteFile ¶
Represents a remote file.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for the file. |
filename |
str
|
File name. |
byte_size |
int
|
Size of the file in bytes. |
created_at |
str
|
Timestamp indicating the file creation time. |
purpose |
str
|
Purpose or use case of the file, including "assistants" and "assistants_output". |
metadata |
Dict[str, Any]
|
Additional metadata associated with the file. |
client |
RemoteFileClient
|
The client of remote file. |
Methods:
| Name | Description |
|---|---|
read_contents |
Asynchronously read the contents of the local file. |
write_contents_to |
Asynchronously write the file contents to a local path. |
get_file_repr |
Return a string representation for use in specific contexts. |
delete |
Asynchronously delete the file from client. |
create_temporary_url |
Asynchronously create a temporary URL for the file. |
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
create_temporary_url
async
¶
To create a temporary valid URL for the file.
erniebot_agent.file.file_manager ¶
FileManager ¶
Manages files, providing methods for creating, retrieving, and listing files.
Attributes:
| Name | Type | Description |
|---|---|---|
remote_file_client(RemoteFileClient) |
The remote file client. |
|
save_dir |
Optional[FilePath]
|
Directory for saving local files. |
closed |
Whether the file manager is closed. |
Methods:
| Name | Description |
|---|---|
create_file_from_path |
Create a file from a specified file path. |
create_local_file_from_path |
Create a local file from a file path. |
create_remote_file_from_path |
Create a remote file from a file path. |
create_file_from_bytes |
Create a file from bytes. |
retrieve_remote_file_by_id |
Retrieve a remote file by its ID. |
look_up_file_by_id |
Look up a file by its ID. |
list_remote_files |
List remote files. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
__init__ ¶
__init__(remote_file_client: Optional[RemoteFileClient] = None, save_dir: Optional[FilePath] = None, *, prune_on_close: bool = True) -> None
Initialize the FileManager object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
remote_file_client |
Optional[RemoteFileClient]
|
The remote file client. |
None
|
prune_on_close |
bool
|
Control whether to automatically clean up files that can be safely deleted when the object is closed. |
True
|
save_dir |
Optional[FilePath]
|
Directory for saving local files. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
close
async
¶
Delete the file manager and clean up its cache
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
create_file_from_bytes
async
¶
create_file_from_bytes(file_contents: bytes, filename: str, *, file_purpose: protocol.FilePurpose = 'assistants', file_metadata: Optional[Dict[str, Any]] = None, file_type: Optional[Literal['local', 'remote']] = None) -> _File
Create a file from bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_contents |
bytes
|
The contents of the file. |
required |
filename |
str
|
The name of the file. |
required |
file_purpose |
FilePurpose
|
The purpose or use case of the file. |
'assistants'
|
file_metadata |
Optional[Dict[str, Any]]
|
Additional metadata associated with the file. |
None
|
file_type |
Optional[Literal['local', 'remote']]
|
The type of file ("local" or "remote"). |
None
|
Returns:
| Type | Description |
|---|---|
_File
|
Union[LocalFile, RemoteFile]: The created file. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
create_file_from_path
async
¶
create_file_from_path(file_path: FilePath, *, file_purpose: protocol.FilePurpose = 'assistants', file_metadata: Optional[Dict[str, Any]] = None, file_type: Optional[Literal['local', 'remote']] = None) -> _File
Create a file from a specified file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
FilePath
|
The path to the file. |
required |
file_purpose |
FilePurpose
|
The purpose or use case of the file,
including |
'assistants'
|
file_metadata |
Optional[Dict[str, Any]]
|
Additional metadata associated with the file. |
None
|
file_type |
Optional[Literal['local', 'remote']]
|
The type of file ("local" or "remote"). |
None
|
Returns:
| Type | Description |
|---|---|
_File
|
Union[LocalFile, RemoteFile]: The created file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unsupported file type is provided. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
create_local_file_from_path
async
¶
create_local_file_from_path(file_path: FilePath, file_purpose: protocol.FilePurpose, file_metadata: Optional[Dict[str, Any]]) -> LocalFile
Create a local file from a local file path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
FilePath
|
The path to the file. |
required |
file_purpose |
FilePurpose
|
The purpose or use case of the file,
including |
required |
file_metadata |
Optional[Dict[str, Any]]
|
Additional metadata associated with the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
LocalFile |
LocalFile
|
The created local file. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
create_remote_file_from_path
async
¶
create_remote_file_from_path(file_path: FilePath, file_purpose: protocol.FilePurpose, file_metadata: Optional[Dict[str, Any]]) -> RemoteFile
Create a remote file from a file path and upload it to the client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path |
FilePath
|
The path to the file. |
required |
file_purpose |
FilePurpose
|
The purpose or use case of the file,
including |
required |
file_metadata |
Optional[Dict[str, Any]]
|
Additional metadata associated with the file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RemoteFile |
RemoteFile
|
The created remote file. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
list_registered_files ¶
List remote files.
Returns:
| Type | Description |
|---|---|
List[_File]
|
List[RemoteFile]: The list of remote files. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
look_up_file_by_id ¶
Look up a file by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_id |
str
|
The ID of the file. |
required |
Returns:
| Type | Description |
|---|---|
_File
|
file[File]: The looked-up file. |
Raises:
| Type | Description |
|---|---|
FileError
|
If the file with the specified ID is not found. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
prune
async
¶
Clean local cache of file manager.
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
retrieve_remote_file_by_id
async
¶
Retrieve a remote file by its ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_id |
str
|
The ID of the remote file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RemoteFile |
RemoteFile
|
The retrieved remote file. |
Source code in erniebot-agent/src/erniebot_agent/file/file_manager.py
erniebot_agent.file.global_file_manager_handler ¶
GlobalFileManagerHandler ¶
Singleton handler for managing the global FileManager instance.
This class provides a singleton instance for managing the global FileManager and allows for its configuration and retrieval.
Methods:
| Name | Description |
|---|---|
get |
Asynchronously retrieves the global FileManager instance. |
configure |
Asynchronously configures the global FileManager at the beginning of event loop. |
set |
Asynchronously sets the global FileManager explicitly. |
Source code in erniebot-agent/src/erniebot_agent/file/global_file_manager_handler.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
configure
async
¶
configure(*, access_token: Optional[str] = None, save_dir: Optional[str] = None, enable_remote_file: bool = False, **opts: Any) -> None
Configure the global FileManager.
This method configures the global FileManager with the provided parameters at the beginning of event loop. If the global FileManager is already set, it raises an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
Optional[str]
|
The access token for remote file client. |
None
|
save_dir |
Optional[str]
|
The directory for saving local files. |
None
|
enable_remote_file |
bool
|
Whether to enable remote file. |
False
|
**opts |
Any
|
Additional options for FileManager. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the global FileManager is already set. |
Source code in erniebot-agent/src/erniebot_agent/file/global_file_manager_handler.py
get
async
¶
Retrieve the global FileManager instance.
This method returns the existing global FileManager instance, creating one if it doesn't exist.
Returns:
| Name | Type | Description |
|---|---|---|
FileManager |
FileManager
|
The global FileManager instance. |
Source code in erniebot-agent/src/erniebot_agent/file/global_file_manager_handler.py
set
async
¶
Set the global FileManager explicitly.
This method sets the global FileManager instance explicitly. If the global FileManager is already set, it raises an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_manager |
FileManager
|
The FileManager instance to set as global. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the global FileManager is already set. |
Source code in erniebot-agent/src/erniebot_agent/file/global_file_manager_handler.py
erniebot_agent.file.remote_file ¶
AIStudioFileClient ¶
Recommended remote file client: AI Studio.
Methods:
| Name | Description |
|---|---|
upload_file |
Upload a file to AI Studio client. |
retrieve_file |
Retrieve information about a file from AI Studio. |
retrieve_file_contents |
Retrieve the contents of a file from AI Studio. |
list_files |
List files available in AI Studio. |
delete_file |
Delete a file in AI Studio client(#TODO: not supported now). |
create_temporary_url |
Create a temporary URL for accessing a file in AI Studio. |
close |
Close the AIStudioFileClient. |
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
__init__ ¶
Initialize the AIStudioFileClient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str
|
The access token for AI Studio. |
required |
aiohttp_session |
Optional[ClientSession]
|
A custom aiohttp session (default is None). |
None
|
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
list_files
async
¶
List files in AI Studio client.
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
retrieve_file
async
¶
Retrieve a file in AI Studio client by id.
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
retrieve_file_contents
async
¶
Retrieve file content in AI Studio client by id.
Source code in erniebot-agent/src/erniebot_agent/file/remote_file.py
upload_file
async
¶
upload_file(file_path: pathlib.Path, file_purpose: protocol.FilePurpose, file_metadata: Dict[str, Any]) -> RemoteFile
Upload a file to AI Studio client.