Package: claude-api-py
Unofficial Claude API For PythonThe UNOFFICIAL free API for Anthropic's Claude LLM.BackgroundClaude isAnthropic'sLLM 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 inGetting conversations you're inStarting a conversationSending a message and receiving a responseDelete a conversationCreate an attachment from a fileSend attachmentsgetting message historyNote that the api issynchronous.TODO featuresTestsFixing models OTHER thanclaude_2asynchronous modeBetter cachingcleaner errors and passing these up to usersThis 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.UsageStep 1Install the library using the following:pip install claude-api-pyIf that doesn't work, you can install directly from this github repository:pip install git+git://github.com/AshwinPathi/claude-api-py.gitThere is one requirement as of now:sseclient-pylink hereStep 2Get asessionKeyfrom 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 intohttps://claude.ai/chatsand doing the following:open inspect element (f12 on chrome)On the top bar, go to theApplicationtab.UnderStorage, go toCookies.look for a cookie calledhttps://claude.ai, click it.click thesessionKeyfield, and copy the session key down. It should begin withsk-ant-sid01...Step 3Use the bot. You can see an example atexample.py.Examples:Importing the necessary libraries# 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.fromclaudeimportclaude_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.fromclaudeimportclaude_wrapperCreate the client and wrapperclient=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 conversationnew_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)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 messageconversation_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# This generates an attachment in the right formatattachment=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 conversationdeleted=claude_obj.delete_conversation(conversation_uuid)Deleting all conversations in an organizationfailed_deletions=claude_obj.delete_all_conversations()assertlen(failed_deletions)==0Renaming a conversationconversation=claude_obj.rename_conversation("New name",conversation_uuid=conversation_uuid)Get conversation historyconversation_history=claude_obj.get_conversation_info(conversation_uuid=conversation_uuid)DisclaimerThis 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 toanthropic website.