Skip to content

ragmate/Adaptive-Cohesion-based-Synthesis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adaptive Cohesion-based Synthesis (ACoS)

License: MIT

ACoS is an advanced semantic retrieval algorithm designed for next-generation Retrieval-Augmented Generation (RAG) systems. It transforms scattered, raw search results into single, coherent, and contextually rich text fragments.

Instead of feeding a Large Language Model (LLM) a noisy list of disconnected chunks, ACoS acts as a "synthesis" layer, reconstructing a logical narrative that significantly improves the quality and relevance of the final generated answer.

Core Concepts

The name "Adaptive Cohesion-based Synthesis" encapsulates the three core principles of the algorithm:

🧠 Adaptive

This highlights the algorithm's intelligence. The chaining threshold isn't a fixed number; it dynamically adapts to the statistical properties (median/IQR of sentence similarities) of each specific document. This makes the retrieval robust across different text styles and densities.

🔗 Cohesion-based

This describes the core mechanism. The algorithm builds fragments by ensuring strong semantic cohesion between adjacent sentences. This creates a truly logical and readable flow, ensuring the retrieved context is a coherent story, not just a collection of facts.

✨ Synthesis

This describes its ultimate purpose in a modern RAG pipeline. It doesn't just "retrieve" or "find" chunks; it takes raw, scattered pieces of information and synthesizes them into a new, complete, and unified context, ready for the LLM.


Why ACoS Matters: A Quality Comparison

The true value of ACoS is the significant improvement in the quality of the final generated answer. By providing the LLM with a clean, synthesized context instead of noisy, raw chunks, the response becomes more accurate and focused.

Here is a direct comparison using the same query ("Why did AI progress slow down in the past?"):

Standard RAG Retriever

Final Answer with the default retriever from RAG Chain: Progress of artificial intelligence slowed down in the past due to computational limitations and a lack of large datasets, leading to reduced funding and many researchers abandoning the field during the "AI winter."

RAG with ACoS Compressor

Final Answer with ACoS from RAG Chain: Progress in artificial intelligence slowed down due to computational limitations and a lack of large datasets.

As you can see, ACoS provides the LLM with a clean and focused context, allowing it to generate a more precise and concise answer that directly addresses the user's question.


Installation

The project relies on several core NLP, machine learning libraries, and LangChain framework.

pip install -r requirements.txt

Setting up Your API Key

To run the code in example.py, you need to set your OpenAI API key as an environment variable.

export OPENAI_API_KEY='your-api-key-here'

Quick Start

A complete, runnable example showing how to set up and use the SemanticFragmentCompressor in a full RAG pipeline is located in the example.py file in this repository.

The example demonstrates:

  1. How to index a document into a in-memory vector store.
  2. How to create a base_retriever.
  3. How to wrap the base retriever with the SemanticFragmentCompressor to create an advanced ContextualCompressionRetriever.
  4. How to run the final RAG chain to get a high-quality answer.

Tuning the Algorithm: Understanding the Parameters

You can control the behavior of the ACoS algorithm by adjusting several key parameters. Understanding these will allow you to fine-tune the retrieval process for your specific documents and use cases.

start_threshold

  • What it is: The minimum similarity score a sentence must have with the original query to be considered a potential starting point for a new fragment.
  • How it works: This is the first filter. A higher value makes the initial selection of relevant zones stricter.
  • Analogy: A detective deciding which clues are interesting enough to start an investigation.
  • Typical Value: 0.5 - 0.65. Raise it if you get too many irrelevant fragments; lower it if you're missing relevant ones.

query_threshold

  • What it is: The minimum similarity score a sentence must have with the original query to be added to an existing chain.
  • How it works: This acts as a "global anchor," ensuring that the chain doesn't drift away from the original question, even if the sentences are locally connected to each other.
  • Analogy: The detective, while following a lead, periodically checks if the new evidence still relates to the original crime.
  • Typical Value: 0.3 - 0.4. It should generally be lower than start_threshold to allow for supporting sentences that are related but not the main point.

chain_threshold_multiplier

  • What it is: A multiplier applied to the document's adaptive cohesion threshold. It controls how strictly sentences must follow each other.
  • How it works:
    • 1.0 means a new sentence must be at least as related to the previous one as the "typical" (median) sentence connection in the document.
    • <1.0 (e.g., 0.9) makes the chaining more lenient, allowing for weaker connections.
    • >1.0 (e.g., 1.1) makes the chaining stricter, requiring a stronger local connection.
  • Analogy: Deciding how closely related two events must be to be part of the same timeline.
  • Typical Value: 0.9 - 1.1.

grace_margin

  • What it is: The number of consecutive "weak link" sentences the algorithm will tolerate before breaking a chain.
  • How it works: This is the "bridge" logic. A grace_margin of 1 allows the algorithm to "jump over" one sentence that fails the threshold checks, preserving the continuity of a larger logical block.
  • Analogy: The detective finds a confusing piece of evidence but continues, hoping the next clue will connect everything.
  • Typical Value: 1. Increasing it to 2 is possible but risks including more irrelevant text.

merge_window

  • What it is: The maximum number of sentences allowed in a "gap" between two separate fragments for them to be merged into one.
  • How it works: After finding all fragments, this parameter checks if any are separated by a small number of sentences (e.g., a short, irrelevant filler sentence). If the gap is within the window, the fragments and the gap between them are combined.
  • Analogy: A detective realizing that two separate evidence boards are actually part of the same case, connected by a minor detail they initially overlooked.
  • Typical Value: 1 - 3.

Architecture in a RAG Pipeline

ACoS is designed to work as a post-processing step after an initial candidate retrieval from a vector store. This two-stage process combines speed with high-quality synthesis.

[Query] -> [1. Vector DB Retriever] -> (Returns ~20 raw chunks) -> [2. ACoS Compressor] -> (Returns 1-2 synthesized fragments) -> [3. LLM]


Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any bugs, suggestions, or improvements.


License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages