Skip to main content

DataStax RAGStack Graph Store

Project description

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

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

ragstack_ai_knowledge_store-0.2.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

ragstack_ai_knowledge_store-0.2.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file ragstack_ai_knowledge_store-0.2.0.tar.gz.

File metadata

File hashes

Hashes for ragstack_ai_knowledge_store-0.2.0.tar.gz
Algorithm Hash digest
SHA256 428800b05996a0873272aaec9eccc2e2efbdf99aeb209b69164715548fa43a5b
MD5 d6c2e1f0e4fdb4e8e8cec837be5596d5
BLAKE2b-256 70f7c110cca6cc229d002a3735fe5626478d94db95bca31ffdc2b5a93f8ba4df

See more details on using hashes here.

File details

Details for the file ragstack_ai_knowledge_store-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ragstack_ai_knowledge_store-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a9302512e8c4fa2991ab433a2b307c79dd04546b3652aff03a35f8e28dce10df
MD5 864ca1f28582255c2f2f40f02147f4e8
BLAKE2b-256 8ca55b526855e460200e505fdab54b4e0e3fc8106f536e71e13f11ef656cdd1f

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