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:
+24
View File
@@ -0,0 +1,24 @@
from langchain_core.tools import tool
def make_graph_tools(graph_service, user_id: str = "default"):
"""지식 그래프 저장/조회 Tool 쌍을 반환한다."""
@tool
def add_relation(subject: str, relation: str, obj: str) -> str:
"""가족 구성원이나 사물 사이의 관계를 지식 그래프에 저장합니다.
알레르기·가족 관계·선호도·질환·특기 등 관계형 정보를 저장할 때 사용하세요.
예:
subject='도율', relation='알레르기', obj='복숭아'
subject='아록', relation='자녀', obj='도율'
subject='근혜', relation='직업', obj='간호사'
subject='하율', relation='좋아하는음식', obj='바나나'"""
return graph_service.add_relation(subject, relation, obj, user_id)
@tool
def query_entity(entity: str) -> str:
"""특정 인물이나 사물에 대해 저장된 모든 관계 정보를 조회합니다.
예: entity='도율' → 도율의 알레르기, 나이, 부모, 좋아하는 것 등 모든 알려진 관계"""
return graph_service.query_entity(entity, user_id)
return add_relation, query_entity