グラフとRAG

Graph RAG with LangChain and Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

講師紹介

 

  • Neo4jのDeveloper Educationマネージャー
  • Neo4j GraphAcademyでの開発者教育
  • ソフトウェア開発20年以上
  • Neo4j経験10年以上

 

Adamの写真

1 https://graphacademy.neo4j.com
Graph RAG with LangChain and Neo4j

RAGのR

ユーザー入力がプロンプトに追加されLLMに送られるアプリ構成図

Graph RAG with LangChain and Neo4j

RAGのR

プロンプトにはドキュメントやデータソースからのコンテキストが必要

Graph RAG with LangChain and Neo4j

RAGのR

このデータはしばしばベクトル埋め込みに変換される

Graph RAG with LangChain and Neo4j

セマンティック検索が失敗するのは?

ベクトルが有効な場面

  • あいまい・オープンな質問

ベクトルが苦手な場面

  • きわめて具体的・事実ベースの質問
  • 数値・完全一致の照合
  • 論理的な問い合わせ

Paul Grahamは生成AIをどう考えていますか?

Paul Grahamは生成AIスタートアップにいくつ投資しましたか?

Graph RAG with LangChain and Neo4j

ベクトル 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'」の歌詞に登場するエンティティのグラフ図。Small town boyがSouth DetroitにBORNとRAISEDの関係を持つ

Graph RAG with LangChain and Neo4j

ベクトル 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

Graph RAG with LangChain and Neo4j

ベクトル 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ノードへ

Graph RAG with LangChain and Neo4j

ナレッジグラフとNeo4j

 

  • Neo4jは世界をリードするグラフデータベース
  • 柔軟・スキーマ任意
  • langchain-neo4j によるLangChain統合
  • LCEL: LangChain Expression Language

neo4jとlangchainのロゴ

1 https://db-engines.com/en/ranking/graph%20dbms
Graph RAG with LangChain and Neo4j

ノードは「もの」を表す

from langchain_neo4j.graphs.graph_document \
  import Node

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

IDとTitleのプロパティを持つ本ノードのグラフ図

Graph RAG with LangChain and Neo4j

リレーションはノードを結ぶ

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(





)

本ノードから著者を表す2つのPersonノードへ伸びるWRITTEN_BY関係

Graph RAG with LangChain and Neo4j

リレーションはノードを結ぶ

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ノード

Graph RAG with LangChain and Neo4j

リレーションはノードを結ぶ

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

Graph RAG with LangChain and Neo4j

リレーションはノードを結ぶ

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と2つのWRITTEN_BY関係を含むノード図。ノードが複数の関係を持てることを示す

Graph RAG with LangChain and Neo4j

Neo4jへの接続

from langchain_neo4j import Neo4jGraph


graph = Neo4jGraph( url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD )
1 https://neo4j.com/docs/getting-started/
Graph RAG with LangChain and Neo4j

グラフドキュメントの保存

from langchain_neo4j.graphs.graph_document import GraphDocument


# ノードとリレーションのラッパー doc = GraphDocument(
nodes=[book, jesus, jim],
relationships=[jesus_wrote_book, jim_wrote_book]
)
# ノードとリレーションをDBに保存 graph.add_graph_documents([graph_document])
Graph RAG with LangChain and Neo4j

GraphRAGとは?

ドキュメントとその埋め込みからのコンテキストを含む前の図

Graph RAG with LangChain and Neo4j

GraphRAGとは?

ドキュメントからのコンテキストに加え、抽出されたノードとリレーションのナレッジグラフを示す図

Graph RAG with LangChain and Neo4j

Vamos praticar!

Graph RAG with LangChain and Neo4j

Preparing Video For Download...