การจัดเก็บและการคิวรีเอกสาร

Retrieval Augmented Generation (RAG) ด้วย LangChain

Meri Nova

Machine Learning Engineer

การสร้าง instance ฐานข้อมูล Neo4j

from langchain_community.graphs import Neo4jGraph

graph = Neo4jGraph(url="bolt://localhost:7687", username="neo4j", password="...")
import os

url = os.environ["NEO4J_URI"]
user = os.environ["NEO4J_USERNAME"]
password = os.environ["NEO4J_PASSWORD"]

graph = Neo4jGraph(url=url, username=user, password=password)
1 https://neo4j.com/download/
Retrieval Augmented Generation (RAG) ด้วย LangChain

การจัดเก็บ graph documents

from langchain_experimental.graph_transformers import LLMGraphTransformer

llm = ChatOpenAI(api_key="...", temperature=0, model="gpt-4o-mini")
llm_transformer = LLMGraphTransformer(llm=llm)

graph_documents = llm_transformer.convert_to_graph_documents(documents)
Retrieval Augmented Generation (RAG) ด้วย LangChain

การจัดเก็บ graph documents

graph.add_graph_documents(
  graph_documents,

include_source=True,
baseEntityLabel=True
)
  • include_source=True: เชื่อมโยงโหนดกับเอกสารต้นทางด้วย edge MENTIONS
  • baseEntityLabel=True: เพิ่ม label __Entity__ ให้แต่ละโหนด
Retrieval Augmented Generation (RAG) ด้วย LangChain

graph documents ที่แสดงเป็นโหนดและ edge

Retrieval Augmented Generation (RAG) ด้วย LangChain

ภาพขยายจากรูปก่อนหน้า แสดงโหนดที่เกี่ยวกับโมเดล OpenAI

Retrieval Augmented Generation (RAG) ด้วย LangChain

Schema ของฐานข้อมูล

print(graph.get_schema)
Node properties:
Concept {id: STRING}
Architecture {id: STRING}
Organization {id: STRING}
Event {id: STRING}
Paper {id: STRING}

The relationships:
(:Concept)-[:DEVELOPED_BY]->(:Person)
(:Architecture)-[:BASED_ON]->(:Concept)
(:Organization)-[:PROPOSED]->(:Concept)
(:Document)-[:MENTIONS]->(:Event)
(:Paper)-[:BASED_ON]->(:Concept)
Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี Neo4j - Cypher Query Language

โหนดชื่อ James มี relationship ชื่อ friends ชี้ไปยังโหนด person ที่ไม่ระบุชื่อ

Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี Neo4j - Cypher Query Language

cypher4.jpg

Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี Neo4j - Cypher Query Language

cypher4.jpg

Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี Neo4j - Cypher Query Language

cypher4.jpg

Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี Neo4j - Cypher Query Language

cypher4.jpg

Retrieval Augmented Generation (RAG) ด้วย LangChain

การคิวรี LLM graph

results = graph.query("""
MATCH (gpt4:Model {id: "Gpt-4"})-[:DEVELOPED_BY]->(org:Organization)
RETURN org
""")

print(results)
[{'org': {'id': 'Openai'}}]
Retrieval Augmented Generation (RAG) ด้วย LangChain

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

Retrieval Augmented Generation (RAG) ด้วย LangChain

Preparing Video For Download...