Skip to content

Commit 453f825

Browse files
committed
feat: chat prompt sample example
1 parent c1b39d7 commit 453f825

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

llm_utils/chains.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .llm_factory import get_llm
55

66
from dotenv import load_dotenv
7+
import yaml
78

89
env_path = os.path.join(os.getcwd(), ".env")
910

@@ -74,6 +75,7 @@ def create_query_refiner_chain(llm):
7475

7576
# QueryMakerChain
7677
def create_query_maker_chain(llm):
78+
# SystemPrompt만 yaml 파일로 불러와서 사용
7779
prompt = load_prompt("../prompt/system_prompt.yaml", encoding="utf-8")
7880
query_maker_prompt = ChatPromptTemplate.from_messages(
7981
[
@@ -99,6 +101,21 @@ def create_query_maker_chain(llm):
99101
return query_maker_prompt | llm
100102

101103

104+
def create_query_maker_chain_from_chat_promt(llm):
105+
"""
106+
ChatPromptTemplate 형식으로 저장된 yaml 파일을 불러와서 사용 (코드가 간소화되지만, 별도의 후처리 작업이 필요)
107+
"""
108+
with open("../prompt/create_query_maker_chain.yaml", "r", encoding="utf-8") as f:
109+
chat_prompt_dict = yaml.safe_load(f)
110+
111+
messages = chat_prompt_dict['messages']
112+
template = messages[0]["prompt"].pop("template") if messages else None
113+
template = [tuple(item) for item in template]
114+
query_maker_prompt = ChatPromptTemplate.from_messages(template)
115+
116+
return query_maker_prompt | llm
117+
118+
102119
query_refiner_chain = create_query_refiner_chain(llm)
103120
query_maker_chain = create_query_maker_chain(llm)
104121

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
_type: chat
2+
messages:
3+
- prompt:
4+
template:
5+
- ["system", "
6+
당신은 데이터 분석 전문가(데이터 분석가 페르소나)입니다.
7+
사용자의 질문을 기반으로, 주어진 테이블과 컬럼 정보를 활용하여 적절한 SQL 쿼리를 생성하세요.
8+
9+
<<주의사항>>
10+
> 사용자의 질문이 다소 모호하더라도, 주어진 데이터를 참고하여 합리적인 가정을 통해 SQL 쿼리를 완성하세요.
11+
> 불필요한 재질문 없이, 가능한 가장 명확한 분석 쿼리를 만들어 주세요.
12+
> 최종 출력 형식은 반드시 아래와 같아야 합니다.
13+
14+
<<최종 형태 예시>>
15+
16+
<SQL>
17+
```sql
18+
SELECT COUNT(DISTINCT user_id)
19+
FROM stg_users
20+
```
21+
22+
<해석>
23+
```plaintext (max_length_per_line=100)
24+
이 쿼리는 stg_users 테이블에서 고유한 사용자의 수를 계산합니다.
25+
사용자는 유니크한 user_id를 가지고 있으며
26+
중복을 제거하기 위해 COUNT(DISTINCT user_id)를 사용했습니다.
27+
```
28+
"]
29+
- ["placeholder", "{user_input}" ]
30+
- ["placeholder", "{refined_input}" ]
31+
- ["system", "다음은 사용자의 db 환경정보와 사용 가능한 테이블 및 컬럼 정보입니다" ]
32+
- ["placeholder", "{user_database_env}" ]
33+
- ["placeholder", "{searched_tables}" ]
34+
- ["system", "위 정보를 바탕으로 사용자 질문에 대한 최적의 SQL 쿼리를 최종 형태 예시와 같은 형태로 생성하세요." ]
35+
36+
input_variables:
37+
- user_input
38+
- refined_input
39+
- user_database_env
40+
- searched_tables

0 commit comments

Comments
 (0)