그래프와 RAG

LangChain과 Neo4j로 배우는 Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

강사 소개

 

  • Neo4j 개발자 교육 매니저
  • Neo4j GraphAcademy를 통한 개발자 교육
  • 소프트웨어 개발 20년+ 경력
  • Neo4j 10년+ 경험

 

Adam의 사진

1 https://graphacademy.neo4j.com
LangChain과 Neo4j로 배우는 Graph RAG

RAG의 R

사용자 입력이 프롬프트에 추가되어 LLM으로 전송되는 애플리케이션 다이어그램

LangChain과 Neo4j로 배우는 Graph RAG

RAG의 R

프롬프트에는 문서와 데이터 소스에서 온 컨텍스트가 필요함

LangChain과 Neo4j로 배우는 Graph RAG

RAG의 R

이 데이터는 종종 벡터 임베딩으로 변환됨

LangChain과 Neo4j로 배우는 Graph RAG

의미 검색은 어디서 실패하나요?

벡터가 적합한 경우

  • 모호하거나 개방형 질문

벡터가 비효율적인 경우

  • 매우 구체적이거나 사실 기반 질문
  • 수치형 또는 정확히 일치해야 하는 질의
  • 논리적 질의

Paul Graham은 생성형 AI에 대해 어떻게 생각하나요?

Paul Graham은 생성형 AI 스타트업에 몇 곳 투자했나요?

LangChain과 Neo4j로 배우는 Graph RAG

벡터 vs. 지식 그래프

텍스트 벡터 임베딩
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

Don't Stop Believin' 가사에 언급된 엔티티를 보여주는 그래프 다이어그램. 작은 마을 소년이 South Detroit와 BORN, RAISED 관계로 연결됨

LangChain과 Neo4j로 배우는 Graph RAG

벡터 vs. 지식 그래프

텍스트 벡터 임베딩
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

노드는 사람: Small town boy, 장소: South Detroit, 열차: Midnight Train, 장소: Anywhere

LangChain과 Neo4j로 배우는 Graph RAG

벡터 vs. 지식 그래프

텍스트 벡터 임베딩
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

BORN, RAISED 관계가 사람에서 장소로 향함. GOING 열차는 Midnight Train 노드에서 Anywhere 노드로 향함

LangChain과 Neo4j로 배우는 Graph RAG

지식 그래프와 Neo4j

 

  • Neo4j는 세계 선도 그래프 데이터베이스입니다
  • 유연함, 스키마 선택적
  • langchain-neo4j LangChain 통합
  • LCEL: LangChain Expression Language

neo4j와 langchain 로고

1 https://db-engines.com/en/ranking/graph%20dbms
LangChain과 Neo4j로 배우는 Graph RAG

노드는 사물을 나타냅니다

from langchain_neo4j.graphs.graph_document \
  import Node

book = Node(
type="Book",
id=f"9781098127107",
properties={ "title": "Building Knowledge Graphs" }
)

ID와 제목 속성이 있는 그래프의 책 노드

LangChain과 Neo4j로 배우는 Graph RAG

관계는 노드를 연결합니다

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(





)

한 Book 노드에서 두 Person 노드(저자)로 향하는 두 WRITTEN_BY 관계

LangChain과 Neo4j로 배우는 Graph RAG

관계는 노드를 연결합니다

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,



)

WRITTEN_BY 관계의 소스 노드는 Book 노드입니다

LangChain과 Neo4j로 배우는 Graph RAG

관계는 노드를 연결합니다

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,


)

WRITTEN_IN 관계의 대상 노드는 Person입니다

LangChain과 Neo4j로 배우는 Graph RAG

관계는 노드를 연결합니다

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,
type="WRITTEN_BY"
properties=dict(...)
)

노드 다이어그램: Book과 두 저자에 대한 WRITTEN_BY 관계로, 노드가 다수의 관계를 가질 수 있음을 보여줌.

LangChain과 Neo4j로 배우는 Graph RAG

Neo4j에 연결하기

from langchain_neo4j import Neo4jGraph


graph = Neo4jGraph( url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD )
1 https://neo4j.com/docs/getting-started/
LangChain과 Neo4j로 배우는 Graph RAG

그래프 문서 저장

from langchain_neo4j.graphs.graph_document import GraphDocument


# 노드와 관계를 감싸는 래퍼 doc = GraphDocument(
nodes=[book, jesus, jim],
relationships=[jesus_wrote_book, jim_wrote_book]
)
# 노드와 관계를 데이터베이스에 저장 graph.add_graph_documents([graph_document])
LangChain과 Neo4j로 배우는 Graph RAG

그렇다면 GraphRAG란?

이전 다이어그램에 문서와 임베딩 기반 컨텍스트가 추가됨

LangChain과 Neo4j로 배우는 Graph RAG

그렇다면 GraphRAG란?

이전 다이어그램에 문서 컨텍스트와, 문서에서 도출된 노드·관계를 가진 지식 그래프가 추가됨

LangChain과 Neo4j로 배우는 Graph RAG

연습해 봅시다!

LangChain과 Neo4j로 배우는 Graph RAG

Preparing Video For Download...