44from .llm_factory import get_llm
55
66from dotenv import load_dotenv
7+ from prompt .template_loader import get_prompt_template
78import yaml
89
910env_path = os .path .join (os .getcwd (), ".env" )
1314else :
1415 print (f"⚠️ 환경변수 파일(.env)이 { os .getcwd ()} 에 없습니다!" )
1516
17+
1618llm = get_llm (
1719 model_type = "openai" ,
1820 model_name = "gpt-4o-mini" ,
2123
2224
2325def create_query_refiner_chain (llm ):
26+ prompt = get_prompt_template ('query_refiner_prompt' )
2427 tool_choice_prompt = ChatPromptTemplate .from_messages (
2528 [
26- (
27- "system" ,
28- """
29- 당신은 데이터 분석 전문가(데이터 분석가 페르소나)입니다.
30- 현재 subscription_activities, contract_activities, marketing_activities,
31- sales_activities, success_activities, support_activities, trial_activities 데이터를
32- 보유하고 있으며, 사용자의 질문이 모호할 경우에도 우리가 가진 데이터를 기반으로
33- 충분히 답변 가능한 형태로 질문을 구체화해 주세요.
34-
35- 주의:
36- - 사용자에게 추가 정보를 요구하는 ‘재질문(추가 질문)’을 하지 마세요.
37- - 질문에 포함해야 할 요소(예: 특정 기간, 대상 유저 그룹, 분석 대상 로그 종류 등)가
38- 불충분하더라도, 합리적으로 추론해 가정한 뒤
39- 답변에 충분한 질문 형태로 완성해 주세요.
40-
41- 예시:
42- 사용자가 "유저 이탈 원인이 궁금해요"라고 했다면,
43- 재질문 형식이 아니라
44- "최근 1개월 간의 접속·결제 로그를 기준으로,
45- 주로 어떤 사용자가 어떤 과정을 거쳐 이탈하는지를 분석해야 한다"처럼
46- 분석 방향이 명확해진 질문 한 문장(또는 한 문단)으로 정리해 주세요.
47-
48- 최종 출력 형식 예시:
49- ------------------------------
50- 구체화된 질문:
51- "최근 1개월 동안 고액 결제 경험이 있는 유저가
52- 행동 로그에서 이탈 전 어떤 패턴을 보였는지 분석"
53-
54- 가정한 조건:
55- - 최근 1개월치 행동 로그와 결제 로그 중심
56- - 고액 결제자(월 결제액 10만 원 이상) 그룹 대상으로 한정
57- ------------------------------
58- """ ,
59- ),
29+ SystemMessagePromptTemplate .from_template (prompt ),
6030 MessagesPlaceholder (variable_name = "user_input" ),
61- (
62- "system" ,
31+ SystemMessagePromptTemplate .from_template (
6332 """
6433 위 사용자의 입력을 바탕으로
6534 분석 관점에서 **충분히 답변 가능한 형태**로
@@ -76,10 +45,10 @@ def create_query_refiner_chain(llm):
7645# QueryMakerChain
7746def create_query_maker_chain (llm ):
7847 # SystemPrompt만 yaml 파일로 불러와서 사용
79- prompt = load_prompt ( "../prompt/system_prompt.yaml" , encoding = "utf-8" )
48+ prompt = get_prompt_template ( 'query_maker_prompt' )
8049 query_maker_prompt = ChatPromptTemplate .from_messages (
8150 [
82- SystemMessagePromptTemplate .from_template (prompt . template ),
51+ SystemMessagePromptTemplate .from_template (prompt ),
8352 (
8453 "system" ,
8554 "아래는 사용자의 질문 및 구체화된 질문입니다:" ,
@@ -101,21 +70,6 @@ def create_query_maker_chain(llm):
10170 return query_maker_prompt | llm
10271
10372
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-
11973query_refiner_chain = create_query_refiner_chain (llm )
12074query_maker_chain = create_query_maker_chain (llm )
12175
0 commit comments