Unofficial Claude2 API supporting direct HTTP chat creation/deletion/retrieval, message attachments and auto session gathering using Firefox with geckodriver.
Project description
unofficial-claude2-api
Claude2 unofficial API supporting direct HTTP chat creation/deletion/retrieval, message attachments and auto session gathering using Firefox with geckodriver.
How to install
pip install unofficial-claude2-api
Uninstallation
pip uninstall unofficial-claude2-api
Requirements
These requirements are needed to auto retrieve session cookie and UserAgent using selenium
-
Firefox installed, and with at least one profile logged into Claude.
-
geckodriver installed inside a folder registered in PATH environment variable.
Example Usage
from claude2_api.client import (
ClaudeAPIClient,
get_session_data,
MessageRateLimitHit,
)
import time
# Delay period between subsequent requests
BACKOFF_DELAY_SECONDS = 60
TEXT_FILEPATH = "test.txt"
PDF_FILEPATH = "test.pdf"
# This function will automatically retrieve a SessionData instance using selenium
# Omitting profile argument will use default Firefox profile
data = get_session_data()
# Initialize a client instance using a session
client = ClaudeAPIClient(data)
# Create a new chat and cache the chat_id
chat_id = client.create_chat()
try:
# Used for textual file attachment
answer = client.send_text_message(
chat_id, "Hello!", attachment_path=TEXT_FILEPATH, timeout=240
)
print(answer)
except MessageRateLimitHit as e:
print(f"\nMessage limit hit, resets at {e.resetDate}")
print(f"\n{e.sleep_sec} seconds left until -> {e.resetTimestamp}")
quit()
finally:
# Perform chat deletion for cleanup
client.delete_chat(chat_id)
# Delay to avoid possible rate limit errors
time.sleep(BACKOFF_DELAY_SECONDS)
# Create another chat for another message
chat_id = client.create_chat()
try:
# This is used for other attachments file types
answer = client.send_file_message(
chat_id, "Hello!", attachment_path=PDF_FILEPATH, timeout=240
)
print(answer)
except MessageRateLimitHit as e:
print(f"\nMessage limit hit, resets at {e.resetDate}")
print(f"\n{e.sleep_sec} seconds left until -> {e.resetTimestamp}")
quit()
finally:
client.delete_chat(chat_id)
# Get a list of all chats ids
all_chat_ids = client.get_all_chat_ids()
# Delete all chats
for chat in all_chat_ids:
client.delete_chat(chat)
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
Close
Hashes for unofficial-claude2-api-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e712bbf399f3856e2e77692c554c90549f89b19c18c37c97a597ef9e9e7f34fe |
|
MD5 | 64bd69dffd8d2a5f6ece8d027893212b |
|
BLAKE2b-256 | 4def6c34624b6d4891c15a2a039fae822a76bb3d26f95bfd2ef0619a34ca7fcd |
Close
Hashes for unofficial_claude2_api-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a0947ea122fb05793ad32614db17f6bc40e0ae335fe08fccc20e22105c88ab0 |
|
MD5 | 5195a8a4a2b0225cbce9114ee2a887a0 |
|
BLAKE2b-256 | dc3cdeb4c79417c95019df8b92c115b1fec88167e0527d87fa617da975e6dc40 |