Skip to content

DBMS별 SQL 차이를 반영한 Few-Shot Prompt 최적화 제안 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
8 tasks
seyoung4503 opened this issue Mar 8, 2025 · 2 comments · Fixed by #14
Closed
8 tasks

DBMS별 SQL 차이를 반영한 Few-Shot Prompt 최적화 제안 #6

seyoung4503 opened this issue Mar 8, 2025 · 2 comments · Fixed by #14
Assignees
Labels
enhancement New feature or request prompt The input provided by the user in natural language that serves as the basis for SQL generation.

Comments

@seyoung4503
Copy link
Contributor

seyoung4503 commented Mar 8, 2025

Why ❓

현재 상황:

  • 현재 llm_utils/chains.py 파일의 create_query_maker_chain 함수에서 Few-Shot SQL예제가 모든 DBMS에 동일하게 적용되고 있습니다.

최종 형태 예시:
<SQL>
```sql
SELECT COUNT(DISTINCT user_id)
FROM stg_users
```
<해석>
```plaintext (max_length_per_line=100)
이 쿼리는 stg_users 테이블에서 고유한 사용자의 수를 계산합니다.
사용자는 유니크한 user_id를 가지고 있으며
중복을 제거하기 위해 COUNT(DISTINCT user_id)를 사용했습니다.
```

문제점 👿:

  • DBMS마다 SQL 문법이 다르므로, 특정 DBMS에서는 올바르지 않은 SQL이 생성될 가능성이 있습니다.

How 🤔

  • 해결 방안 ✅:
    • DBMS에 따른 SQL-FewShot을 위 프롬프트에 추가하여 데이터베이스 환경에 맞는 올바른 쿼리를 생성하게 합니다.
    • DBMS에 따른 Syntax 용어 사전을 생성해 SQL 문법 구조를 반영하여 쿼리를 생성할 수 있습니다.

What 🛠️

  • DBMS 종류 선정 논의
    • Oracle, MySQL, PostgreSQL, DuckDB 등 종류 선정
  • DBMS별 SQL 예제 맞춤화
    • DBMS별 문법 차이 분석 (키워드 등)
    • DBMS별 예제 코드 정의 및 추가 - best_practice_query에 따른 로깅이나 최적의 케이스 수동 추가
  • DBMS Syntax 용어 사전 구현
    • SQL 용어 사전 정의 - 함수 및 키워드에 따른 설명 작성
    • create_query_maker_chain과 용어 사전 결합을 통해 올바른 쿼리 생성
@YuujinHwang
Copy link

YuujinHwang commented Mar 10, 2025

DBMS 별로 SQL 쿼리 생성을 위한 Node를 분기하여 적용하는 방식을 적용해 볼 수도 있을 것 같습니다 :)
DB별로 제공하고 있는 내장함수들이 다르며, 오라클 같은 경우 LLM 생성 쿼리 유효성 검증 시 거의 항상 사용되는 LIMIT clause 등을 상이하게 사용하고 있어, Langchain에서 제공하는 기본 SQL 유틸에서는 별도의 프롬프트로 분기처리 하고 있는게 확인됩니다.

https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/chains/sql_database/prompt.py

-> 링크수정완료

@ehddnr301
Copy link
Collaborator

비개발 직군이 데이터 조회하기 위한 용도라서 langchain에서 제공하는 유틸을 사용하면 어떤 디비에 연결을 했는지에 따라 프롬프트가 공개되어있음

@ehddnr301 ehddnr301 added enhancement New feature or request prompt The input provided by the user in natural language that serves as the basis for SQL generation. labels Mar 11, 2025
@DShomin DShomin self-assigned this Mar 13, 2025
@ehddnr301 ehddnr301 added this to the Mission: March Complete ✅ milestone Mar 13, 2025
ehddnr301 added a commit that referenced this issue Mar 25, 2025
#6 이슈 해결
#11 이슈에서 `쿼리 설명이 영어로 나오는` 부분 해결 기대
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request prompt The input provided by the user in natural language that serves as the basis for SQL generation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants