RAG Citation is an project that combines Retrieval-Augmented Generation (RAG) with automatic citation generation. This tool is designed to enhance the credibility of AI-generated content by providing relevant citations for the information used in generating responses.
Project description
RAG Citation: Enhancing Rag Pipeline with Automatic Citations (A Non-LLM Approach)
Project Overview
RAG Citation is an project that combines Retrieval-Augmented Generation (RAG) with automatic citation generation. This tool is designed to enhance the credibility of RAG-generated content by providing relevant citations for the information used in generating responses.
Key Features
- Non-LLM Approach: Utilizes efficient algorithms and NLP techniques for citation generation, making it fast and lightweight.
- Semantic Search: Identifies relevant source documents based on meaning and context rather than just keyword matching.
- Named Entity Recognition: Extracts and returns relevant named entities from LLM-generated answers, such as people, organizations, money and dates.
- Flexible Integration: Can be easily integrated into rag pipeline.
- Hallucination (Beta) This beta feature identifies instances where the LLM-generated answer contains entities like ["DATE", "MONEY", "CARDINAL", "ORDINAL", "QUANTITY", "TIME"], but these entities cannot be found within the context. If such a mismatch occurs, it flags the result as a potential hallucination.
Quickstart
- Langchain example: langchain.ipynb
- Embeddchain example: embeddchain.ipynb
- custom embedding model: custom_embedding_model.ipynb
To get started with rag-citation
, install it using pip and download the spacy model:
pip install rag-citation
To download the spacy model-sm
python -m spacy download en_core_web_sm
To download the spacy model-md
python -m spacy download en_core_web_md
To download the spacy model-lg
python -m spacy download en_core_web_lg
Here's a basic example demonstrating how to use the library:
from rag_citation import CiteItem, Inference
import uuid
## Sample context from vectorDB or semantic search
documents = [
"Elon MuskCEO, Tesla$221.6B$439M (0.20%)Real Time Net Worthas of 8/6/24Reflects change since 5 pm ET of prior trading day. 1 in the world todayPhoto by Martin Schoeller for ForbesAbout Elon MuskElon Musk cofounded six companies, including electric car maker Tesla, rocket producer SpaceX and tunneling startup Boring Company.He owns about 12% of Tesla excluding options, but has pledged more than half his shares as collateral for personal loans of up to $3.5 billion.In early 2024, a Delaware judge voided Musk's 2018 deal to receive options equaling an additional 9% of Tesla.",
"people in the world; as of August 2024[update], Forbes estimates his net worth to be US$241 billion.[3] Musk was born in Pretoria to model Maye and businessman and engineer Errol Musk, and briefly attended the University of Pretoria before immigrating to Canada at age 18, acquiring citizenship through his Canadian-born mother. Two years later, he matriculated at Queen's University at Kingston in Canada. Musk later transferred to the University of Pennsylvania and received bachelor's degrees in economics and physics."
]
## Example answer generated by an LLM
answer = "Elon Musk's net worth is estimated to be US$241 billion as of August 2024."
## Helper function to generate a UUID
def generate_uuid():
return str(uuid.uuid4())
## Helper function to create context in the correct format
def format_document(documents):
context = []
for document in documents:
context.append(
{
"source_id": generate_uuid(),
"document": document,
"meta": [{"meta-data": "some-info"}],
}
)
return context
context = format_document(documents)
cite_item = CiteItem(answer=answer, context=context)
## Initialize the Inference
inference = Inference(spacy_model="sm", embedding_model="md")
## Get citation and other information
output = inference(cite_item)
print("------ Citation ------")
print(output.citation)
print("------ Hallucination ------")
print(output.hallucination)
print("------ Missing Entities ------")
print(output.missing)
Output Explanation
print(output.citation)
[
{
"answer_sentences": "Elon Musk's net worth is estimated to be US$241 billion as of August 2024.",
"cite_document": [
{
"document": "people in the world; as of August 2024[update], Forbes estimates his net worth to be US$241 billion.[3]",
"source_id": "23d1f1f0-2afa-4749-8639-78ec685fd837",
"entity": [
{
"word": "US$241 billion",
"entity_name": "MONEY"
},
{
"word": "August 2024",
"entity_name": "DATE"
}
],
"meta": [
{
"url": "https://www.forbes.com/profile/elon-musk/",
"chunk_id": "1eab8dd1ffa92906f7fc839862871ca5"
}
]
}
]
}
]
Key | Description | Example |
---|---|---|
answer_sentences |
Textual information or sentences extracted as answers or relevant information related to the citation. | "Elon Musk's net worth is estimated to be US$241 billion as of August 2024." |
cite_document |
List of source documents used in the citation. Each document contains: | |
- document : Text from the source document. |
"people in the world; as of August 2024[update], Forbes estimates his net worth to be US$241 billion.[3]" |
|
- source_id : Unique identifier for the source document. |
"6874d990-fedc-42bd-b0be-730bcdd59d26" |
|
- entity : List of recognized entities in the document. Each entity contains: |
||
- word : Recognized word or phrase. |
"US$241 billion" |
|
- entity_name Type of the entity (e.g., MONEY , DATE ). |
"MONEY" |
|
- meta Metadata about the document: |
[] |
print(output.hallucination)
False
Key | Description | Example |
---|---|---|
hallucination |
Indicates if the output contains hallucinated information. | false |
print(output.missing)
[]
Key | Description | Example |
---|---|---|
missing |
List of entities expected but not found. | ["$100 USD"] |
Installation
From PyPI:
pip install rag-citation
From Source:
- Clone the repository:
git clone https://github.com/your-username/rag-citation.git cd rag-citation
- Install the dependencies:
pip install -r requirements.txt
Configuration
The Inference
class can be configured with different models and settings:
-
spacy_model
: The spaCy model used for named entity recognition (default:"en_core_web_sm"
). To use different models, pass:"sm"
foren_core_web_sm
"md"
foren_core_web_md
"lg"
foren_core_web_lg
You can download and install spaCy models here.
-
embedding_model
: The sentence embedding model from the SentenceTransformers library used for semantic similarity (default:"all-mpnet-base-v2"
). To use different models, pass:"sm"
foravsolatorio/GIST-small-Embedding-v0
"md"
foravsolatorio/GIST-Embedding-v0
"lg"
foravsolatorio/GIST-large-Embedding-v0
Install SentenceTransformers with:pip install -U sentence-transformers
You can explore the models on Hugging Face.
-
therhold_value
: The similarity threshold value for semantic matching (current default:0.88
). You can adjust this value as needed.
Contributing
We welcome contributions! Here’s how you can help:
- Report Bugs: Submit issues on GitHub.
- Suggest Features: Open an issue with your ideas.
- Code Contributions: Fork, make changes, and submit a pull request.
- Documentation: Update and enhance our docs.
License
This project is licensed under the MIT License.
Acknowledgements
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rag_citation-0.0.5.tar.gz
.
File metadata
- Download URL: rag_citation-0.0.5.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf95d81caa392c0581fb239c2651e183ff0c507c7c46212a37591a8e9fe63bd1 |
|
MD5 | 6a98189fdb0a83a5f31ebc7a94164ba3 |
|
BLAKE2b-256 | 99be491d67c1705f43b91f0f9ecab75149ea1dbf43f3a642b4ba75a6ff36aae4 |
File details
Details for the file rag_citation-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: rag_citation-0.0.5-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 089db0540c96dfb4ccd3d041631a9c0c6b9fbc6fa4ca61d1e0f4eeb1e5ddf6b3 |
|
MD5 | 2093d7f8b67dd17a6581990f098aeacc |
|
BLAKE2b-256 | 3240cdbed929060d9fd8087f39f674cad9f5ddaa5a863914402bd97208e5c978 |