Skip to main content

localrag

localrag is a Python package enabling users to "chat" with their documents using a local Retrieval Augmented Generation (RAG) approach, without needing an external Large Language Model (LLM) provider.

It allows for quick, local, and easy interactions with text data, extracting and generating responses based on the content.

Features

  • Local Processing: Runs entirely on your local machine - no need to send data externally.
  • Customizable: Easy to set up with default models or specify your own.
  • Versatile: Use it for a variety of applications, from automated Q&A systems to data mining. You add files, folders or websites to the index!

Prerequisites

Before you install and start using localrag, make sure you meet the following requirements:

Ollama for Local Inference

localrag uses Ollama for local inference, particularly beneficial for macOS users. Ollama allows for easy model serving and inference. To set up Ollama:

Installation

To install localrag, simply use pip:

pip install localrag

Quick Start

Here's a quick example of how you can use localrag to chat with your documents:

Here is an example in test.txt in the docs folder:

I have a dog
import localrag
my_local_rag = localrag.init()
# Add docs
my_local_rag.add_to_index("./docs")
# Chat with docs
response = my_local_rag.chat("What type of pet do I have?")
print(response.answer)
print(response.context)
# Based on the context you provided, I can determine that you have a dog. Therefore, the type of pet you have is "dog."
# [Document(page_content='I have a dog', metadata={'source': 'docs/test.txt'})]

Website Example

import localrag
my_local_rag = localrag.init()
my_local_rag.add_to_index("https://github.com/banjtheman/localrag")
response = my_local_rag.chat("What is localrag?")
print(response.answer)
# Based on the context provided in the GitHub repository page for "banjtheman/localrag", localrag is a chat application that allows users to communicate with their documents locally...

More examples in the tests folder.

localrag config options

Here is how you can configure localrag:

import localrag
my_local_rag = localrag.init(
    llm_model="llama2", # Can choose from ollama models: https://ollama.ai/library
    embedding_model="BAAI/bge-small-en-v1.5", # Can choose variations of https://huggingface.co/BAAI/bge-large-en-v1.5, top 5 embedding model https://huggingface.co/spaces/mteb/leaderboard
    device="mps", # can set device to mps, cpu or cuda:X
    index_location="localrag_index", # Location of the vectorstore
    system_prompt="You are Duck. Start each response with Quack.", # Custom system prompt
)
my_local_rag.add_to_index("./docs")

# can change the URL of the ollama server with my_local_rag.llm.base_url = "http://ollama:11434"

localrag custom everything

You can provide the foloowing custom langchain objects:

  • llm
  • vector databases (must also add an "add docs" function)
  • embedding function
from langchain_community.embeddings.sentence_transformer import (
    SentenceTransformerEmbeddings,
)
from langchain_community.vectorstores import Chroma
from langchain_openai import ChatOpenAI

import localrag

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)

# create the open-source embedding function
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

# Load Chroma Vectordb
chroma_vectordb = Chroma(
    persist_directory="./chroma_db", embedding_function=embedding_function
)

# Custom embed text function
# the Vectorstore and the documents will be passed in
def chroma_add_docs(vectorstore, texts):
    print(texts)
    vectorstore.add_documents(texts)
    print("Added to vector store")

# Set up with all the necessary configurations
my_local_rag = localrag.custom_init(
    llm=llm,
    embedding_model=embedding_function,
    vectorstore=chroma_vectordb,
    custom_embed_text_func=chroma_embed_text,
)

# Add a file
my_local_rag.add_to_index("pizza.txt")
response = my_local_rag.chat("What type of food do I like?")
print(response.answer)
print(response.context)

License

This library is licensed under the Apache 2.0 License. See the LICENSE file.

Metadata

Release files for localrag 0.1.52

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

Source distribution (sdist)

Source distribution for localrag 0.1.52
File Size Uploaded Provenance
localrag-0.1.52.tar.gz 11.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for localrag 0.1.52
File Interpreter ABI Platform
localrag-0.1.52-py3-none-any.whl Python 3 none any Details

Total release size: 23.2 kB

Release files / localrag-0.1.52.tar.gz

Download URL localrag-0.1.52.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e696c6a6216727d8604420b7b5f9aa2799ba55f04e537a12ae5de8acb4384e1b
BLAKE2b-256 checksum
How to use checksums
ab8f6f856c3871b109ecb08271e6f2c28f9b6b9c6b918a94ca3b82987472791d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / localrag-0.1.52-py3-none-any.whl

Download URL localrag-0.1.52-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e117c1e0626e9522eca399b1d84186674b04aa0859fa89682a13c2d9c0630264
BLAKE2b-256 checksum
How to use checksums
7687e43f0fd18535dc41093d4fded80a2a07043ee5fe0244de6aa698fc96a314
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for localrag 0.1.52

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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