Skip to main content

Implementation of the GraphReader Paper

Project description

GraphReader

Overview

GraphReader is a graph-based agent system designed to enhance long-context capabilities for Large Language Models (LLMs). This project is based on the paper "GraphReader: Building Graph-based Agent to Enhance Long-Context Abilities of Large Language Models." The system aims to address the challenges of processing long and complex texts by structuring the content into a graph and using an LLM agent to explore the graph autonomously.

GraphReader employs Retrieval-Augmented Generation (RAG) techniques to break down large texts into manageable chunks, store them in a graph, and retrieve relevant information based on user queries. It leverages advanced AI tools like Langchain, Pinecone for vector searches, and OpenAI's GPT-3.5-turbo, allowing for efficient query processing over long input contexts.

Key Features

  • Handles Long Contexts: Processes long documents by structuring them into a graph for efficient exploration and retrieval.
  • RAG Techniques: Uses Retrieval-Augmented Generation (RAG) techniques to fetch relevant graph nodes based on user queries.
  • LLM Integration: Employs GPT-3.5-turbo for reasoning, planning, and answering user queries by exploring the graph.
  • Graph-based Exploration: Structures large texts as a graph, enabling coarse-to-fine exploration of nodes and their relationships.
  • Predefined Toolset: Utilizes a set of predefined functions to read nodes and their neighbors, optimizing the exploration process.

Installation

  1. Install the required dependencies:
pip install -r requirements.txt

Example Usage

  1. Initialize the document and graph:
file_path = 'path/to/your/document.doc.pdf'
doc = Document(file_path)
chunk_dict = doc.get_chunks(st_ind=705)
g = Graph(chunk_dict, openai_api_key = "your_api_key")
doc.export_chunks()
g.export_graph()
  1. Initialize GraphReader and query the system:
g_reader = GraphReader(graph=g, vect_db_name='graph-reader-test5', pinecone_api_key = "pinecone_api_key", openai_api_key="openai_api_key")

user_query = input(prompt='Enter your query: ')
response = g_reader.get_response(query=user_query)
print(response)

How It Works

GraphReader Workflow

  1. Document Chunking: The text document is chunked into smaller pieces for easy processing. This is done using the Document class, which extracts content from the file and chunks it.

  2. Graph Creation: The chunks are organized into a graph structure, where each node represents a section of the document. The graph is then stored in a Pinecone vector database for efficient querying.

  3. LLM-Based Querying: When a user provides a query, the LLM agent reasons through the content in the graph. It selects relevant nodes, reads their content, and navigates through the graph to generate a coherent response.

  4. Response Generation: The agent explores the graph in a step-by-step manner using a rational plan. It reads node contents, neighbors, and continues gathering information until it has enough data to answer the query.

Experimental Results

Based on the paper, GraphReader demonstrates superior long-context processing capabilities. The agent consistently outperforms models like GPT-4-128k across a variety of benchmarks. For context lengths ranging from 16k to 256k, GraphReader shows a large margin of improvement, highlighting its ability to handle complex and extended inputs efficiently.

Tools and Techniques

  • Langchain: Provides the framework for LLM interaction and tool usage.
  • Pinecone: Vector database used to store and query the graph data.
  • OpenAI GPT-3.5-turbo: LLM used for reasoning, planning, and generating responses.
  • RAG (Retrieval-Augmented Generation): The technique used to retrieve relevant nodes from the graph based on the user’s query.

License

This project is licensed under the MIT License. See the LICENSE file for more information.


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphreader-0.0.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphreader-0.0.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file graphreader-0.0.1.tar.gz.

File metadata

  • Download URL: graphreader-0.0.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for graphreader-0.0.1.tar.gz
Algorithm Hash digest
SHA256 583184f6e75e231a1bd0bec64fdfac3cdad620ef09bb6accbecc498024e9db00
MD5 bdda1c4f58e2df7354e5f4d38e5a470c
BLAKE2b-256 2956994a43df5885ebe0d2cda56111a19369f7cf5267c4aea5406858cdabed76

See more details on using hashes here.

File details

Details for the file graphreader-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: graphreader-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for graphreader-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1bc6535bb8419d555e67c21199860d23609800934497c57af1a533fc7548394c
MD5 61a2be35cc610d59b419676e894fd0a4
BLAKE2b-256 59bc852eea12167ae5842e4b4e7754e157fbc8eaf1e1e7d729396a99e97702d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page