claude-api-py


Nameclaude-api-py JSON
Version 0.0.5 PyPI version JSON
download
home_pagehttps://github.com/AshwinPathi/claude-api-py
SummaryUnofficial Anthropic Claude API for Python3.
upload_time2024-03-06 09:18:35
maintainer
docs_urlNone
author
requires_python
licenseMIT
keywords llm claude api gpt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Unofficial Claude API For Python

The UNOFFICIAL free API for Anthropic's Claude LLM.

## Background

Claude is [Anthropic's](https://www.anthropic.com/) LLM app (similar to ChatGPT). This library allows you to use the API (for free) and interact with it in your python projects.

## Implemented actions:
The Unofficial Claude API is under active development. The following endpoints are usable in some capacity:

- Getting organizations you're in
- Getting conversations you're in
- Starting a conversation
- Sending a message and receiving a response
- Delete a conversation
- Create an attachment from a file
- Send attachments
- getting message history

Note that the api is __**synchronous**__.

## TODO features
- Tests
- Fixing models OTHER than `claude_2`
- asynchronous mode
- Better caching
- cleaner errors and passing these up to users


This project is under active development and is extremely unstable, so there are no guarantees it will work for you. If you find a bug or you think it should work in a scenario where it doesn't, file an issue.


## Usage

### Step 1
Install the library using the following:
```
pip install claude-api-py
```

If that doesn't work, you can install directly from this github repository:

```
pip install git+git://github.com/AshwinPathi/claude-api-py.git
```

There is one requirement as of now:
- `sseclient-py` [link here](https://github.com/mpetazzoni/sseclient)


### Step 2
Get a `sessionKey` from the Claude website. You will need this to start the bot. Ideally also have a user agent of the computer you use to access claude.

You can get this information by logging into `https://claude.ai/chats` and doing the following:

1. open inspect element (f12 on chrome)
2. On the top bar, go to the `Application` tab.
3. Under `Storage`, go to `Cookies`.
4. look for a cookie called `https://claude.ai`, click it.
5. click the `sessionKey` field, and copy the session key down. It should begin with `sk-ant-sid01...`


### Step 3
Use the bot. You can see an example at `example.py`.

### Examples:

#### Importing the necessary libraries
```py
# The ClaudeClient is the raw API that gives you access to all organization and conversation level API calls
# with a simple python interface. However, you have to pass organization_uuid and conversation_uuid everywhere, so
# its not ideal if you want a simple to use API.
from claude import claude_client

# The ClaudeWrapper takes in a claude client instance and allows you to use a single organization and conversation
# context. This allows you to use the API more ergonomically.
from claude import claude_wrapper
```

#### Create the client and wrapper
```py
client = claude_client.ClaudeClient(SESSION_KEY)

organizations = client.get_organizations()
# You can omit passing in the organization uuid and the wrapper will assume
# you will use the first organization instead.
claude_obj = claude_wrapper.ClaudeWrapper(client, organization_uuid=organizations[0]['uuid'])
```

#### Starting a new conversation
```py
new_conversation_data = claude_obj.start_new_conversation("New Conversation", "Hi Claude!")
conversation_uuid = new_conversation_data['uuid']
# You can get the response from the initial message you sent with:
initial_response = new_conversation_data['response']
# You can get the title of the new chat you created with this:
chat_title = new_conversation_data['title']
```

#### Send a message (passing in the client uuid)
```py
conversation_uuid = claude_obj.get_conversations()[0]['uuid']
response = claude_obj.send_message("How are you doing today!", conversation_uuid=conversation_uuid)
```

#### Setting a conversation context and sending a message
```py
conversation_uuid = claude_obj.get_conversations()[0]['uuid']
# This is so you don't have to constantly pass in conversation uuid on every call that requires it.
# anywhere that has an argument conversation_uuid=X can be omitted if you set the conversation context.
claude_obj.set_conversation_context(conversation_uuid)

response = claude_obj.send_message("How are you doing today!")
response = claude_obj.send_message("Who won the league of legends worlds 2022 finals?")
```

#### Sending an attachment
```py
# This generates an attachment in the right format
attachment = claude_obj.get_attachment('example_attachment.txt')
response = claude_obj.send_message("Hi Claude, what does this attachment say?", attachments=[attachment],
                                    conversation_uuid = conversation_uuid)
```

#### Deleting a conversation
```py
deleted = claude_obj.delete_conversation(conversation_uuid)
```

#### Deleting all conversations in an organization
```py
failed_deletions = claude_obj.delete_all_conversations()
assert len(failed_deletions) == 0
```

#### Renaming a conversation
```py
conversation = claude_obj.rename_conversation("New name", conversation_uuid = conversation_uuid)
```

#### Get conversation history
```py
conversation_history = claude_obj.get_conversation_info(conversation_uuid = conversation_uuid)
```


## Disclaimer
This library is for purely educational purposes and is UNOFFICIAL. I am not responsible if your account gets banned. If you would like to use the actual API, go to [anthropic website](https://docs.anthropic.com/claude/docs).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/AshwinPathi/claude-api-py",
    "name": "claude-api-py",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "llm,claude,api,gpt",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/20/b3/3e30c9593f2f661bcd79284f763148bf39e4c3bad84cd712d63601b6374e/claude-api-py-0.0.5.tar.gz",
    "platform": null,
    "description": "# Unofficial Claude API For Python\n\nThe UNOFFICIAL free API for Anthropic's Claude LLM.\n\n## Background\n\nClaude is [Anthropic's](https://www.anthropic.com/) LLM app (similar to ChatGPT). This library allows you to use the API (for free) and interact with it in your python projects.\n\n## Implemented actions:\nThe Unofficial Claude API is under active development. The following endpoints are usable in some capacity:\n\n- Getting organizations you're in\n- Getting conversations you're in\n- Starting a conversation\n- Sending a message and receiving a response\n- Delete a conversation\n- Create an attachment from a file\n- Send attachments\n- getting message history\n\nNote that the api is __**synchronous**__.\n\n## TODO features\n- Tests\n- Fixing models OTHER than `claude_2`\n- asynchronous mode\n- Better caching\n- cleaner errors and passing these up to users\n\n\nThis project is under active development and is extremely unstable, so there are no guarantees it will work for you. If you find a bug or you think it should work in a scenario where it doesn't, file an issue.\n\n\n## Usage\n\n### Step 1\nInstall the library using the following:\n```\npip install claude-api-py\n```\n\nIf that doesn't work, you can install directly from this github repository:\n\n```\npip install git+git://github.com/AshwinPathi/claude-api-py.git\n```\n\nThere is one requirement as of now:\n- `sseclient-py` [link here](https://github.com/mpetazzoni/sseclient)\n\n\n### Step 2\nGet a `sessionKey` from the Claude website. You will need this to start the bot. Ideally also have a user agent of the computer you use to access claude.\n\nYou can get this information by logging into `https://claude.ai/chats` and doing the following:\n\n1. open inspect element (f12 on chrome)\n2. On the top bar, go to the `Application` tab.\n3. Under `Storage`, go to `Cookies`.\n4. look for a cookie called `https://claude.ai`, click it.\n5. click the `sessionKey` field, and copy the session key down. It should begin with `sk-ant-sid01...`\n\n\n### Step 3\nUse the bot. You can see an example at `example.py`.\n\n### Examples:\n\n#### Importing the necessary libraries\n```py\n# The ClaudeClient is the raw API that gives you access to all organization and conversation level API calls\n# with a simple python interface. However, you have to pass organization_uuid and conversation_uuid everywhere, so\n# its not ideal if you want a simple to use API.\nfrom claude import claude_client\n\n# The ClaudeWrapper takes in a claude client instance and allows you to use a single organization and conversation\n# context. This allows you to use the API more ergonomically.\nfrom claude import claude_wrapper\n```\n\n#### Create the client and wrapper\n```py\nclient = claude_client.ClaudeClient(SESSION_KEY)\n\norganizations = client.get_organizations()\n# You can omit passing in the organization uuid and the wrapper will assume\n# you will use the first organization instead.\nclaude_obj = claude_wrapper.ClaudeWrapper(client, organization_uuid=organizations[0]['uuid'])\n```\n\n#### Starting a new conversation\n```py\nnew_conversation_data = claude_obj.start_new_conversation(\"New Conversation\", \"Hi Claude!\")\nconversation_uuid = new_conversation_data['uuid']\n# You can get the response from the initial message you sent with:\ninitial_response = new_conversation_data['response']\n# You can get the title of the new chat you created with this:\nchat_title = new_conversation_data['title']\n```\n\n#### Send a message (passing in the client uuid)\n```py\nconversation_uuid = claude_obj.get_conversations()[0]['uuid']\nresponse = claude_obj.send_message(\"How are you doing today!\", conversation_uuid=conversation_uuid)\n```\n\n#### Setting a conversation context and sending a message\n```py\nconversation_uuid = claude_obj.get_conversations()[0]['uuid']\n# This is so you don't have to constantly pass in conversation uuid on every call that requires it.\n# anywhere that has an argument conversation_uuid=X can be omitted if you set the conversation context.\nclaude_obj.set_conversation_context(conversation_uuid)\n\nresponse = claude_obj.send_message(\"How are you doing today!\")\nresponse = claude_obj.send_message(\"Who won the league of legends worlds 2022 finals?\")\n```\n\n#### Sending an attachment\n```py\n# This generates an attachment in the right format\nattachment = claude_obj.get_attachment('example_attachment.txt')\nresponse = claude_obj.send_message(\"Hi Claude, what does this attachment say?\", attachments=[attachment],\n                                    conversation_uuid = conversation_uuid)\n```\n\n#### Deleting a conversation\n```py\ndeleted = claude_obj.delete_conversation(conversation_uuid)\n```\n\n#### Deleting all conversations in an organization\n```py\nfailed_deletions = claude_obj.delete_all_conversations()\nassert len(failed_deletions) == 0\n```\n\n#### Renaming a conversation\n```py\nconversation = claude_obj.rename_conversation(\"New name\", conversation_uuid = conversation_uuid)\n```\n\n#### Get conversation history\n```py\nconversation_history = claude_obj.get_conversation_info(conversation_uuid = conversation_uuid)\n```\n\n\n## Disclaimer\nThis library is for purely educational purposes and is UNOFFICIAL. I am not responsible if your account gets banned. If you would like to use the actual API, go to [anthropic website](https://docs.anthropic.com/claude/docs).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Unofficial Anthropic Claude API for Python3.",
    "version": "0.0.5",
    "project_urls": {
        "Homepage": "https://github.com/AshwinPathi/claude-api-py"
    },
    "split_keywords": [
        "llm",
        "claude",
        "api",
        "gpt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db2c5ef876a09ba07ab7a99904a9a0d98b60a568aa4d2b3590fd51dcb96cdea1",
                "md5": "486cc75de4c3b3adfafc712667940ab7",
                "sha256": "fcfea43aba7d77ba5c89b9edd11ade16aa684fc03c93f2edf3c1837a8add1ba3"
            },
            "downloads": -1,
            "filename": "claude_api_py-0.0.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "486cc75de4c3b3adfafc712667940ab7",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15382,
            "upload_time": "2024-03-06T09:18:33",
            "upload_time_iso_8601": "2024-03-06T09:18:33.802478Z",
            "url": "https://files.pythonhosted.org/packages/db/2c/5ef876a09ba07ab7a99904a9a0d98b60a568aa4d2b3590fd51dcb96cdea1/claude_api_py-0.0.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "20b33e30c9593f2f661bcd79284f763148bf39e4c3bad84cd712d63601b6374e",
                "md5": "76d251e3775ac9b745ad26686fe3ca33",
                "sha256": "abfdf9d26c7ec4c58c9f4adceafdb9fa6c695cbb673262cb02a1ac0f5943f02b"
            },
            "downloads": -1,
            "filename": "claude-api-py-0.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "76d251e3775ac9b745ad26686fe3ca33",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 15111,
            "upload_time": "2024-03-06T09:18:35",
            "upload_time_iso_8601": "2024-03-06T09:18:35.713210Z",
            "url": "https://files.pythonhosted.org/packages/20/b3/3e30c9593f2f661bcd79284f763148bf39e4c3bad84cd712d63601b6374e/claude-api-py-0.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-06 09:18:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "AshwinPathi",
    "github_project": "claude-api-py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "claude-api-py"
}
        
Elapsed time: 0.19632s