從向量到圖形

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Meri Nova

Machine Learning Engineer

向量式 RAG 的限制

典型的向量存放區 RAG 工作流程。

包含「主題與關係」的泡泡。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

向量式 RAG 的限制

典型的向量存放區 RAG 工作流程。

包含「可擴充性」的泡泡。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

向量式 RAG 的限制

典型的向量存放區 RAG 工作流程。

包含「整合多元資料」的泡泡。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

圖形資料庫

顯示人物、地點與興趣之間連結的圖。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

圖形資料庫:節點

顯示人物、地點與興趣之間連結的圖。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

圖形資料庫:邊

顯示人物、地點與興趣之間連結的圖。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Neo4j 圖形資料庫

 

Neo4j 標誌。

LangChain 標誌。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

從圖像到圖形…

顯示人物、地點與興趣之間連結的圖。

使用 LangChain 的 Retrieval Augmented Generation(RAG)

從圖像到圖形…

barcelona_graph.jpg

使用 LangChain 的 Retrieval Augmented Generation(RAG)

從圖像到圖形…

barcelona_graph.jpg

使用 LangChain 的 Retrieval Augmented Generation(RAG)

載入並切分 Wikipedia 頁面

from langchain_community.document_loaders import WikipediaLoader
from langchain_text_splitters import TokenTextSplitter

raw_documents = WikipediaLoader(query="large language model").load()

text_splitter = TokenTextSplitter(chunk_size=100, chunk_overlap=20) documents = text_splitter.split_documents(raw_documents[:3]) print(documents[0])
page_content='A large language model (LLM) is a computational model capable of...' 
metadata={'title': 'Large language model',
          'summary': "A large language model (LLM) is...",
          'source': 'https://en.wikipedia.org/wiki/Large_language_model'}
使用 LangChain 的 Retrieval Augmented Generation(RAG)

從文字到圖形!

from langchain_openai import ChatOpenAI

from langchain_experimental.graph_transformers import LLMGraphTransformer
llm = ChatOpenAI(api_key="...", temperature=0, model_name="gpt-4o-mini")
llm_transformer = LLMGraphTransformer(llm=llm)
graph_documents = llm_transformer.convert_to_graph_documents(documents) print(graph_documents)
使用 LangChain 的 Retrieval Augmented Generation(RAG)

從文字到圖形!

[GraphDocument(
    nodes=[
        Node(id='Llm', type='Computational model'),
        Node(id='Language Generation', type='Concept'),
        Node(id='Natural Language Processing Tasks', type='Concept'),
        Node(id='Llama Family', type='Computational model'),
        Node(id='Ibm', type='Organization'),
        ..., Node(id='Bert', type='Computational model')],
    relationships=[
        Relationship(source=Node(id='Llm', type='Computational model'),
                     target=Node(id='Language Generation', type='Concept'),
                     type='CAPABLE_OF'),
        ...])]
使用 LangChain 的 Retrieval Augmented Generation(RAG)

一起來練習吧!

使用 LangChain 的 Retrieval Augmented Generation(RAG)

Preparing Video For Download...