Skip to content

Commit 3d2fb4a

Browse files
committed
feat: Add common utility modules for file operations
- Add constants.py for shared constants (KILOBYTE, MEGABYTE) - Add file_utils.py for reusable file-to-base64 conversion - Extract common file validation and encoding logic
1 parent 1f27a18 commit 3d2fb4a

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

libs/upstage/langchain_upstage/utils/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import base64
2+
import os
3+
import re
4+
from typing import List
5+
6+
KILOBYTE = 1024
7+
MEGABYTE = 1024 * KILOBYTE
8+
9+
10+
def _process_input(
11+
input_url: str, supported_extensions: List[str], max_file_size: int
12+
) -> str:
13+
if re.match(r"^https?://", input_url):
14+
return input_url
15+
16+
if os.path.exists(input_url):
17+
if os.path.getsize(input_url) > max_file_size:
18+
raise ValueError(f"File too large: max {max_file_size / MEGABYTE}MB")
19+
else:
20+
raise FileNotFoundError(f"File not found: {input_url}")
21+
22+
validate_extension(input_url, supported_extensions)
23+
24+
try:
25+
with open(input_url, "rb") as img_file:
26+
img_bytes = img_file.read()
27+
base64_data = base64.b64encode(img_bytes).decode("utf-8")
28+
29+
return f"data:application/octet-stream;base64,{base64_data}"
30+
except Exception as e:
31+
raise ValueError(f"Error occurred while processing the file: {e}")
32+
33+
34+
def validate_extension(input_url: str, supported_extensions: List[str]) -> None:
35+
file_ext = input_url.lower().split(".")[-1]
36+
if file_ext not in supported_extensions:
37+
supported = ", ".join([f".{ext}" for ext in supported_extensions])
38+
raise ValueError(f"Unsupported image extension. supported: {supported}")
39+
40+
41+
def create_message(
42+
input_url: str, supported_extensions: List[str], max_file_size: int
43+
) -> dict:
44+
url = _process_input(input_url, supported_extensions, max_file_size)
45+
46+
return {"type": "image_url", "image_url": {"url": url}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
from typing import Optional
3+
4+
5+
def get_from_param_or_env(
6+
key: str,
7+
param: Optional[str] = None,
8+
env_key: Optional[str] = None,
9+
default: Optional[str] = None,
10+
) -> str:
11+
"""Get a value from a param or an environment variable."""
12+
if param is not None:
13+
return param
14+
elif env_key and env_key in os.environ and os.environ[env_key]:
15+
return os.environ[env_key]
16+
elif default is not None:
17+
return default
18+
else:
19+
raise ValueError(
20+
f"Did not find {key}, please add an environment variable"
21+
f" `{env_key}` which contains it, or pass"
22+
f" `{key}` as a named parameter."
23+
)

0 commit comments

Comments
 (0)