लेक्सिकल ग्राफ़ को वेक्टर सर्च के साथ जोड़ना

LangChain और Neo4j के साथ Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

सिमेंटिक सर्च में रिलेशनशिप्स का उपयोग

Neo4j में काम करना कैसा है?

एक ग्राफ़ डायग्राम जिसमें Review नोड है और text प्रॉपर्टी "Neo4j is a great place to work"

LangChain और Neo4j के साथ Graph RAG

सिमेंटिक सर्च में रिलेशनशिप्स का उपयोग

Neo4j में काम करना कैसा है?

यह रिव्यू एक Person द्वारा पोस्ट किया गया है जिसका स्क्रीन नाम @emileifrem है

LangChain और Neo4j के साथ Graph RAG

सिमेंटिक सर्च में रिलेशनशिप्स का उपयोग

Neo4j में काम करना कैसा है?

Emil Eifrem का WORKS_AT रिलेशनशिप एक Company नोड से है जिसका नाम Neo4j है

LangChain और Neo4j के साथ Graph RAG

Text प्रॉपर्टीज़ से embeddings

 

  • text प्रॉपर्टी से नोड embeddings बनाएँ

एक scene जिसमें HAS_CHUNK रिलेशनशिप्स हैं Chunk नोड्स से जिनमें string प्रॉपर्टी है

LangChain और Neo4j के साथ Graph RAG

Text प्रॉपर्टीज़ से embeddings

 

  • text प्रॉपर्टी से नोड embeddings बनाएँ
  • embeddings पर वेक्टर सर्च करें
  • संदर्भ के लिए रिलेशनशिप्स उपयोग करें

नोड्स पर मौजूद text प्रॉपर्टीज़ से embedding बनाई जाती है

LangChain और Neo4j के साथ Graph RAG

वेक्टर स्टोर के रूप में Neo4j

from langchain_neo4j import Neo4jVector

 

  • Neo4jVector.from_documents(): Documents से नोड्स और अंडरलाइंग इंडेक्स बनाता है
  • Neo4jVector.from_existing_graph(): मौजूदा लेबल और प्रॉपर्टी कॉम्बिनेशंस पर इंडेक्स बनाता है
LangChain और Neo4j के साथ Graph RAG

मौजूदा नोड प्रॉपर्टीज़ को चंक करना

एक नॉलेज ग्राफ़ डायग्राम जिसमें Act नोड से Scene नोड तक HAS_SCENE रिलेशनशिप हाईलाइट है

res = graph.query("""

MATCH (s:Scene)
WHERE NOT {(s)-[:HAS_LINE]->()}
RETURN s.id AS id, s.text AS text
""")
for scene in res: # Create chunks from scene['text']
LangChain और Neo4j के साथ Graph RAG

मौजूदा नोड प्रॉपर्टीज़ को चंक करना

  • text को "\n\n" से स्प्लिट करें
  • स्टेज डायरेक्शंस एक लाइन में होती हैं
  • बोले गए डायलॉग कैरेक्टर नाम से शुरू होते हैं
    • अगली लाइनों में उसी के बोले शब्द होते हैं
 Enter Sampson and Gregory armed with swords..

SAMPSON.
Gregory, on my word...
GREGORY. No, for then we should...
SAMPSON. I mean, if we be in choler, we'll draw...
LangChain और Neo4j के साथ Graph RAG

मौजूदा नोड प्रॉपर्टीज़ को चंक करना

# Split text into lines
lines = [
    line.strip() for line in text.split("\n\n")

if "\n" in line
]
for line in lines: parts = line.split("\n")
character = parts[0]
text = "\n".join(parts[1:])
LangChain और Neo4j के साथ Graph RAG

मौजूदा नोड प्रॉपर्टीज़ को चंक करना

# ...Continuing loop over scenes

line_node = Node(type="Line", id=f"{scene.id}-line-{i}", properties={})
character_node = Node(type="Character", id=character, properties={})


# (:Scene)-[:HAS_LINE]->(:Line) graph_document.relationships.append( Relationship(source=scene, target=line_node, type="HAS_LINE"))
# (:Line)-[:SPOKEN_BY]->(:Character) graph_document.relationships.append(Relationship( source=line_node, target=characters[character], type="SPOKEN_BY"))
LangChain और Neo4j के साथ Graph RAG

मौजूदा ग्राफ़ पर वेक्टर इंडेक्स बनाना

from langchain.embeddings import init_embeddings
store = Neo4jVector.from_existing_graph(

init_embeddings("openai:text-embedding-3-small"),
url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD,
node_label="Line",
text_node_properties=["text"],
embedding_node_property="embedding",
index_name="lines",
)
LangChain और Neo4j के साथ Graph RAG

LCEL के साथ वेक्टर रिट्रीवल

# Create retriever
retriever = store.as_retriever()

# Invoke manually retriever.invoke("What does Romeo think of Juliet?") # [ Document, Document, ... ]
LangChain और Neo4j के साथ Graph RAG

अभ्यास करते हैं!

LangChain और Neo4j के साथ Graph RAG

Preparing Video For Download...