Unofficial Claude API for Python
Overview
The Unofficial Claude API for Python is a versatile tool designed for seamless interaction with Anthropic's Claude Language Learning Model (LLM). This tool emulates the functionality of more established platforms like ChatGPT, providing users with free access to Claude's capabilities directly within Python projects.
What Can It Do?
Even though it's still evolving, the Unofficial Claude API offers a variety of functionalities:
- Manage Organizations and Conversations: Users can retrieve a list of organizations and conversations they are involved in.
- Initiate Conversations: Easily start new conversations.
- Messaging: Send a message and receive responses, facilitating real-time interaction with the model.
- Manage Attachments: Create, send, and manage attachments linked to conversations.
- History Retrieval: Access the message history within a conversation.
- Delete Conversations: Remove existing conversations as needed.
The API operates synchronously, meaning tasks are executed in sequence, one after the other.
Future Enhancements
The project team plans to implement several improvements:
- Incorporating tests to ensure the API's reliability.
- Fixing models other than the current
claude_2
. - Introducing asynchronous capabilities to allow concurrent processing.
- Enhancing caching mechanisms for better efficiency.
- Simplifying error handling and providing clearer error messages to users.
Usage Instructions
Step 1: Installation
Begin by installing the library through pip:
pip install claude-api-py
If issues occur, install directly from the GitHub repository:
pip install git+git://github.com/AshwinPathi/claude-api-py.git
A dependency required is sseclient-py
, details can be found here.
Step 2: Obtain a Session Key
A session key is necessary to start interacting with the bot. To acquire this:
- Log into the Claude website at
https://claude.ai/chats
. - Use inspect element tools to navigate to
Cookies
under theApplication
tab. - Locate the session key (
sessionKey
) and copy it for use in your projects.
Step 3: Engage with the Bot
Following installation and session key retrieval, users can engage with the bot using provided code examples.
Code Examples
Importing Libraries
from claude import claude_client, claude_wrapper
Creating a Client and Wrapper
client = claude_client.ClaudeClient(SESSION_KEY)
organizations = client.get_organizations()
claude_obj = claude_wrapper.ClaudeWrapper(client, organization_uuid=organizations[0]['uuid'])
Starting a Conversation
new_conversation_data = claude_obj.start_new_conversation("New Conversation", "Hi Claude!")
Sending a Message
response = claude_obj.send_message("How are you doing today!", conversation_uuid=conversation_uuid)
Handling Attachments
attachment = claude_obj.get_attachment('example_attachment.txt')
response = claude_obj.send_message("Hi Claude, what does this attachment say?", attachments=[attachment])
Deleting Conversations
claude_obj.delete_conversation(conversation_uuid)
Conversation History
conversation_history = claude_obj.get_conversation_info(conversation_uuid)
Disclaimer
The Unofficial Claude API is intended solely for educational purposes. It is not an official product of Anthropic, and users are responsible for any repercussions, such as account bans. For legitimate access, visit Anthropic's official website.