Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.0 KB
Raw Blame
#!/usr/bin/env python3
"""
Simple API Client Example - No more CSRF complexity!

This shows how easy it is to use the LDR API with the new client.
All the CSRF token handling is done automatically.
"""

from local_deep_research.api import LDRClient, quick_query

# Configuration
USERNAME = "your_username"  # Change this!
PASSWORD = "your_password"  # Change this!


def example_1_simple():
    """Simplest possible usage - one line research."""
    print("=== Example 1: One-liner ===")

    # Just one line to get a research summary!
    summary = quick_query(USERNAME, PASSWORD, "What is machine learning?")
    print(f"Summary: {summary[:200]}...")


def example_2_client():
    """Using the client for multiple operations."""
    print("\n=== Example 2: Client Usage ===")

    # Create client
    client = LDRClient()

    # Login once
    if not client.login(USERNAME, PASSWORD):
        print("Login failed!")
        return

    # Now just use it - no more CSRF hassles!
    try:
        # Do research
        result = client.quick_research("What are neural networks?")
        print("Research complete!")
        print(f"Summary: {result['summary'][:200]}...")
        print(f"Sources found: {len(result.get('sources', []))}")

        # Check settings
        settings = client.get_settings()
        print(
            f"\nYou have {len(settings.get('settings', {}))} settings configured"
        )

        # Get history
        history = client.get_history()
        print(f"You have {len(history)} items in history")

    finally:
        client.logout()


def example_3_context_manager():
    """Using context manager for automatic cleanup."""
    print("\n=== Example 3: Context Manager ===")

    # Automatic login/logout with context manager
    with LDRClient() as client:
        if client.login(USERNAME, PASSWORD):
            # Start research without waiting
            result = client.quick_research(
                "What is quantum computing?", wait_for_result=False
            )
            print(f"Research started with ID: {result['research_id']}")

            # Do other things...
            print("Doing other work while research runs...")

            # Later, get the results
            final_result = client.wait_for_research(result["research_id"])
            print(f"Research complete: {final_result['summary'][:100]}...")


def example_4_batch_research():
    """Running multiple research queries efficiently."""
    print("\n=== Example 4: Batch Research ===")

    questions = [
        "What is DNA?",
        "How do vaccines work?",
        "What causes earthquakes?",
    ]

    with LDRClient() as client:
        if not client.login(USERNAME, PASSWORD):
            print("Login failed!")
            return

        # Start all research tasks
        research_ids = []
        for question in questions:
            result = client.quick_research(question, wait_for_result=False)
            research_ids.append((question, result["research_id"]))
            print(f"Started: {question}")

        print("\nWaiting for all results...")

        # Collect all results
        for question, research_id in research_ids:
            try:
                result = client.wait_for_research(research_id, timeout=120)
                print(f"\n{question}")
                print(f"→ {result['summary'][:150]}...")
            except Exception as e:
                print(f"\n{question}")
                print(f"→ Error: {e}")


if __name__ == "__main__":
    print("LDR Simple Client Examples")
    print("=" * 50)
    print("\nBefore: Complex CSRF handling, HTML parsing, manual polling...")
    print("After: Just login() and quick_research()!")
    print("\nMake sure:")
    print("1. LDR server is running: python -m local_deep_research.web.app")
    print("2. You've updated USERNAME and PASSWORD in this script")
    print("=" * 50)

    # Uncomment the examples you want to run:
    # example_1_simple()
    # example_2_client()
    # example_3_context_manager()
    # example_4_batch_research()

    print("\nUncomment the examples in the script to run them!")