-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (170 loc) · 5.34 KB
/
main.py
File metadata and controls
195 lines (170 loc) · 5.34 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
from pathlib import Path
from dotenv import load_dotenv
# Carica SEMPRE il .env del progetto e sovrascrive eventuali variabili già presenti
ENV_PATH = Path(__file__).resolve().parent / ".env"
load_dotenv(dotenv_path=ENV_PATH, override=True)
import argparse
import streamlit.web.cli as stcli
import sys
from scripts.evaluation.main import evaluation
from utils.config import Config
def parse_args() -> argparse.Namespace:
"""
Parse command-line arguments for the DQL application.
Provides options for configuring LLM providers, API credentials,
database connection strings, and processing behavior.
Returns:
argparse.Namespace: Object containing the parsed and validated arguments.
"""
parser = argparse.ArgumentParser(
description="DQL - Data Query Language",
formatter_class=argparse.RawTextHelpFormatter,
)
# Authentication argument for the AI model
parser.add_argument(
"-api",
action="store",
dest="api_key",
required=False,
help=(
"API key for the Gemini model.\n"
"If not specified, it will be read from '.env'."
),
)
# Persistence layer connection string
parser.add_argument(
"-uri_db",
action="store",
dest="uri_db",
required=False,
help=(
"MongoDB connection URI.\n"
"If not specified, it will be read from '.env'."
)
)
# Throttle control for API rate limits
parser.add_argument(
"-wait_seconds",
action="store",
dest="seconds",
type=int,
required=False,
default=0,
help="Number of seconds to wait after each LLM call (default: 0).",
)
# Optimization flag to reduce LLM overhead
parser.add_argument(
"-parsers",
action="store_true",
dest="parsers",
help=(
"Enable to avoid llm when possible"
),
)
# Specific model selection
parser.add_argument(
"-model_name",
action="store",
dest="model_name",
required=False,
default="gemini-2.5-flash",
help=(
"Specify the LLM model name.\n"
"Examples:\n"
" gemini-2.5-flash (default)\n"
" gpt-4o-mini\n"
" mistralai/Mistral-7B-Instruct-v0.2\n"
" claude-3-5-sonnet\n"
),
)
# AI engine provider selection
parser.add_argument(
"-provider",
action="store",
dest="provider",
required=False,
default="google_genai",
choices=["google_genai", "openai", "copilot", "huggingface"],
help=(
"Specify the LLM provider (default: google_genai).\n"
"Available options:\n"
" google_genai → Google Gemini\n"
" openai → OpenAI GPT models\n"
" copilot → GitHub Copilot API\n"
" huggingface → Hugging Face models\n"
),
)
# Evaluation Script
parser.add_argument(
"-evaluation_mode",
action="store_true",
dest="evaluation_mode",
help=(
"The streamlit application is not applied, but the 'evaluation' script."
),
)
# Optional future flags (commented for now)
# parser.add_argument(
# "-rag",
# action="store_true",
# dest="rag",
# help=(
# "Enable Retrieval-Augmented Generation (RAG) mode. "
# "Retrieves either entire documents or relevant chunks."
# ),
# )
#
# parser.add_argument(
# "-max_iterations",
# action="store",
# dest="max_iterations",
# type=int,
# required=False,
# help="Maximum number of query rewrite attempts.",
# )
#
# parser.add_argument(
# "-minimum_score",
# action="store",
# dest="minimum_score",
# type=float,
# required=False,
# help="Minimum rewrite score required to complete the process.",
# )
return parser.parse_args()
def _launch_streamlit() -> None:
"""
Launch the Streamlit user interface for DQL within the same process.
This helper overrides the system arguments to simulate a direct call
to the 'streamlit run' command pointing to the application's entry point.
"""
# Modify sys.argv to trigger Streamlit's CLI runner pointing to app.py
sys.argv = [
"streamlit",
"run",
"gui/app.py",
"--server.fileWatcherType=none", # Optimized performance by disabling file watcher
]
# Execute the Streamlit entry point and exit the parent process
sys.exit(stcli.main())
def main() -> None:
"""
Entry point for the DQL CLI application.
Workflow:
1. Capture runtime arguments from the shell.
2. Initialize the thread-safe Config Singleton with the parsed options.
3. Transition from the CLI environment to the Streamlit Web UI.
"""
# Parse CLI options
opts = parse_args()
# Bootstrap the configuration singleton before starting the UI
Config.get_instance(opts)
if opts:
if opts.evaluation_mode:
evaluation()
else:
# Hand off execution to the web interface
_launch_streamlit()
if __name__ == "__main__":
# Start the application lifecycle
main()