diff --git a/README.md b/README.md index e769c6c..70dc6b7 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ - [Retrieving Chat History](#retrieving-chat-history) - [Faster Loading](#faster-loading-avoiding-selenium) - [Proxies](#proxies) - - [Switching model version](#changing-claude-model) + - [Changing model version](#changing-claude-model) - [Troubleshooting](#troubleshooting) - [Donating](#donating) @@ -23,7 +23,7 @@ While not officially supported by Anthropic, this library can enable interesting It allows for: - Creating chat sessions with Claude and getting chat IDs. -- Sending messages to Claude containing up to 5 attachment files (txt, pdf, csv, etc...) 10 MB each. +- Sending messages to Claude containing up to 5 attachment files (txt, pdf, csv, png, jpeg, etc...) 10 MB each, images are also supported! - Retrieving chat message history, accessing specific chat conversations. - Deleting old chats when they are no longer needed. - Sending requests through proxies. @@ -39,6 +39,8 @@ and more. - Receive thoughtful responses to open-ended prompts and ideas. Claude can brainstorm ideas, expand on concepts, and have philosophical discussions. +- Send images and let Claude analyze them for you. + ## How to install ```shell @@ -256,7 +258,7 @@ __________ ### Changing Claude model -In case you have accounts that are unable to migrate to latest model, you can override the `model_name` string parameter of `ClaudeAPIClient` constructor. +In case you'd like to change the model used, or you do have accounts that are unable to migrate to latest model, you can override the `model_name` string parameter of `ClaudeAPIClient` constructor like so: ```py from claude_api.client import ClaudeAPIClient @@ -265,10 +267,11 @@ from claude_api.session import SessionData session = SessionData(...) # Defaults to None (latest Claude model) -# Can be either claude-2.0 or claude-2.1 client = ClaudeAPIClient(session, model_name="claude-2.0") ``` +You can retrieve the `model_name` strings from the [official API docs](https://docs.anthropic.com/claude/docs/models-overview#model-comparison) + ## TROUBLESHOOTING Some common errors that may arise during the usage of this API: diff --git a/claude_api/client.py b/claude_api/client.py index 1c4e095..a836f34 100644 --- a/claude_api/client.py +++ b/claude_api/client.py @@ -134,10 +134,6 @@ def __init__( Raises `ValueError` in case of failure """ - if model_name is not None and model_name not in {"claude-2.0", "claude-2.1"}: - raise ValueError( - "model_name must be either None or one of 'claude-2.0' or 'claude-2.1' strings" - ) self.model_name: str = model_name self.timeout: float = timeout @@ -235,7 +231,7 @@ def __prepare_file_attachment(self, fpath: str, chat_id: str) -> dict | None: if content_type == "text/plain": return self.__prepare_text_file_attachment(fpath) - url = f"{self.__BASE_URL}/api/convert_document" + url = f"{self.__BASE_URL}/api/{self.__session.organization_id}/upload" headers = { "Host": "claude.ai", @@ -268,7 +264,9 @@ def __prepare_file_attachment(self, fpath: str, chat_id: str) -> dict | None: proxies=self.__get_proxy(), ) if response.status_code == 200: - return response.json() + res = response.json() + if "file_uuid" in res: + return res["file_uuid"] print( f"\n[{response.status_code}] Unable to prepare file attachment -> {fpath}\n" f"\nReason: {response.text}\n\n" @@ -552,14 +550,8 @@ def send_message( attachments = [] if attachment_paths: - attachments = [ - a - for a in [ - self.__prepare_file_attachment(path, chat_id) - for path in attachment_paths - ] - if a - ] + for path in attachment_paths: + attachments.append(self.__prepare_file_attachment(path, chat_id)) url = ( f"{self.__BASE_URL}/api/organizations/" @@ -568,11 +560,20 @@ def send_message( ) payload = { - "attachments": attachments, + "attachments": [], "files": [], "prompt": prompt, "timezone": self.timezone, } + + for a in attachments: + if isinstance(a, dict): + # Text file attachment + payload["attachments"].append(a) + elif isinstance(a, str): + # Other files uploaded + payload["files"].append(a) + if self.model_name is not None: payload["model"] = self.model_name diff --git a/setup.py b/setup.py index 4a8d4f3..199d572 100644 --- a/setup.py +++ b/setup.py @@ -16,7 +16,7 @@ setup( name="unofficial-claude-api", - version="0.3.1", + version="0.3.2", author="st1vms", author_email="stefano.maria.salvatore@gmail.com", description=__DESCRIPTION,