Skip to main content

A zero-configuration tool to automate web-based LLM interactions.

Project description

LLMSession

License: MIT

A zero-configuration tool to automate interactions with web-based LLM providers (currently ChatGPT). It handles authentication, session persistence, and chained prompt execution programmatically.

Features

  • Zero-Config Setup: Automatically handles browser binaries via Playwright.
  • Session Persistence: Reuses cookies/storage for subsequent runs (no need to login every time).
  • Cross-Language Sharing: Sessions are shared between Python and Node.js environments automatically.
  • Smart OTP Handling: Supports non-blocking callbacks for 2FA/OTP challenges.
  • Resilient: Allows custom CSS selectors to adapt to UI changes without updating the library.

Prerequisites

You must set the following environment variables, or pass them directly to the constructor:

  • CHATGPT_EMAIL
  • CHATGPT_PASSWORD
  • CHATGPT_GOOGLE_LOGIN (Optional: set to "true" if using Google Auth. Note: Google Auth is experimental and may require manual intervention.)

Disclaimer

[!WARNING] Cloudflare/Bot Detection: Automated interactions with ChatGPT are subject to high-security bot detection (Cloudflare). This library uses standard browser automation and may be blocked. For production reliability, please use the Official OpenAI API.

This tool automates a third-party web interface. It is subject to breakage if the target website changes its DOM structure. Use responsibly and in accordance with the provider's Terms of Service.


Python Usage

Installation

pip install llm-session

Quick Start

import logging
from llm_session import Automator

# 1. Configure Standard Logging
logging.basicConfig(level=logging.INFO)

# 2. Define OTP Callback (Optional, but recommended for headless envs)
def my_otp_handler():
    # In production, you might fetch this from an email API
    return input("Enter OTP Code sent to email: ")

# 3. Initialize
bot = Automator(
    provider="chatgpt",
    headless=False,  # Set to True for production (CURRENTLY ONLY WORKS WITH headless=False)
    credentials={
        "email": "your_email@example.com", 
        "password": "your_password",
        "method": "email" # or "google"
    },
    on_otp_required=my_otp_handler
)

# 4. Single Prompt
print(bot.process_prompt("Hello, world!"))

# 5. Chained Prompt (Inject previous response)
chain = [
    "Write a haiku about Python.",
    "Translate this haiku to Spanish: {{previous}}"
]
responses = bot.process_chain(chain)
print(responses)

bot.close()

Node.js Usage

Installation

npm install llm-session

Quick Start

import { Automator } from 'llm-session';

async function main() {
    // 1. Define OTP Callback
    const onOtpRequired = async () => {
        console.log("Please check your email for code.");
        // Return code from your logic here
        return "123456"; 
    };

    // 2. Initialize
    const bot = new Automator(
        "chatgpt", 
        false, // headless
        {
            email: "your_email@example.com",
            password: "your_password"
        },
        undefined, // sessionPath (optional)
        {
            onOtpRequired: onOtpRequired,
            // Optional: Inject your own logger (e.g., Winston, Pino)
            // logger: myLoggerInstance 
        }
    );
    
    try {
        await bot.init();

        const response = await bot.processPrompt("Hello from Node.js!");
        console.log(response);

    } finally {
        await bot.close();
    }
}
main();

Advanced Configuration (Resilience)

Websites change their layout often. If ChatGPT updates their CSS class names, you don't need to wait for a package update. You can inject your own selectors during initialization.

Python:

bot = Automator(
    provider="chatgpt", 
    config={
        "selectors": {
            "textarea": "#new-prompt-id",
            "send_btn": ".new-send-button-class",
            "assistant_msg": ".new-message-wrapper"
        }
    }
)

Node.js:

const bot = new Automator("chatgpt", true, undefined, undefined, {
    selectors: {
        textarea: "#new-prompt-id",
        send_btn: ".new-send-button-class"
    }
});

Session Management

This library stores browser cookies and local storage in your OS's standard user data directory (e.g., %LOCALAPPDATA%/LLMSession on Windows, ~/.local/share/LLMSession on Linux).

  • Cross-Language: If you login using the Python script, the Node.js script will automatically detect the existing session and skip login (and vice-versa).
  • Persistence: Sessions persist across reboots.

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines on how to build the project locally.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

llm_session-0.1.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

llm_session-0.1.2-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file llm_session-0.1.2.tar.gz.

File metadata

  • Download URL: llm_session-0.1.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for llm_session-0.1.2.tar.gz
Algorithm Hash digest
SHA256 98b9fe441abec4208b0333c40c570a8c3abea36d1f7be5f652fadc3dca7dc721
MD5 ad29cb261ab8e977f9ae82b15abef3d7
BLAKE2b-256 ba8d19714e5cdcc73801b251693386e672362c6fd52bd578838d3c02c4e4a27a

See more details on using hashes here.

File details

Details for the file llm_session-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: llm_session-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for llm_session-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ebdc43e5b642143bac30a5fb0c55244e93dadf6a74e247c33ecd8c49862baafa
MD5 73d7375112eb4a366f13c5d045505806
BLAKE2b-256 267ea77548fd4b50406b0559ee0629e4175403b0841f80aa5ce3b0154a3ad0dc

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