IDEA-8: GraphRAG — NetworkX 기반 지식 그래프

- td_knowledge_graph 테이블 (user_id, subject, relation, object 트리플)
- GraphService: MultiDiGraph 인메모리 캐시 + MySQL 영속화
- add_relation / query_entity LangChain 도구
- call_model에 그래프 요약 자동 주입 (시스템 프롬프트)
- GRAPH_ENABLED=true 환경변수로 활성화
- requirements.txt에 networkx>=3.0 추가

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
sal
2026-06-04 10:08:39 +09:00
parent 0b50444e43
commit a05d2f474e
10 changed files with 170 additions and 6 deletions
+22
View File
@@ -17,6 +17,7 @@ class AgentState(TypedDict):
crag_fallback_used: bool
from services.agent.tools import get_current_date, make_memory_tools, make_reminder_tools, make_retriever_tool, make_search_tool, make_vision_tool, web_search
from services.agent.graph_tools import make_graph_tools
class AgentService:
@@ -41,6 +42,7 @@ class AgentService:
ingestion_service=None,
crag_enabled: bool = False,
conv_rag_enabled: bool = False,
graph_service=None,
user_id: str = "default",
):
self._system_prompt = system_prompt
@@ -60,6 +62,7 @@ class AgentService:
self._ingestion_service = ingestion_service
self._crag_enabled = crag_enabled
self._conv_rag_enabled = conv_rag_enabled
self._graph_service = graph_service
if conversation_repository:
try:
@@ -91,6 +94,9 @@ class AgentService:
if reminder_repository is not None:
set_reminder_tool, list_reminders_tool = make_reminder_tools(reminder_repository, user_id)
self._base_tools += [set_reminder_tool, list_reminders_tool]
if graph_service is not None:
add_relation_tool, query_entity_tool = make_graph_tools(graph_service, user_id)
self._base_tools += [add_relation_tool, query_entity_tool]
self._vision_model = None # set via set_vision_model()
self._llm_with_tools = chat_model.bind_tools(self._base_tools)
self._chat_model = chat_model
@@ -103,6 +109,22 @@ class AgentService:
f"오늘 날짜: {date.today().isoformat()}\n\n"
+ self._system_prompt
)
if self._graph_service:
graph_summary = self._graph_service.get_summary(self._user_id)
if graph_summary:
system_content += (
"\n\n## 지식 그래프 (저장된 관계 정보)\n"
+ graph_summary
+ "\n\n**지식 그래프 사용 규칙**: 가족·사물 간 관계 정보(알레르기, "
"가족 관계, 선호도, 질환 등)는 add_relation으로 저장하고, "
"특정 인물 정보 조회 시 query_entity를 먼저 호출하세요."
)
else:
system_content += (
"\n\n**지식 그래프 사용 규칙**: 가족·사물 간 관계 정보(알레르기, "
"가족 관계, 선호도, 질환 등)를 언급하면 add_relation으로 저장하세요."
)
if self._profile_repo:
profile = self._profile_repo.get_all(self._user_id)
if profile: