1+ """
2+ LLM 체인 생성 모듈.
3+
4+ 이 모듈은 Lang2SQL에서 사용하는 다양한 LangChain 기반 체인을 정의합니다.
5+ - Query Maker
6+ - Query Enrichment
7+ - Profile Extraction
8+ - Question Gate (SQL 적합성 분류)
9+ """
110import os
211from langchain_core .prompts import (
312 ChatPromptTemplate ,
4- MessagesPlaceholder ,
513 SystemMessagePromptTemplate ,
614)
715from pydantic import BaseModel , Field
16+ from llm_utils .output_parser .question_suitability import QuestionSuitability
817
918from llm_utils .llm import get_llm
1019
1524
1625
1726class QuestionProfile (BaseModel ):
27+ """
28+ 자연어 질문의 특징을 구조화해 표현하는 프로파일 모델.
29+
30+ 이 프로파일은 이후 컨텍스트 보강 및 SQL 생성 시 힌트로 사용됩니다.
31+ """
1832 is_timeseries : bool = Field (description = "시계열 분석 필요 여부" )
1933 is_aggregation : bool = Field (description = "집계 함수 필요 여부" )
2034 has_filter : bool = Field (description = "조건 필터 필요 여부" )
@@ -26,6 +40,15 @@ class QuestionProfile(BaseModel):
2640
2741# QueryMakerChain
2842def create_query_maker_chain (llm ):
43+ """
44+ SQL 쿼리 생성을 위한 체인을 생성합니다.
45+
46+ Args:
47+ llm: LangChain 호환 LLM 인스턴스
48+
49+ Returns:
50+ Runnable: 입력 프롬프트를 받아 SQL을 생성하는 체인
51+ """
2952 prompt = get_prompt_template ("query_maker_prompt" )
3053 query_maker_prompt = ChatPromptTemplate .from_messages (
3154 [
@@ -36,6 +59,15 @@ def create_query_maker_chain(llm):
3659
3760
3861def create_query_enrichment_chain (llm ):
62+ """
63+ 사용자 질문을 메타데이터로 보강하기 위한 체인을 생성합니다.
64+
65+ Args:
66+ llm: LangChain 호환 LLM 인스턴스
67+
68+ Returns:
69+ Runnable: 보강된 질문 텍스트를 반환하는 체인
70+ """
3971 prompt = get_prompt_template ("query_enrichment_prompt" )
4072
4173 enrichment_prompt = ChatPromptTemplate .from_messages (
@@ -49,6 +81,15 @@ def create_query_enrichment_chain(llm):
4981
5082
5183def create_profile_extraction_chain (llm ):
84+ """
85+ 질문으로부터 `QuestionProfile`을 추출하는 체인을 생성합니다.
86+
87+ Args:
88+ llm: LangChain 호환 LLM 인스턴스
89+
90+ Returns:
91+ Runnable: `QuestionProfile` 구조화 출력을 반환하는 체인
92+ """
5293 prompt = get_prompt_template ("profile_extraction_prompt" )
5394
5495 profile_prompt = ChatPromptTemplate .from_messages (
@@ -61,9 +102,28 @@ def create_profile_extraction_chain(llm):
61102 return chain
62103
63104
105+ def create_question_gate_chain (llm ):
106+ """
107+ 질문 적합성(Question Gate) 체인을 생성합니다.
108+
109+ ChatPromptTemplate(SystemMessage) + LLM 구조화 출력으로
110+ `QuestionSuitability`를 반환합니다.
111+
112+ Args:
113+ llm: LangChain 호환 LLM 인스턴스
114+
115+ Returns:
116+ Runnable: invoke({"question": str}) -> QuestionSuitability
117+ """
118+
119+ prompt = get_prompt_template ("question_gate_prompt" )
120+ gate_prompt = ChatPromptTemplate .from_messages (
121+ [SystemMessagePromptTemplate .from_template (prompt )]
122+ )
123+ return gate_prompt | llm .with_structured_output (QuestionSuitability )
124+
125+
64126query_maker_chain = create_query_maker_chain (llm )
65127profile_extraction_chain = create_profile_extraction_chain (llm )
66128query_enrichment_chain = create_query_enrichment_chain (llm )
67-
68- if __name__ == "__main__" :
69- pass
129+ question_gate_chain = create_question_gate_chain (llm )
0 commit comments