File Utilities¶
llm_client.utils.file_utils
¶
File utilities for handling uploads to LLM providers.
Functions¶
detect_file_type(file_path)
¶
Detect the type of a file based on its extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
FileType
|
File type category. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If file type cannot be determined or is unsupported. |
Examples:
Source code in llm_client/utils/file_utils.py
encode_file_base64(file_path)
¶
Encode a file to base64 string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Base64 encoded string. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist. |
Examples:
Source code in llm_client/utils/file_utils.py
get_mime_type(file_path)
¶
Get the MIME type of a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
str
|
MIME type string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If MIME type cannot be determined. |
Examples:
Source code in llm_client/utils/file_utils.py
prepare_file_for_gemini(file_path)
¶
Prepare a file for Gemini API format (via OpenAI compatibility).
Gemini nutzt die OpenAI-Kompatibilitätsschicht, unterstützt aber PDFs nur im image_url Format, nicht im file Format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with file data in Gemini format. |
Examples:
Source code in llm_client/utils/file_utils.py
prepare_file_for_openai(file_path)
¶
Prepare a file for OpenAI API format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with file data in OpenAI format. |
Examples:
>>> file_data = prepare_file_for_openai("image.jpg")
>>> "type" in file_data and "image_url" in file_data
True
Source code in llm_client/utils/file_utils.py
prepare_files_for_provider(file_paths, provider)
¶
Prepare multiple files for a specific provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_paths
|
list[str | Path]
|
List of file paths. |
required |
provider
|
str
|
Name of the provider. |
required |
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of file data dictionaries. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any file is not supported by the provider. |
FileNotFoundError
|
If any file doesn't exist. |
Examples:
Source code in llm_client/utils/file_utils.py
validate_file_for_provider(file_path, provider)
¶
Validate if a file is supported by a provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the file. |
required |
provider
|
str
|
Name of the provider (openai, gemini, groq, ollama). |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (is_valid, error_message). |
Examples:
>>> is_valid, error = validate_file_for_provider("image.jpg", "openai")
>>> is_valid
True
>>> is_valid, error = validate_file_for_provider("video.mp4", "groq")
>>> is_valid
False