Domain graphs

Graph RAG ด้วย LangChain และ Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Domain graphs

ไดอะแกรม knowledge graph แสดง lexical graph ด้านซ้ายซึ่งมีหนังสือ บท และหน้า ส่วนด้านขวาแสดงบุคคลและบริษัทในหนังสือพร้อมอ้างอิงจากแหล่งอื่น เช่น Wikipedia

Graph RAG ด้วย LangChain และ Neo4j

จาก Lexical graphs สู่ Domain graphs

เนื้อเพลง Don't Stop Believing ของวง Journey

Graph RAG ด้วย LangChain และ Neo4j

จาก Lexical graphs สู่ Domain graphs

เนื้อเพลง Don't Stop Believing ของวง Journey โดยมี "Small Town Boy" ถูกไฮไลต์เป็นบุคคล

Graph RAG ด้วย LangChain และ Neo4j

จาก Lexical graphs สู่ Domain graphs

เนื้อเพลง Don't Stop Believing ของวง Journey โดยมี "South Detroit" ถูกไฮไลต์เป็นสถานที่

Graph RAG ด้วย LangChain และ Neo4j

จาก Lexical graphs สู่ Domain graphs

เนื้อเพลง Don't Stop Believing ของวง Journey โดยมี "Born" ถูกไฮไลต์เป็นความสัมพันธ์ระหว่างโหนดบุคคล "Small Town Boy" กับโหนดสถานที่ "South Detroit"

Graph RAG ด้วย LangChain และ Neo4j

จาก Lexical graphs สู่ Domain graphs

เนื้อเพลง Don't Stop Believing ของวง Journey โดยมี "Raised" ถูกไฮไลต์เป็นความสัมพันธ์ระหว่างโหนดบุคคล "Small Town Boy" กับโหนดสถานที่ "South Detroit"

Graph RAG ด้วย LangChain และ Neo4j

ตัวอย่าง Domain graph

ตัวอย่าง domain graph model แสดงบุคคลที่ทำงานในบริษัทซึ่งตั้งอยู่ที่ที่อยู่หนึ่ง บุคคลนั้นถือบัตรเครดิตที่มีคำสั่งซื้อกับบริษัทในสถานที่เดียวกัน

Graph RAG ด้วย LangChain และ Neo4j

Structured outputs

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")
Graph RAG ด้วย LangChain และ Neo4j

การใช้ Structured outputs

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...])
Graph RAG ด้วย LangChain และ Neo4j

การบันทึก Entity

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])
Graph RAG ด้วย LangChain และ Neo4j

มาฝึกกันเถอะ!

Graph RAG ด้วย LangChain และ Neo4j

Preparing Video For Download...