Skip to content

Commit

Permalink
Merge pull request #22 from st1vms/dev-0.3.2
Browse files Browse the repository at this point in the history
dev-0.3.2 merge to main
  • Loading branch information
st1vms authored Apr 4, 2024
2 parents de88d8e + d5a6702 commit 422581d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
31 changes: 16 additions & 15 deletions claude_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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/"
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name="unofficial-claude-api",
version="0.3.1",
version="0.3.2",
author="st1vms",
author_email="[email protected]",
description=__DESCRIPTION,
Expand Down

0 comments on commit 422581d

Please sign in to comment.