Skip to main content

RAGStack Graph Store

Hybrid Graph Store combining vector similarity and edges between chunks.

Usage

  1. Pre-process your documents to populate metadata information.
  2. Create a Hybrid GraphStore and add your LangChain Documents.
  3. Retrieve documents from the GraphStore.

Populate Metadata

The Graph Store makes use of the following metadata fields on each Document:

  • content_id: If assigned, this specifies the unique ID of the Document. If not assigned, one will be generated. This should be set if you may re-ingest the same document so that it is overwritten rather than being duplicated.
  • links: A set of Links indicating how this node should be linked to other nodes.

Hyperlinks

To connect nodes based on hyperlinks, you can use the HtmlLinkExtractor as shown below:

from ragstack_knowledge_store.langchain.extractors import HtmlLinkExtractor

html_link_extractor = HtmlLinkExtractor()

for doc in documents:
    doc.metadata["content_id"] = doc.metadata["source"]

    # Add link tags from the page_content to the metadata.
    # Should be passed the HTML content as a string or BeautifulSoup.
    add_links(doc,
        html_link_extractor.extract_one(HtmlInput(doc.page_content, doc.metadata["source_url"])))

Store

import cassio
from langchain_openai import OpenAIEmbeddings
from ragstack_knowledge_store import GraphStore

cassio.init(auto=True)

graph_store = GraphStore(embeddings=OpenAIEmbeddings())

# Store the documents
graph_store.add_documents(documents)

Retrieve

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o")

# Retrieve and generate using the relevant snippets of the blog.
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

# Depth 0 - don't traverse edges. equivalent to vector-only.
# Depth 1 - vector search plus 1 level of edges
retriever = graph_store.as_retriever(k=4, depth=1)

template = """You are a helpful technical support bot. You should provide complete answers explaining the options the user has available to address their problem. Answer the question based only on the following context:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

def format_docs(docs):
    formatted = "\n\n".join(f"From {doc.metadata['content_id']}: {doc.page_content}" for doc in docs)
    return formatted


rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

Development

poetry install --with=dev

# Run Tests
poetry run pytest

Metadata

Release files for ragstack-ai-knowledge-store 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ragstack-ai-knowledge-store 0.2.1
File Size Uploaded
ragstack_ai_knowledge_store-0.2.1.tar.gz 17.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ragstack-ai-knowledge-store 0.2.1
File Interpreter ABI Platform
ragstack_ai_knowledge_store-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 35.7 kB

Release files / ragstack_ai_knowledge_store-0.2.1.tar.gz

Download URL ragstack_ai_knowledge_store-0.2.1.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
20424d42978be228feb887a71d76228638765446d1a37a1f6faef109e448fe06
BLAKE2b-256 checksum
How to use checksums
3ee1da324f5b590aef5ae07b8e7e38ee249101b2bfae6882ba1152c06f8228b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release files / ragstack_ai_knowledge_store-0.2.1-py3-none-any.whl

Download URL ragstack_ai_knowledge_store-0.2.1-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2749c6ab43768e14892dbc1190f2348e9006ad5eb77188e875d704ff811ccc6b
BLAKE2b-256 checksum
How to use checksums
3bab0afba28f5e9ff962130c8c08bea1ea609be98ec0be3175748911732bd2ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page