Spaces:
Running
Running
zach
commited on
Commit
·
d4052d1
1
Parent(s):
58de40c
Add utilities file with some helper functions
Browse files
utils.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
utils.py
|
3 |
+
|
4 |
+
This file contains utility functions that are shared across the project.
|
5 |
+
These functions provide reusable logic to simplify code in other modules.
|
6 |
+
|
7 |
+
Key Features:
|
8 |
+
- Validates that required environment variables are set, raising meaningful errors otherwise.
|
9 |
+
- Provides helper functions for text validation and truncation.
|
10 |
+
|
11 |
+
Functions:
|
12 |
+
- validate_env_var: Ensures the presence of a specific environment variable and retrieves its value.
|
13 |
+
- truncate_text: Truncates a string to a specified length with ellipses.
|
14 |
+
- validate_prompt_length: Ensures that a prompt does not exceed the specified maximum length.
|
15 |
+
"""
|
16 |
+
|
17 |
+
# Standard Library Imports
|
18 |
+
import os
|
19 |
+
# Local Application Imports
|
20 |
+
from config import logger
|
21 |
+
|
22 |
+
|
23 |
+
def truncate_text(text: str, max_length: int = 50) -> str:
|
24 |
+
"""
|
25 |
+
Truncate a string to the specified length, appending ellipses if necessary.
|
26 |
+
|
27 |
+
Args:
|
28 |
+
text (str): The text to truncate.
|
29 |
+
max_length (int): The maximum length of the truncated string.
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
str: The truncated text.
|
33 |
+
|
34 |
+
Examples:
|
35 |
+
>>> truncate_text("Hello, World!", 5)
|
36 |
+
'Hello...'
|
37 |
+
>>> truncate_text("Short string", 20)
|
38 |
+
'Short string'
|
39 |
+
>>> truncate_text("Edge case with zero length", 0)
|
40 |
+
''
|
41 |
+
"""
|
42 |
+
if max_length <= 0:
|
43 |
+
logger.warning(f"Invalid max_length: {max_length}. Returning empty string.")
|
44 |
+
return ""
|
45 |
+
|
46 |
+
is_truncated = len(text) > max_length
|
47 |
+
truncated_text = text[:max_length]
|
48 |
+
if is_truncated:
|
49 |
+
truncated_text += "..."
|
50 |
+
|
51 |
+
logger.debug(f"Truncating text to max_length={max_length}. Result: {truncated_text}")
|
52 |
+
return truncated_text
|
53 |
+
|
54 |
+
|
55 |
+
def validate_env_var(var_name: str) -> str:
|
56 |
+
"""
|
57 |
+
Validates that an environment variable is set and returns its value.
|
58 |
+
|
59 |
+
Args:
|
60 |
+
var_name (str): The name of the environment variable to validate.
|
61 |
+
|
62 |
+
Returns:
|
63 |
+
str: The value of the environment variable.
|
64 |
+
|
65 |
+
Raises:
|
66 |
+
ValueError: If the environment variable is not set.
|
67 |
+
|
68 |
+
Examples:
|
69 |
+
>>> import os
|
70 |
+
>>> os.environ["EXAMPLE_VAR"] = "example_value"
|
71 |
+
>>> validate_env_var("EXAMPLE_VAR")
|
72 |
+
'example_value'
|
73 |
+
|
74 |
+
>>> validate_env_var("MISSING_VAR")
|
75 |
+
Traceback (most recent call last):
|
76 |
+
...
|
77 |
+
ValueError: MISSING_VAR is not set. Please ensure it is defined in your environment variables.
|
78 |
+
"""
|
79 |
+
logger.debug(f"Validating environment variable: {var_name}")
|
80 |
+
value = os.environ.get(var_name, "")
|
81 |
+
if not value:
|
82 |
+
raise ValueError(f"{var_name} is not set. Please ensure it is defined in your environment variables.")
|
83 |
+
return value
|
84 |
+
|
85 |
+
|
86 |
+
def validate_prompt_length(prompt: str, max_length: int, min_length: int) -> None:
|
87 |
+
"""
|
88 |
+
Validates that a prompt is within specified minimum and maximum length limits.
|
89 |
+
|
90 |
+
Args:
|
91 |
+
prompt (str): The input prompt to validate.
|
92 |
+
max_length (int): The maximum allowed length for the prompt.
|
93 |
+
min_length (int): The minimum required length for the prompt.
|
94 |
+
|
95 |
+
Raises:
|
96 |
+
ValueError: If the prompt is empty, too short, or exceeds max_length.
|
97 |
+
|
98 |
+
Example:
|
99 |
+
>>> validate_prompt_length("Hello world", max_length=500, min_length=5)
|
100 |
+
# Passes validation
|
101 |
+
|
102 |
+
>>> validate_prompt_length("", max_length=500, min_length=1)
|
103 |
+
# Raises ValueError: "Prompt must be at least 1 character(s) long."
|
104 |
+
"""
|
105 |
+
logger.debug(f"Prompt length being validated: {len(prompt)} characters")
|
106 |
+
|
107 |
+
# Check if prompt is empty or too short
|
108 |
+
stripped_prompt = prompt.strip()
|
109 |
+
if len(stripped_prompt) < min_length:
|
110 |
+
raise ValueError(
|
111 |
+
f"Prompt must be at least {min_length} character(s) long. "
|
112 |
+
f"Received only {len(stripped_prompt)}."
|
113 |
+
)
|
114 |
+
|
115 |
+
# Check if prompt is too long
|
116 |
+
if len(stripped_prompt) > max_length:
|
117 |
+
raise ValueError(
|
118 |
+
f"The prompt exceeds the maximum allowed length of {max_length} characters. "
|
119 |
+
f"Your prompt contains {len(stripped_prompt)} characters."
|
120 |
+
)
|
121 |
+
|
122 |
+
logger.debug(f"Prompt length validation passed for prompt: {truncate_text(stripped_prompt)}")
|