Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
418 lines (355 loc) · 12.9 KB
/
Copy pathmain.py
File metadata and controls
418 lines (355 loc) · 12.9 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""CLI entrypoint for the autonomous data engineering pipeline."""
from __future__ import annotations
import argparse
import logging
import os
from pathlib import Path
import re
import sys
from env_utils import (
API_KEY_ENV_VARS,
ENV_FILE_PATH,
configured_api_key_present,
env_value_is_usable,
load_env_into_process,
read_env_file,
)
from llm import LLMError, LLMGateway
from pipeline_service import run_pipeline
from settings import get_settings
from tracing import configure_llm_tracing
ESCAPE_SEQUENCE_RE = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
def configure_logging(*, level: str = "INFO") -> None:
"""Configure logging for pipeline progress."""
logging.basicConfig(
level=getattr(logging, level.upper(), logging.INFO),
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
)
def parse_args(argv: list[str]) -> argparse.Namespace:
"""Parse CLI arguments."""
parser = argparse.ArgumentParser(
description="Run the Autonomous Data Engineer pipeline."
)
parser.add_argument(
"--goal",
help="High-level machine learning data goal.",
)
parser.add_argument(
"--max-agents",
type=int,
default=2,
help="Maximum number of parallel research agents to run.",
)
parser.add_argument(
"--set-api-key",
action="store_true",
help="Prompt for an API key, save it to .env.local, and exit.",
)
parser.add_argument(
"--clear-api-key",
action="store_true",
help="Remove stored API keys from .env.local and exit.",
)
parser.add_argument(
"--api-key-status",
action="store_true",
help="Print the currently configured API key source and exit.",
)
parser.add_argument(
"--set-model",
action="store_true",
help="Prompt for a preferred model, save it to .env.local, and exit.",
)
parser.add_argument(
"--clear-model",
action="store_true",
help="Remove OPENAI_MODEL from .env.local and exit.",
)
parser.add_argument(
"--model-status",
action="store_true",
help="Print the currently configured model and exit.",
)
parser.add_argument(
"--setup",
action="store_true",
help="Interactive setup for API key and preferred model, then exit.",
)
parser.add_argument(
"--doctor",
action="store_true",
help="Run local deployment and routing checks, then exit.",
)
parser.add_argument(
"--log-level",
default="INFO",
choices=("DEBUG", "INFO", "WARNING", "ERROR"),
help="Logging verbosity for the current run.",
)
args = parser.parse_args(argv)
if not (
args.set_api_key
or args.clear_api_key
or args.api_key_status
or args.set_model
or args.clear_model
or args.model_status
or args.setup
or args.doctor
) and not args.goal:
parser.error("the following arguments are required: --goal")
return args
def main(argv: list[str] | None = None) -> int:
"""Run the four-stage autonomous data engineering pipeline."""
args = parse_args(argv or sys.argv[1:])
load_local_env()
if args.set_api_key:
return prompt_and_store_api_key()
if args.setup:
return interactive_setup()
if args.clear_api_key:
return clear_stored_api_keys()
if args.api_key_status:
return print_api_key_status()
if args.set_model:
return prompt_and_store_model()
if args.clear_model:
return clear_stored_model()
if args.model_status:
return print_model_status()
if args.doctor:
from deployment_checks import format_results, run_deployment_checks
load_local_env()
results, failures = run_deployment_checks()
print(format_results(results))
return 0 if failures == 0 else 1
maybe_prompt_for_api_key()
configure_logging(level=args.log_level)
logger = logging.getLogger(__name__)
configure_llm_tracing(get_settings())
llm_gateway = _optional_llm_gateway()
try:
artifacts = run_pipeline(
goal=args.goal,
max_agents=max(1, args.max_agents),
llm_gateway=llm_gateway,
)
print(f"Dataset name: {artifacts.dataset_name}")
print(f"CSV: {artifacts.csv_path}")
print(f"Parquet: {artifacts.parquet_path}")
print(f"Profile: {artifacts.profile_path}")
print(f"Validation: {artifacts.validation_path}")
print(f"Rows: {artifacts.rows}")
print(f"Columns: {artifacts.columns}")
return 0
except LLMError as exc:
logger.error("%s", exc)
return 2
except KeyboardInterrupt:
logger.error("Interrupted by user")
return 130
except Exception as exc:
logger.exception("Unhandled pipeline error: %s", exc)
return 3
def _records_look_like_ui_chrome(records: list[dict[str, object]]) -> bool:
blocked_phrases = (
"jump to content",
"main menu",
"search wikipedia",
"create account",
"log in",
"donate",
"help",
"navigation",
"button",
"searchbox",
"radio",
"appearance",
"page tools",
"personal tools",
"views",
"site",
"tools",
"thumbnail for",
)
suspicious = 0
for record in records[:20]:
for value in record.values():
if isinstance(value, str) and any(phrase in value.lower() for phrase in blocked_phrases):
suspicious += 1
break
return suspicious >= max(1, min(len(records), 5) // 2)
def _predictive_result_matches_goal(dataframe, *, goal: str, target_field: str | None) -> bool:
columns = {str(column).lower() for column in dataframe.columns}
markers = set()
if target_field:
markers.update(token for token in re.findall(r"[a-z0-9]+", target_field.lower()) if len(token) >= 3)
markers.update(
token
for token in re.findall(r"[a-z0-9]+", goal.lower())
if token in {"salary", "valuation", "revenue", "income", "profit", "growth", "spend", "transfer", "playoff", "population", "gdp"}
)
if not markers:
return True
if any(marker in column for marker in markers for column in columns):
return True
if "growth" in markers and any(token in column for token in {"change", "growth"} for column in columns):
return True
return False
def load_local_env(path: Path = ENV_FILE_PATH) -> None:
"""Load locally stored environment variables without requiring python-dotenv."""
load_env_into_process(path)
def maybe_prompt_for_api_key(path: Path = ENV_FILE_PATH) -> None:
"""On first interactive boot, offer to save an API key for future runs."""
if _configured_api_key_present():
return
if not sys.stdin.isatty():
return
print(
"No API key found for LLM features.\n"
"Enter an API key to store in .env.local for future runs,\n"
"or press Enter to continue without one."
)
prompt_and_store_api_key(path)
def interactive_setup(path: Path = ENV_FILE_PATH) -> int:
"""Guide the user through storing an API key and optional model."""
print("Interactive setup")
return prompt_and_store_api_key(path)
def prompt_and_store_api_key(path: Path = ENV_FILE_PATH) -> int:
"""Prompt for an API key, persist it locally, and load it into the process."""
if not sys.stdin.isatty():
print("Cannot prompt for an API key without an interactive terminal.")
return 1
api_key = input("API key (input is visible): ").strip()
if not api_key:
print("No API key entered.")
return 1
env_var = _infer_api_env_var(api_key)
_store_api_key(path, env_var, api_key)
os.environ[env_var] = api_key
print(f"Stored {env_var} in {path}.")
suggested_model = _default_model_for_env_var(env_var)
model = _clean_prompt_input(input(
f"Preferred model [{suggested_model}] (press Enter to keep default): "
))
if model:
_store_env_value(path, "OPENAI_MODEL", model)
os.environ["OPENAI_MODEL"] = model
print(f"Stored OPENAI_MODEL in {path}.")
return 0
def clear_stored_api_keys(path: Path = ENV_FILE_PATH) -> int:
"""Remove stored API keys from the local env file."""
if not path.exists():
print(f"No {path} file found.")
return 0
existing = read_env_file(path)
for env_var in API_KEY_ENV_VARS:
existing.pop(env_var, None)
if existing:
serialized = "\n".join(f"{key}='{value}'" for key, value in sorted(existing.items()))
path.write_text(f"{serialized}\n", encoding="utf-8")
else:
path.unlink()
for env_var in API_KEY_ENV_VARS:
os.environ.pop(env_var, None)
print("Cleared stored API keys.")
return 0
def prompt_and_store_model(path: Path = ENV_FILE_PATH) -> int:
"""Prompt for a preferred model and persist it locally."""
if not sys.stdin.isatty():
print("Cannot prompt for a model without an interactive terminal.")
return 1
suggested = _suggested_model_from_environment()
model = _clean_prompt_input(input(f"Model [{suggested}]: ")) or suggested
_store_env_value(path, "OPENAI_MODEL", model)
os.environ["OPENAI_MODEL"] = model
print(f"Stored OPENAI_MODEL='{model}' in {path}.")
return 0
def clear_stored_model(path: Path = ENV_FILE_PATH) -> int:
"""Remove the locally stored preferred model."""
if not path.exists():
print(f"No {path} file found.")
return 0
existing = read_env_file(path)
if "OPENAI_MODEL" not in existing:
print("No stored model found.")
return 0
existing.pop("OPENAI_MODEL", None)
if existing:
serialized = "\n".join(f"{key}='{value}'" for key, value in sorted(existing.items()))
path.write_text(f"{serialized}\n", encoding="utf-8")
else:
path.unlink()
os.environ.pop("OPENAI_MODEL", None)
print("Cleared stored model.")
return 0
def print_api_key_status(path: Path = ENV_FILE_PATH) -> int:
"""Print which provider key is currently configured."""
for env_var in ("OPENAI_API_KEY", "GEMINI_API_KEY", "GROQ_API_KEY"):
value = os.getenv(env_var, "").strip()
if env_value_is_usable(value, key=env_var):
source = "environment"
if path.exists() and read_env_file(path).get(env_var) == value:
source = str(path)
print(f"{env_var} configured via {source}.")
return 0
print("No API key configured.")
return 0
def print_model_status(path: Path = ENV_FILE_PATH) -> int:
"""Print the currently configured model source."""
model = os.getenv("OPENAI_MODEL", "").strip()
if not model:
print("No model configured. Provider default will be used.")
return 0
source = "environment"
if path.exists() and read_env_file(path).get("OPENAI_MODEL") == model:
source = str(path)
print(f"OPENAI_MODEL='{model}' configured via {source}.")
return 0
def _configured_api_key_present() -> bool:
return configured_api_key_present()
def _infer_api_env_var(api_key: str) -> str:
if api_key.startswith("gsk_"):
return "GROQ_API_KEY"
if api_key.startswith("AI"):
return "GEMINI_API_KEY"
return "OPENAI_API_KEY"
def _store_api_key(path: Path, env_var: str, api_key: str) -> None:
_store_env_value(path, env_var, api_key)
def _store_env_value(path: Path, key: str, value: str) -> None:
existing = read_env_file(path)
existing[key] = value
serialized = "\n".join(f"{item_key}='{item_value}'" for item_key, item_value in sorted(existing.items()))
path.write_text(f"{serialized}\n", encoding="utf-8")
def _default_model_for_env_var(env_var: str) -> str:
if env_var == "GROQ_API_KEY":
return "openai/gpt-oss-20b"
if env_var == "GEMINI_API_KEY":
return "gemini-2.0-flash-lite"
return "gpt-4.1-mini"
def _suggested_model_from_environment() -> str:
for env_var in ("GROQ_API_KEY", "GEMINI_API_KEY", "OPENAI_API_KEY"):
value = os.getenv(env_var, "").strip()
if env_value_is_usable(value, key=env_var):
return _default_model_for_env_var(env_var)
return "gpt-4.1-mini"
def _clean_prompt_input(value: str) -> str:
cleaned = ESCAPE_SEQUENCE_RE.sub("", value).strip()
if cleaned in {"^[[A", "^[[B", "^[[C", "^[[D"}:
return ""
return cleaned
def _optional_llm_gateway() -> LLMGateway | None:
try:
return LLMGateway()
except LLMError:
return None
def _fallback_provenance_map(records: list[dict[str, object]]) -> dict[str, str]:
if not records:
return {}
source_ref = "synthesized"
first = records[0]
if isinstance(first.get("source_url"), str) and first["source_url"]:
source_ref = str(first["source_url"])
return {column: source_ref for column in first.keys()}
if __name__ == "__main__":
raise SystemExit(main())