-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
49 lines (41 loc) · 1.74 KB
/
utils.py
File metadata and controls
49 lines (41 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from openai import OpenAI
import os
import json
def call_llm(prompt, max_retries=3):
"""
Calls GPT-4o-Mini with retry logic and better error handling.
Uses the new OpenAI client syntax.
"""
api_key = os.getenv("AIPROXY_TOKEN")
if not api_key:
raise ValueError("AIPROXY_TOKEN environment variable is required")
client = OpenAI(api_key=api_key)
for attempt in range(max_retries):
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant focused on data processing tasks."},
{"role": "user", "content": prompt}
],
temperature=0.3
)
return response.choices[0].message.content
except Exception as e:
if attempt == max_retries - 1:
raise RuntimeError(f"Failed to get LLM response after {max_retries} attempts: {str(e)}")
continue
def validate_file_path(path, allow_write=False):
"""
Validates that the file path is within /data/ and prevents unauthorized access.
`allow_write` controls whether the path can be written to.
"""
base_dir = "/data/"
# Ensure the path is within /data/
normalized_path = os.path.normpath(path)
if not normalized_path.startswith(base_dir):
raise ValueError(f"Access denied: {path} is outside the allowed directory")
# Prevent deletions by ensuring no file is being removed
if not allow_write and not os.path.exists(normalized_path):
raise ValueError(f"Access denied: {path} does not exist")
return normalized_path