feat: improve file handling and streamline provider implementations

- Added file upload usage example with bucket_id in docs/file.md
- Fixed ARTA provider by refactoring error handling with a new raise_error function
- Simplified aspect_ratio handling in ARTA with proper default
- Improved AllenAI provider by cleaning up image handling logic
- Fixed Blackbox provider's media handling to properly process images
- Updated file tools to handle URL downloads correctly
- Fixed bucket_id pattern matching in ToolHandler.process_bucket_tool
- Cleaned up imports in typing.py by removing unnecessary sys import
- Fixed inconsistent function parameters in g4f/tools/files.py
- Fixed return value of upload_and_process function to return bucket_id
This commit is contained in:
hlohaus 2025-04-08 19:00:44 +02:00
parent fa36dccf16
commit c083f85206
7 changed files with 82 additions and 104 deletions

View file

@ -75,16 +75,45 @@ def upload_and_process(files_or_urls, bucket_id=None):
else:
print(f"Unhandled SSE event: {line}")
response.close()
return bucket_id5
# Example with URLs
urls = [{"url": "https://github.com/xtekky/gpt4free/issues"}]
bucket_id = upload_and_process(urls)
#Example with files
files = {'files': open('document.pdf', 'rb'), 'files': open('data.json', 'rb')}
files = {'files': ('document.pdf', open('document.pdf', 'rb'))}
bucket_id = upload_and_process(files)
```
**Usage of Uploaded Files:**
```python
from g4f.client import Client
# Enable debug mode
import g4f.debug
g4f.debug.logging = True
client = Client()
# Upload example file
files = {'files': ('demo.docx', open('demo.docx', 'rb'))}
bucket_id = upload_and_process(files)
# Send request with file:
response = client.chat.completions.create(
[{"role": "user", "content": [
{"type": "text", "text": "Discribe this file."},
{"bucket_id": bucket_id}
]}],
)
print(response.choices[0].message.content)
```
**Example Output:**
```
This document is a demonstration of the DOCX Input plugin capabilities in the software ...
```
**Example Usage (JavaScript):**