Skip to main content

Semantic Search Without LLMs

This package provides an AI-powered document semantic search system that doesn't rely on large language models (LLMs). It allows you to process documents, web content, and scanned images, and then perform efficient semantic searches using cosine similarity.

Installation

Install the package using pip:

pip install semantic-search-no-llms

System Architecture

Here's a high-level overview of the system architecture:

graph TD
    A[User] -->|Upload Document/Scan Image/Enter URL| B[Streamlit UI]
    B -->|Process Document| C[DataLoader]
    B -->|Crawl Web| H[WebCrawler]
    C -->|Chunk Text| D[Text Splitter]
    H -->|Extract Content| D
    D -->|Generate Embeddings| E[HuggingFace Embeddings]
    E -->|Store Vectors| F[FAISS Vector Store]
    A -->|Enter Query| B
    B -->|Retrieve Similar Chunks| F
    F -->|Calculate Cosine Similarity| G[Semantic Search]
    G -->|Return Results| B
    B -->|Display Results| A

Usage

Document Processing

Load a Document

To load a document (PDF, DOCX, TXT, XLS, XLSX):

from semantic_search_no_llms.data_loader import DataLoader

# Load a document
data_loader = DataLoader("path/to/document.pdf")
documents = data_loader.load_document()

Chunk the Document

To chunk the document into smaller pieces:

# Chunk the document into smaller pieces
chunks = data_loader.chunk_document(documents, chunk_size=1024, chunk_overlap=80)

Process the Chunks

This step creates embeddings for the document chunks and builds a FAISS index for efficient similarity search:

from semantic_search_no_llms.utils import process_chunks

process_chunks(chunks)

Web Crawling

Crawl a Website

To crawl a website and fetch its content:

from semantic_search_no_llms.web_crawler import WebCrawler

# Crawl a website
crawler = WebCrawler("https://www.example.com")
content = crawler.fetch_content()

Process the Crawled Content

To process the content fetched from the website:

from langchain.schema import Document as LangChainDocument

document = LangChainDocument(page_content=content, metadata={"source": "https://www.example.com"})
chunks = data_loader.chunk_document([document], chunk_size=1024, chunk_overlap=80)
process_chunks(chunks)

To perform a semantic search using the FAISS index and retrieve the top matching document chunks:

from semantic_search_no_llms.utils import cosine_similarity, load_embeddings
import numpy as np

# Load the embeddings
embeddings = load_embeddings()

# Define a query
query = "What is the main topic of the document?"
query_embedding = embeddings.embed_query(query)

# Reconstruct the document embeddings
from semantic_search_no_llms.utils import FAISS
vectorstore = FAISS.from_documents(documents=chunks, embedding=embeddings)
document_embeddings = vectorstore.index.reconstruct_n(0, vectorstore.index.ntotal)

# Calculate similarities and get the top results
similarities = [cosine_similarity(query_embedding, doc_embedding) for doc_embedding in document_embeddings]
top_k_indices = np.argsort(similarities)[-5:][::-1]

for i, idx in enumerate(top_k_indices):
    doc = vectorstore.docstore.search(vectorstore.index_to_docstore_id[idx])
    print(f"Match {i+1} - Similarity: {similarities[idx]:.4f}")
    print(doc.page_content)

This example shows how to load the embeddings, perform a semantic search using the FAISS index, and retrieve the top matching document chunks.

Customization

You can customize the behavior of the document processing by adjusting the chunk_size and chunk_overlap parameters when calling the chunk_document() method. Larger chunk sizes provide more context, while smaller chunks can improve search precision.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Metadata

Release files for semantic-search-nollms 0.3

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

Source distribution (sdist)

Source distribution for semantic-search-nollms 0.3
File Size Uploaded Provenance Actions
semantic_search_nollms-0.3.tar.gz 5.7 kB Without provenance Details for semantic_search_nollms-0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for semantic-search-nollms 0.3
File Interpreter ABI Platform Actions
semantic_search_nollms-0.3-py3-none-any.whl Python 3 none any Details for semantic_search_nollms-0.3-py3-none-any.whl

Total release size: 12.9 kB

Release files / semantic_search_nollms-0.3.tar.gz

Download URL semantic_search_nollms-0.3.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
29028553d7c1e1ab86d8bb1c75d93966a34d69e54b06d1d48a2bd401bdb29e6e
BLAKE2b-256 checksum
How to use checksums
382477095efc80000eedec55a97a00caeeedb297933ed76f5ded706e543fd439
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / semantic_search_nollms-0.3-py3-none-any.whl

Download URL semantic_search_nollms-0.3-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69ec5150d9734042e76f169bea9c7e04639d2bc0e1346bd84ac727e53a9b13f1
BLAKE2b-256 checksum
How to use checksums
2e487601d0c2b546ac0f75561fede486c9e40aaa54662672bb96e9eb47da2dec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for semantic-search-nollms 0.3

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3 This release

2 release files No provenance

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