|
| 1 | +# CLI 모듈 |
| 2 | + |
| 3 | +Lang2SQL 프로젝트의 CLI(Command Line Interface) 모듈입니다. 자연어 질문을 SQL 쿼리로 변환하고, Streamlit 웹 애플리케이션을 실행하는 기능을 제공합니다. |
| 4 | + |
| 5 | +## 디렉토리 구조 |
| 6 | + |
| 7 | +``` |
| 8 | +cli/ |
| 9 | +├── __init__.py # CLI 진입점 및 메인 그룹 정의 |
| 10 | +├── __pycache__/ # Python 캐시 디렉토리 |
| 11 | +├── commands/ # CLI 명령어 정의 모듈 |
| 12 | +│ ├── __pycache__/ |
| 13 | +│ ├── quary.py # 자연어 질문을 SQL로 변환하는 명령어 |
| 14 | +│ ├── run_streamlit.py # Streamlit 실행 명령어 |
| 15 | +│ └── README.md # Commands 모듈 문서 |
| 16 | +├── core/ # CLI 핵심 기능 모듈 |
| 17 | +│ ├── __pycache__/ |
| 18 | +│ ├── environment.py # 환경 변수 초기화 모듈 |
| 19 | +│ ├── streamlit_runner.py # Streamlit 실행 유틸리티 |
| 20 | +│ └── README.md # Core 모듈 문서 |
| 21 | +├── utils/ # CLI 유틸리티 모듈 |
| 22 | +│ ├── __pycache__/ |
| 23 | +│ ├── env_loader.py # 환경 변수 로드 유틸리티 |
| 24 | +│ ├── logger.py # 로깅 설정 유틸리티 |
| 25 | +│ └── README.md # Utils 모듈 문서 |
| 26 | +└── README.md # 이 파일 |
| 27 | +``` |
| 28 | + |
| 29 | +## 모듈 개요 |
| 30 | + |
| 31 | +### `__init__.py` |
| 32 | + |
| 33 | +CLI 프로그램의 진입점입니다. Click 프레임워크를 사용하여 명령어 그룹과 옵션을 정의합니다. |
| 34 | + |
| 35 | +**주요 구성 요소:** |
| 36 | + |
| 37 | +#### CLI 그룹 정의 |
| 38 | +- **함수**: `cli(ctx, datahub_server, run_streamlit, port, env_file_path, prompt_dir_path, vectordb_type, vectordb_location)` |
| 39 | +- **데코레이터**: `@click.group()` |
| 40 | +- **역할**: Lang2SQL CLI의 최상위 명령어 그룹 |
| 41 | + |
| 42 | +#### 주요 옵션 |
| 43 | + |
| 44 | +1. **`--version` / `-v`**: 버전 정보 출력 |
| 45 | +2. **`--datahub_server`** (Deprecated): DataHub GMS URL 설정 - 더 이상 사용되지 않음 |
| 46 | +3. **`--run-streamlit`**: CLI 실행 시 Streamlit 애플리케이션을 바로 실행하는 플래그 |
| 47 | +4. **`-p, --port`**: Streamlit 서버 포트 번호 (기본값: 8501) |
| 48 | +5. **`--env-file-path`**: 환경 변수를 로드할 .env 파일 경로 |
| 49 | +6. **`--prompt-dir-path`**: 프롬프트 템플릿 디렉토리 경로 |
| 50 | +7. **`--vectordb-type`** (Deprecated): VectorDB 타입 - 더 이상 사용되지 않음 |
| 51 | +8. **`--vectordb-location`** (Deprecated): VectorDB 위치 - 더 이상 사용되지 않음 |
| 52 | + |
| 53 | +#### 주요 동작 |
| 54 | + |
| 55 | +1. **환경 변수 초기화**: `initialize_environment()` 호출로 환경 변수 설정 |
| 56 | +2. **Deprecated 옵션 경고**: 사용되지 않는 옵션 사용 시 경고 메시지 출력 |
| 57 | +3. **Streamlit 자동 실행**: `--run-streamlit` 플래그가 설정된 경우 Streamlit 실행 |
| 58 | + |
| 59 | +#### 등록된 명령어 |
| 60 | + |
| 61 | +- `run-streamlit`: Streamlit 애플리케이션 실행 (`cli/commands/run_streamlit.py`) |
| 62 | +- `query`: 자연어 질문을 SQL 쿼리로 변환 (`cli/commands/quary.py`) |
| 63 | + |
| 64 | +#### Import 관계 |
| 65 | + |
| 66 | +**Import하는 모듈:** |
| 67 | +- `cli.commands.quary.query_command`: query 명령어 |
| 68 | +- `cli.commands.run_streamlit.run_streamlit_cli_command`: run-streamlit 명령어 |
| 69 | +- `cli.core.environment.initialize_environment`: 환경 변수 초기화 |
| 70 | +- `cli.core.streamlit_runner.run_streamlit_command`: Streamlit 실행 함수 |
| 71 | +- `cli.utils.logger.configure_logging`: 로깅 설정 |
| 72 | +- `version.__version__`: 버전 정보 |
| 73 | + |
| 74 | +**사용 위치:** |
| 75 | +- CLI 진입점: `pyproject.toml`의 `[project.scripts]` 섹션에 정의됨 |
| 76 | + ```toml |
| 77 | + [project.scripts] |
| 78 | + lang2sql = "cli.__init__:cli" |
| 79 | + ``` |
| 80 | +- 사용 방법: 패키지 설치 후 `lang2sql` 명령어로 실행 |
| 81 | + ```bash |
| 82 | + lang2sql --help |
| 83 | + lang2sql --version |
| 84 | + lang2sql --run-streamlit |
| 85 | + lang2sql query "질문" |
| 86 | + lang2sql run-streamlit |
| 87 | + ``` |
| 88 | + |
| 89 | +#### 코드 예시 |
| 90 | + |
| 91 | +```python |
| 92 | +from cli import cli |
| 93 | + |
| 94 | +# CLI 그룹 자동 등록 |
| 95 | +# lang2sql 명령어로 실행 가능 |
| 96 | +``` |
| 97 | + |
| 98 | +### 하위 모듈 |
| 99 | + |
| 100 | +#### `commands/` 모듈 |
| 101 | + |
| 102 | +CLI 명령어를 정의하는 모듈입니다. 자연어 질문을 SQL로 변환하는 `query` 명령어와 Streamlit을 실행하는 `run-streamlit` 명령어를 제공합니다. |
| 103 | + |
| 104 | +자세한 내용은 [`commands/README.md`](commands/README.md)를 참고하세요. |
| 105 | + |
| 106 | +**주요 파일:** |
| 107 | +- `quary.py`: `query_command` - 자연어를 SQL로 변환하는 명령어 |
| 108 | +- `run_streamlit.py`: `run_streamlit_cli_command` - Streamlit 실행 명령어 |
| 109 | + |
| 110 | +**사용 위치:** |
| 111 | +- `cli/__init__.py` (10-11, 116-117번 라인): CLI 그룹에 명령어 등록 |
| 112 | + ```python |
| 113 | + from cli.commands.quary import query_command |
| 114 | + from cli.commands.run_streamlit import run_streamlit_cli_command |
| 115 | + |
| 116 | + cli.add_command(run_streamlit_cli_command) |
| 117 | + cli.add_command(query_command) |
| 118 | + ``` |
| 119 | + |
| 120 | +#### `core/` 모듈 |
| 121 | + |
| 122 | +CLI의 핵심 기능을 제공하는 모듈입니다. 환경 변수 초기화와 Streamlit 실행 기능을 담당합니다. |
| 123 | + |
| 124 | +자세한 내용은 [`core/README.md`](core/README.md)를 참고하세요. |
| 125 | + |
| 126 | +**주요 파일:** |
| 127 | +- `environment.py`: `initialize_environment` - 환경 변수 초기화 함수 |
| 128 | +- `streamlit_runner.py`: `run_streamlit_command` - Streamlit 실행 함수 |
| 129 | + |
| 130 | +**사용 위치:** |
| 131 | +- `cli/__init__.py` (12-13, 85, 113번 라인): |
| 132 | + ```python |
| 133 | + from cli.core.environment import initialize_environment |
| 134 | + from cli.core.streamlit_runner import run_streamlit_command |
| 135 | + |
| 136 | + # 환경 변수 초기화 |
| 137 | + initialize_environment( |
| 138 | + env_file_path=env_file_path, |
| 139 | + prompt_dir_path=prompt_dir_path |
| 140 | + ) |
| 141 | + |
| 142 | + # Streamlit 실행 |
| 143 | + if run_streamlit: |
| 144 | + run_streamlit_command(port) |
| 145 | + ``` |
| 146 | + |
| 147 | +#### `utils/` 모듈 |
| 148 | + |
| 149 | +CLI 애플리케이션에서 사용되는 유틸리티 함수들을 제공하는 모듈입니다. |
| 150 | + |
| 151 | +자세한 내용은 [`utils/README.md`](utils/README.md)를 참고하세요. |
| 152 | + |
| 153 | +**주요 파일:** |
| 154 | +- `env_loader.py`: 환경 변수 로드 및 설정 함수들 |
| 155 | + - `load_env`: .env 파일 로드 |
| 156 | + - `set_prompt_dir`: 프롬프트 디렉토리 설정 |
| 157 | + - `set_vectordb`: VectorDB 설정 |
| 158 | +- `logger.py`: `configure_logging` - CLI 전용 로깅 설정 |
| 159 | + |
| 160 | +**사용 위치:** |
| 161 | +- `cli/__init__.py` (14, 18번 라인): 로깅 설정 |
| 162 | + ```python |
| 163 | + from cli.utils.logger import configure_logging |
| 164 | + |
| 165 | + logger = configure_logging() |
| 166 | + ``` |
| 167 | +- `cli/core/environment.py`: 환경 변수 로드 |
| 168 | +- `cli/core/streamlit_runner.py`: 로깅 설정 |
| 169 | +- `cli/commands/quary.py`: 로깅 설정 |
| 170 | +- `cli/commands/run_streamlit.py`: 로깅 설정 |
| 171 | + |
| 172 | +## CLI 사용 방법 |
| 173 | + |
| 174 | +### 기본 사용법 |
| 175 | + |
| 176 | +```bash |
| 177 | +# 도움말 보기 |
| 178 | +lang2sql --help |
| 179 | + |
| 180 | +# 버전 확인 |
| 181 | +lang2sql --version |
| 182 | + |
| 183 | +# Streamlit 바로 실행 |
| 184 | +lang2sql --run-streamlit |
| 185 | + |
| 186 | +# 환경 변수 파일 지정하여 실행 |
| 187 | +lang2sql --env-file-path /path/to/.env --run-streamlit |
| 188 | +``` |
| 189 | + |
| 190 | +### 명령어 |
| 191 | + |
| 192 | +#### `query` 명령어 |
| 193 | + |
| 194 | +자연어 질문을 SQL 쿼리로 변환합니다. |
| 195 | + |
| 196 | +```bash |
| 197 | +# 기본 사용 |
| 198 | +lang2sql query "고객 데이터를 기반으로 유니크한 유저 수를 카운트하는 쿼리" |
| 199 | + |
| 200 | +# 옵션과 함께 사용 |
| 201 | +lang2sql query "질문" \ |
| 202 | + --database-env clickhouse \ |
| 203 | + --retriever-name 기본 \ |
| 204 | + --top-n 5 \ |
| 205 | + --device cpu \ |
| 206 | + --use-enriched-graph \ |
| 207 | + --vectordb-type faiss \ |
| 208 | + --vectordb-location ./dev/table_info_db |
| 209 | +``` |
| 210 | + |
| 211 | +#### `run-streamlit` 명령어 |
| 212 | + |
| 213 | +Streamlit 웹 애플리케이션을 실행합니다. |
| 214 | + |
| 215 | +```bash |
| 216 | +# 기본 포트(8501)로 실행 |
| 217 | +lang2sql run-streamlit |
| 218 | + |
| 219 | +# 특정 포트로 실행 |
| 220 | +lang2sql run-streamlit -p 9000 |
| 221 | +``` |
| 222 | + |
| 223 | +자세한 사용법은 [`commands/README.md`](commands/README.md)를 참고하세요. |
| 224 | + |
| 225 | +## 의존성 |
| 226 | + |
| 227 | +### 내부 의존성 |
| 228 | + |
| 229 | +- `cli.commands.*`: CLI 명령어 정의 |
| 230 | +- `cli.core.*`: 핵심 기능 모듈 |
| 231 | +- `cli.utils.*`: 유틸리티 함수 |
| 232 | +- `version`: 버전 정보 |
| 233 | + |
| 234 | +### 외부 의존성 |
| 235 | + |
| 236 | +- `click`: CLI 프레임워크 |
| 237 | +- `subprocess`: Streamlit 프로세스 실행 (core 모듈) |
| 238 | +- `python-dotenv`: 환경 변수 파일 로드 (utils 모듈) |
| 239 | +- `streamlit`: Streamlit 웹 애플리케이션 프레임워크 |
| 240 | + |
| 241 | +## 모듈 간 관계도 |
| 242 | + |
| 243 | +``` |
| 244 | +cli/__init__.py (진입점) |
| 245 | +├── commands/ |
| 246 | +│ ├── quary.py → engine.query_executor |
| 247 | +│ └── run_streamlit.py → core/streamlit_runner.py |
| 248 | +├── core/ |
| 249 | +│ ├── environment.py → utils/env_loader.py |
| 250 | +│ └── streamlit_runner.py → utils/logger.py |
| 251 | +└── utils/ |
| 252 | + ├── env_loader.py (독립적) |
| 253 | + └── logger.py (독립적) |
| 254 | +``` |
| 255 | + |
| 256 | +## 주요 특징 |
| 257 | + |
| 258 | +1. **모듈화된 구조**: 명령어, 핵심 기능, 유틸리티가 명확히 분리됨 |
| 259 | +2. **확장 가능성**: 새로운 명령어를 쉽게 추가할 수 있는 구조 |
| 260 | +3. **환경 관리**: 일관된 환경 변수 초기화 보장 |
| 261 | +4. **UI 중심 설계**: VectorDB 및 DataHub 설정은 UI에서 관리 |
| 262 | +5. **로깅 지원**: 모든 모듈에서 일관된 로깅 사용 |
| 263 | +6. **Deprecated 옵션 처리**: 사용되지 않는 옵션에 대한 명확한 경고 |
| 264 | + |
| 265 | +## 참고 문서 |
| 266 | + |
| 267 | +- [`commands/README.md`](commands/README.md): 명령어 모듈 상세 문서 |
| 268 | +- [`core/README.md`](core/README.md): 핵심 기능 모듈 상세 문서 |
| 269 | +- [`utils/README.md`](utils/README.md): 유틸리티 모듈 상세 문서 |
| 270 | + |
0 commit comments