|
| 1 | +# LLMSession |
| 2 | + |
| 3 | +[](LICENSE) |
| 4 | +[](https://pypi.org/project/llm-session/) |
| 5 | +[](https://www.npmjs.com/package/llm-session) |
| 6 | + |
| 7 | +A zero-configuration tool to automate interactions with web-based LLM providers (currently ChatGPT). It handles authentication, session persistence, and chained prompt execution programmatically. |
| 8 | + |
| 9 | +## Features |
| 10 | +- **Zero-Config Setup**: Automatically handles browser binaries via Playwright. |
| 11 | +- **Session Persistence**: Reuses cookies/storage for subsequent runs (no need to login every time). |
| 12 | +- **Cross-Language Sharing**: Sessions are shared between Python and Node.js environments automatically. |
| 13 | +- **Smart OTP Handling**: Supports non-blocking callbacks for 2FA/OTP challenges. |
| 14 | +- **Resilient**: Allows custom CSS selectors to adapt to UI changes without updating the library. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | +You must set the following environment variables, or pass them directly to the constructor: |
| 18 | +- `CHATGPT_EMAIL` |
| 19 | +- `CHATGPT_PASSWORD` |
| 20 | +- `CHATGPT_GOOGLE_LOGIN` (Optional: set to "true" if using Google Auth. *Note: Google Auth is experimental and may require manual intervention.*) |
| 21 | + |
| 22 | +## Disclaimer |
| 23 | +> [!WARNING] |
| 24 | +> **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. |
| 25 | +
|
| 26 | +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. |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Python Usage |
| 31 | + |
| 32 | +### Installation |
| 33 | +```bash |
| 34 | +pip install llm-session |
| 35 | +``` |
| 36 | + |
| 37 | +### Quick Start |
| 38 | +```python |
| 39 | +import logging |
| 40 | +from llm_session import Automator |
| 41 | + |
| 42 | +# 1. Configure Standard Logging |
| 43 | +logging.basicConfig(level=logging.INFO) |
| 44 | + |
| 45 | +# 2. Define OTP Callback (Optional, but recommended for headless envs) |
| 46 | +def my_otp_handler(): |
| 47 | + # In production, you might fetch this from an email API |
| 48 | + return input("Enter OTP Code sent to email: ") |
| 49 | + |
| 50 | +# 3. Initialize |
| 51 | +bot = Automator( |
| 52 | + provider="chatgpt", |
| 53 | + headless=False, # Set to True for production (CURRENTLY ONLY WORKS WITH headless=False) |
| 54 | + credentials={ |
| 55 | + "email": "your_email@example.com", |
| 56 | + "password": "your_password", |
| 57 | + "method": "email" # or "google" |
| 58 | + }, |
| 59 | + on_otp_required=my_otp_handler |
| 60 | +) |
| 61 | + |
| 62 | +# 4. Single Prompt |
| 63 | +print(bot.process_prompt("Hello, world!")) |
| 64 | + |
| 65 | +# 5. Chained Prompt (Inject previous response) |
| 66 | +chain = [ |
| 67 | + "Write a haiku about Python.", |
| 68 | + "Translate this haiku to Spanish: {{previous}}" |
| 69 | +] |
| 70 | +responses = bot.process_chain(chain) |
| 71 | +print(responses) |
| 72 | + |
| 73 | +bot.close() |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Node.js Usage |
| 79 | + |
| 80 | +### Installation |
| 81 | +```bash |
| 82 | +npm install llm-session |
| 83 | +``` |
| 84 | + |
| 85 | +### Quick Start |
| 86 | +```typescript |
| 87 | +import { Automator } from 'llm-session'; |
| 88 | + |
| 89 | +async function main() { |
| 90 | + // 1. Define OTP Callback |
| 91 | + const onOtpRequired = async () => { |
| 92 | + console.log("Please check your email for code."); |
| 93 | + // Return code from your logic here |
| 94 | + return "123456"; |
| 95 | + }; |
| 96 | + |
| 97 | + // 2. Initialize |
| 98 | + const bot = new Automator( |
| 99 | + "chatgpt", |
| 100 | + false, // headless |
| 101 | + { |
| 102 | + email: "your_email@example.com", |
| 103 | + password: "your_password" |
| 104 | + }, |
| 105 | + undefined, // sessionPath (optional) |
| 106 | + { |
| 107 | + onOtpRequired: onOtpRequired, |
| 108 | + // Optional: Inject your own logger (e.g., Winston, Pino) |
| 109 | + // logger: myLoggerInstance |
| 110 | + } |
| 111 | + ); |
| 112 | + |
| 113 | + try { |
| 114 | + await bot.init(); |
| 115 | + |
| 116 | + const response = await bot.processPrompt("Hello from Node.js!"); |
| 117 | + console.log(response); |
| 118 | + |
| 119 | + } finally { |
| 120 | + await bot.close(); |
| 121 | + } |
| 122 | +} |
| 123 | +main(); |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Advanced Configuration (Resilience) |
| 129 | + |
| 130 | +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. |
| 131 | + |
| 132 | +**Python:** |
| 133 | +```python |
| 134 | +bot = Automator( |
| 135 | + provider="chatgpt", |
| 136 | + config={ |
| 137 | + "selectors": { |
| 138 | + "textarea": "#new-prompt-id", |
| 139 | + "send_btn": ".new-send-button-class", |
| 140 | + "assistant_msg": ".new-message-wrapper" |
| 141 | + } |
| 142 | + } |
| 143 | +) |
| 144 | +``` |
| 145 | + |
| 146 | +**Node.js:** |
| 147 | +```typescript |
| 148 | +const bot = new Automator("chatgpt", true, undefined, undefined, { |
| 149 | + selectors: { |
| 150 | + textarea: "#new-prompt-id", |
| 151 | + send_btn: ".new-send-button-class" |
| 152 | + } |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +## Session Management |
| 157 | +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). |
| 158 | + |
| 159 | +* **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). |
| 160 | +* **Persistence:** Sessions persist across reboots. |
| 161 | + |
| 162 | +## Contributing |
| 163 | +We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to build the project locally. |
| 164 | + |
| 165 | +## License |
| 166 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments