A Python utility for interacting with large language models (LLMs) via web automation
Project description
talktollm
A Python utility for interacting with large language models (LLMs) through a command-line interface. It leverages image recognition to automate interactions with LLM web interfaces, enabling seamless conversations and task execution.
Features
- Command-Line Interaction: Provides a simple and intuitive command-line interface for interacting with LLMs.
- Automated Image Recognition: Employs image recognition techniques (via
optimisewait) to identify and interact with elements on the LLM interface. Includes fallback ifoptimisewaitis not installed. - Multi-LLM Support: Currently supports DeepSeek and Gemini.
- Automated Conversations: Facilitates automated conversations and task execution by simulating user interactions.
- Image Support: Allows sending images (base64 encoded) to the LLM.
- Robust Clipboard Handling: Includes configurable retry mechanisms (default 5 retries) for setting text/images to the clipboard and reading text from the clipboard to handle access errors and timing issues.
- Dynamic Image Path Management: Copies necessary recognition images to a temporary directory, ensuring they are accessible and up-to-date.
- Easy to use: Designed for simple setup and usage.
Core Functionality
The core function is talkto(llm, prompt, imagedata=None, debug=False, tabswitch=True, read_retries=5, read_delay=0.3).
Arguments:
llm(str): The LLM name ('deepseek' or 'gemini').prompt(str): The text prompt.imagedata(list[str] | None): Optional list of base64 encoded image strings (e.g., "data:image/png;base64,...").debug(bool): Enable detailed console output. Defaults toFalse.tabswitch(bool): Switch focus back to the previous window after closing the LLM tab. Defaults toTrue.read_retries(int): Number of attempts to read the final response from the clipboard. Defaults to 5.read_delay(float): Delay in seconds between clipboard read attempts. Defaults to 0.3.
Steps:
- Validates the LLM name.
- Sets up image paths for
optimisewaitusingset_image_path. - Opens the LLM's website in a new browser tab.
- Waits and clicks the message input area using
optimiseWait('message', clicks=2). - If
imagedatais provided:- Iterates through images.
- Sets each image to the clipboard using
set_clipboard_image(with retries). - Pastes the image (
Ctrl+V). - Waits for potential upload (
sleep(7)).
- Sets the
prompttext to the clipboard usingset_clipboard(with retries). - Pastes the prompt (
Ctrl+V). - Waits and clicks the 'run' button using
optimiseWait('run'). - Waits for the response generation, using
optimiseWait('copy')as an indicator that the response is ready and the copy button is visible. - Waits briefly (
sleep(0.5)) afteroptimiseWait('copy')clicks the copy button. - Closes the browser tab (
Ctrl+W). - Switches focus back if
tabswitchisTrue(Alt+Tab). - Attempts to read the LLM's response from the clipboard with retry logic (
read_retries,read_delay). - Returns the retrieved text response, or an empty string if reading fails.
Helper Functions
Clipboard Handling:
set_clipboard(text: str, retries: int = 5, delay: float = 0.2): Sets text to the clipboard, handlingCF_UNICODETEXT. Retries on common access errors (winerror 5or1418).set_clipboard_image(image_data: str, retries: int = 5, delay: float = 0.2): Sets a base64 encoded image to the clipboard (CF_DIBformat). Decodes, converts to BMP, and retries on common access errors.
Image Path Management:
set_image_path(llm: str, debug: bool = False): Orchestrates copying images.copy_images_to_temp(llm: str, debug: bool = False): Copies necessary.pngimages for the specifiedllmfrom the package'simages/<llm>directory to a temporary location (%TEMP%\\talktollm_images\\<llm>). Creates the temporary directory if needed and only copies if the source file is newer or the destination doesn't exist. Sets theoptimisewaitautopath. Includes error handling for missing package resources.
Installation
pip install talktollm
Note: Requires optimisewait for image recognition. Install separately if needed (pip install optimisewait).
Usage
Here are some examples of how to use talktollm.
Example 1: Simple Text Prompt
Send a basic text prompt to Gemini.
import talktollm
prompt_text = "Explain quantum entanglement in simple terms."
response = talktollm.talkto('gemini', prompt_text)
print("--- Simple Gemini Response ---")
print(response)
Example 2: Text Prompt with Debugging
Send a text prompt and enable debugging output to see more details about the process.
import talktollm
prompt_text = "What are the main features of Python 3.12?"
response = talktollm.talkto('deepseek', prompt_text, debug=True)
print("--- DeepSeek Debug Response ---")
print(response)
Example 3: Preparing Image Data
Load an image file, encode it in base64, and format it correctly for the imagedata argument.
import base64
import io
from PIL import Image
# Load your image (replace 'path/to/your/image.png' with the actual path)
try:
with open("path/to/your/image.png", "rb") as image_file:
# Encode to base64
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
# Format as a data URI
image_data_uri = f"data:image/png;base64,{encoded_string}"
print("Image prepared successfully!")
# You can now pass [image_data_uri] to the imagedata parameter
except FileNotFoundError:
print("Error: Image file not found. Please check the path.")
image_data_uri = None
except Exception as e:
print(f"Error processing image: {e}")
image_data_uri = None
# This 'image_data_uri' variable holds the string needed for the next example
Example 4: Text and Image Prompt
Send a text prompt along with a prepared image to Gemini. (Assumes image_data_uri was successfully created in Example 3).
import talktollm
# Assuming image_data_uri is available from the previous example
if image_data_uri:
prompt_text = "Describe the main subject of this image."
response = talktollm.talkto(
'gemini',
prompt_text,
imagedata=[image_data_uri], # Pass the image data as a list
debug=True
)
print("--- Gemini Image Response ---")
print(response)
else:
print("Skipping image example because image data is not available.")
Dependencies
pywin32: For Windows API access (clipboard).pyautogui: For GUI automation (keystrokes, potentially mouse ifoptimisewaitfails).Pillow: For image processing (opening, converting for clipboard).optimisewait(Optional but Recommended): For robust image-based waiting and clicking.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file talktollm-0.3.6.tar.gz.
File metadata
- Download URL: talktollm-0.3.6.tar.gz
- Upload date:
- Size: 32.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1df2a5200f148f6d853c308bc58aa9b65aced76b66a0f7c295841df0941649c8
|
|
| MD5 |
735d379d0eb131c6b8c086e3b1b45898
|
|
| BLAKE2b-256 |
fa46c0d29d643f0d4ae6b9f978930090be1830d23799aff3c1816ec80ed6bf1b
|
File details
Details for the file talktollm-0.3.6-py3-none-any.whl.
File metadata
- Download URL: talktollm-0.3.6-py3-none-any.whl
- Upload date:
- Size: 30.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d587032bf243d2172c171ef960ba81109c7495348a33889b239a778911c6640
|
|
| MD5 |
f4ce3093ab22ad58e741ff5019c43747
|
|
| BLAKE2b-256 |
77a878c43b3ecd55c77b19ec12520f57a2bd184ec943041bfffc07894b038b2d
|