Skip to content

Commit 5e5accd

Browse files
committed
feat(graph) : CONTEXT_ENRICHMENT 노드 추가 refined된 질문에서 테이블 정보를 정렬하는 노드
1 parent 885e3f5 commit 5e5accd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

llm_utils/graph.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,56 @@ def query_refiner_node(state: QueryMakerState):
6565
return state
6666

6767

68+
# 노드 함수: CONTEXT_ENRICHMENT 노드
69+
def context_enrichment_node(state: QueryMakerState):
70+
71+
import json
72+
73+
searched_tables = state["searched_tables"]
74+
searched_tables_json = json.dumps(searched_tables, ensure_ascii=False, indent=2)
75+
76+
question_profile = state["question_profile"].model_dump()
77+
question_profile_json = json.dumps(question_profile, ensure_ascii=False, indent=2)
78+
79+
from langchain.prompts import PromptTemplate
80+
81+
enrichment_prompt = PromptTemplate(
82+
input_variables=["refined_question", "profiles", "related_tables"],
83+
template="""
84+
You are a smart assistant that takes a user question and enriches it using:
85+
1. Question profiles: {profiles}
86+
2. Table metadata (names, columns, descriptions):
87+
{related_tables}
88+
89+
Tasks:
90+
- Correct any wrong terms by matching them to actual column names.
91+
- If the question is time-series or aggregation, add explicit hints (e.g., "over the last 30 days").
92+
- If needed, map natural language terms to actual column values (e.g., ‘미국’ → ‘USA’ for country_code).
93+
- Output the enriched question only.
94+
95+
Refined question:
96+
{refined_question}
97+
98+
Using the refined version for enrichment, but keep original intent in mind.
99+
""".strip(),
100+
)
101+
102+
llm = get_llm()
103+
prompt = enrichment_prompt.format_prompt(
104+
refined_question=state["refined_input"],
105+
profiles=question_profile_json,
106+
related_tables=searched_tables_json,
107+
)
108+
enriched_text = llm.invoke(prompt.to_messages())
109+
110+
state["refined_input"] = enriched_text
111+
from langchain_core.messages import HumanMessage
112+
113+
state["messages"].append(HumanMessage(content=enriched_text.content))
114+
115+
return state
116+
117+
68118
def get_table_info_node(state: QueryMakerState):
69119
# retriever_name과 top_n을 이용하여 검색 수행
70120
documents_dict = search_tables(

0 commit comments

Comments
 (0)