도메인 그래프

LangChain과 Neo4j로 배우는 Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

도메인 그래프

좌측에는 책, 장, 페이지로 된 어휘 그래프, 우측에는 책의 인물과 회사 및 위키피디아 등 외부 출처의 참조를 보여주는 지식 그래프 다이어그램

LangChain과 Neo4j로 배우는 Graph RAG

어휘 그래프에서 도메인 그래프로

Journey의 'Don't Stop Believin'' 가사

LangChain과 Neo4j로 배우는 Graph RAG

어휘 그래프에서 도메인 그래프로

Journey의 'Don't Stop Believin'' 가사에서 "Small Town Boy"가 인물로 하이라이트됨

LangChain과 Neo4j로 배우는 Graph RAG

어휘 그래프에서 도메인 그래프로

Journey의 'Don't Stop Believin'' 가사에서 "South Detroit"가 위치로 하이라이트됨

LangChain과 Neo4j로 배우는 Graph RAG

어휘 그래프에서 도메인 그래프로

Journey의 'Don't Stop Believin'' 가사에서 사람 "Small Town Boy"와 위치 "South Detroit" 사이의 관계로 "Born"이 하이라이트됨

LangChain과 Neo4j로 배우는 Graph RAG

어휘 그래프에서 도메인 그래프로

Journey의 'Don't Stop Believin'' 가사에서 사람 "Small Town Boy"와 위치 "South Detroit" 사이의 관계로 "Raised"가 하이라이트됨

LangChain과 Neo4j로 배우는 Graph RAG

도메인 그래프 예시

사람이 회사에서 일하고 주소에 위치한 도메인 그래프 모델 예시. 그 사람은 신용카드를 보유하고, 같은 위치의 회사에서 해당 카드로 주문함

LangChain과 Neo4j로 배우는 Graph RAG

구조화 출력

from pydantic import BaseModel, Field

from typing import Optional
class Character(BaseModel): """A character from the play."""
id: str = Field(description="A unique identifier for the character in slug-case",
examples=["montague-romeo", "tybalt"])
name: str = Field(description="The full name of the character", examples=["Romeo", "Tybalt"])
family: Optional[str] = Field(description="The name of the family they belong to", examples=["Montague", "Capulet"])
class CharacterOutput(BaseModel): characters: list[Character] = Field(description="A list of characters")
LangChain과 Neo4j로 배우는 Graph RAG

구조화 출력 사용

from langchain.chat_models import init_chat_model
llm = init_chat_model("gpt-4o-mini", model_provider="openai", api_key="...")


# With a structured output character_model = llm.with_structured_output(CharacterOutput)
# Use within a chain chain = extraction_prompt | character_model
# Handle the response output = chain.invoke(...)
CharacterOutput(characters=[Character, Character, Character...])
LangChain과 Neo4j로 배우는 Graph RAG

엔터티 저장

nodes = [

Node(
type="Character",
id=character.id,
properties={"name": character.name, "family": character.family}
)
for character in output.characters
]
# Build a GraphDocument graph_document = GraphDocument(nodes=nodes, relationships=[])
# Save to the graph graph.add_graph_documents([graph_document])
LangChain과 Neo4j로 배우는 Graph RAG

Vamos praticar!

LangChain과 Neo4j로 배우는 Graph RAG

Preparing Video For Download...