Skip to main content

Chat with PDFs using LangChain In Few Lines of Code

Project description

chat-pdf

Chat with PDF documents using LangChain and OpenAI.

chat-pdf is a lightweight Python library that allows you to load a PDF, ask questions about its content, and optionally retrieve the source passages used to generate the answers.

The library is intentionally simple and pinned to stable LangChain versions to avoid breaking changes.


Features

  • Load and index PDF documents
  • Ask natural language questions
  • Retrieve answers with or without source documents
  • Minimal, function-based API
  • Stable LangChain 1.2.x compatibility
  • Suitable for scripts, notebooks, and APIs

Installation

pip install chat-pdf

Requirements

  • Python 3.10 or higher
  • OpenAI API key

Set your API key as an environment variable or pass it directly in code:

export OPENAI_API_KEY="your-api-key"

Quick Start

1. Load a PDF

from chat_pdf import load_pdf

db = load_pdf(
    "document.pdf",
    api_key="OPENAI_API_KEY"
)

This will:

  • Load the PDF file
  • Split it into chunks
  • Generate embeddings
  • Store them in a Chroma vector database

2. Ask a Question

from chat_pdf import ask_question

answer = ask_question(
    "What is this document about?",
    db=db,
    api_key="OPENAI_API_KEY"
)

print(answer)

3. Ask a Question with Sources (Optional)

from chat_pdf import ask_question_with_sources

result = ask_question_with_sources(
    "What are the termination conditions?",
    db=db,
    api_key="OPENAI_API_KEY"
)

print("Answer:")
print(result["answer"])

print("\nSources:")
for doc in result["sources"]:
    print(doc.metadata)

Returned keys:

  • answer – generated response
  • sources – list of retrieved document chunks

API Reference

load_pdf

load_pdf(
    pdf_path: str,
    api_key: str,
    chunk_size: int = 1000,
    chunk_overlap: int = 200,
    persist_directory: str | None = None
)

ask_question

ask_question(
    question: str,
    db,
    api_key: str,
    model_name: str = "gpt-3.5-turbo",
    search_kwargs: dict | None = None,
    system_prompt: str | None = None
)

Returns a string answer.


ask_question_with_sources

ask_question_with_sources(
    question: str,
    db,
    api_key: str,
    model_name: str = "gpt-3.5-turbo",
    search_kwargs: dict | None = None,
    system_prompt: str | None = None
)

Returns a dictionary:

{
    "answer": str,
    "sources": list
}

Version Compatibility

This package is pinned for stability.

chat-pdf LangChain
1.2.0.0 1.2.x

Upgrading LangChain independently is not recommended.


Design Philosophy

  • Simple over clever
  • Stable over experimental
  • Explicit over magical

This library is designed to be easy to read, debug, and extend.


License

MIT License

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

easy_rag_pdf-0.0.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

easy_rag_pdf-0.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file easy_rag_pdf-0.0.1.tar.gz.

File metadata

  • Download URL: easy_rag_pdf-0.0.1.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for easy_rag_pdf-0.0.1.tar.gz
Algorithm Hash digest
SHA256 60a8aa05ca287a15d9bc0f5a41d4c8e0503b77b06f89e139610ce1c49ddaba27
MD5 81f181741591d6b839fc17884d00aa78
BLAKE2b-256 638191ce0cdd8040a54131340f49d2197441de44a636b8959a5d46460c131e9f

See more details on using hashes here.

File details

Details for the file easy_rag_pdf-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: easy_rag_pdf-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for easy_rag_pdf-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1ac81299f150b66b14a9047cb4a482d5be3676b8d86287c8170023fa92719128
MD5 20aafd9bbf19b2611b0a1081051507ad
BLAKE2b-256 2fd85f4f100d2091ff433c2b797f8a5f9d5e143a34060a1e09cbd0f46ac5abe1

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