AshwinPathi / claude-api-py

Unofficial Python API for Anthropic's Claude LLM
https://pypi.org/project/claude-api-py/
MIT License
108 stars 11 forks source link

Writing a Blogpost #1

Closed MLWhiz closed 11 months ago

MLWhiz commented 11 months ago

Hey Ashwin

Great effort. Would it be alright if I write an Medium post for this repo?

Thanks

AshwinPathi commented 11 months ago

@MLWhiz It's still unstable so there are no guarantees it'll work, but if you're able to actually use it I don't mind if you post about it.

MLWhiz commented 11 months ago

Yes. I tried it and it gives me a403 error as of now. Seems to be an header issue for requests module

On Sun, 16 Jul 2023 at 22:25, Ashwin @.***> wrote:

@MLWhiz https://github.com/MLWhiz It's still unstable so there are no guarantees it'll work, but if you're able to actually use it I don't mind if you post about it.

— Reply to this email directly, view it on GitHub https://github.com/AshwinPathi/claude/issues/1#issuecomment-1637193594, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFK26IU4KJM3AYEB2EM4ALXQRL6BANCNFSM6AAAAAA2MBWMUE . You are receiving this because you were mentioned.Message ID: @.***>

-- Regards, Rahul Agarwal Machine Learning Engineer at Facebook Linkedin Profile: https://www.linkedin.com/in/rahul-agarwal-b8a29724/

AshwinPathi commented 11 months ago

@MLWhiz The API supports providing a spoofed header that you can use to bypass 403s.

You can find these headers by going to the claude website, initiating a chat, opening up inspect element, going to the networks tab, and copying the headers that your browser uses to send requests. Convert that header to a python dictionary and you can pass it into the API like so:

session_key = ...
spoofed_headers = {'user-agent': ..., 'accept': ..., ...}
api = ClaudeAPI(session_key, spoofed_headers=spoofed_headers)

I haven't experimented with what headers actually matter, but I'd wager that the necessary one is the user-agent header. Let me know if that works.

MLWhiz commented 11 months ago

Already tried user-agent. Didn’t work for me.

On Sun, 16 Jul 2023 at 22:58, Ashwin @.***> wrote:

@MLWhiz https://github.com/MLWhiz The API supports providing a spoofed header that you can use to bypass 403s.

You can find these headers by going to the claude website, initiating a chat, opening up inspect element, going to the networks tab, and copying the headers that your browser uses to send requests. Convert that header to a python dictionary and you can pass it into the API like so:

session_key = ...spoofed_headers = {'user-agent': ..., 'accept': ..., ...}api = ClaudeAPI(session_key, spoofed_headers=spoofed_headers)

I haven't experimented with what headers actually matter, but I'd wager that the necessary one is the user-agent header. Let me know if that works.

— Reply to this email directly, view it on GitHub https://github.com/AshwinPathi/claude/issues/1#issuecomment-1637198783, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFK26LP4OHE34AW6PJLPJLXQRPWRANCNFSM6AAAAAA2MBWMUE . You are receiving this because you were mentioned.Message ID: @.***>

-- Regards, Rahul Agarwal Machine Learning Engineer at Facebook Linkedin Profile: https://www.linkedin.com/in/rahul-agarwal-b8a29724/

AshwinPathi commented 11 months ago

@MLWhiz did you just use User-Agent or did you spoof the full set of headers that the browser sends? Also, which requests does it fail on?

For example, it should look something like this:

# Some fields left out since these are browswer specific. Find them using inspect element.
CUSTOM_HEADERS = {
    'authority': 'claude.ai',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'accept-language': 'en-US,en;q=0.9',
    'cache-control': 'max-age=0',
    'dnt': '1',
    'sec-ch-ua': '{Broswer metadata}',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-origin',
    'upgrade-insecure-requests': '1',
    'user-agent': '{user agent of choice}',
}

You can try this header but its probably best to generate one on your own using the method I mentioned before. I just tested it out and it works locally, let me know if this fixes the issue.

Keep in mind if you pull the headers directly from inspect element it'll also contain a header section for cookies - delete this section when you use the headers in the code since you already provide cookies through the session_key constructor argument.

MLWhiz commented 11 months ago

Just used user-agent. Will try this as well.

On Sun, 16 Jul 2023 at 23:03, Ashwin @.***> wrote:

@MLWhiz https://github.com/MLWhiz did you just use User-Agent or did you spoof the full set of headers that the browser sends? For example, it should look something like this:

CUSTOM_HEADERS = { 'authority': 'claude.ai', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7', 'accept-language': 'en-US,en;q=0.9', 'cache-control': 'max-age=0', 'dnt': '1', 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"macOS"', 'sec-fetch-dest': 'empty', 'sec-fetch-mode': 'navigate', 'sec-fetch-site': 'same-origin', 'upgrade-insecure-requests': '1', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36', }

You can try this user agent but its probably best to generate one on your own using the method I mentioned before. I just tested it out and it works locally, let me know if this fixes the issue.

— Reply to this email directly, view it on GitHub https://github.com/AshwinPathi/claude/issues/1#issuecomment-1637199736, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFK26IPLRQSSGGBLXE3QOTXQRQJLANCNFSM6AAAAAA2MBWMUE . You are receiving this because you were mentioned.Message ID: @.***>

-- Regards, Rahul Agarwal Machine Learning Engineer at Facebook Linkedin Profile: https://www.linkedin.com/in/rahul-agarwal-b8a29724/

AshwinPathi commented 11 months ago

@MLWhiz were you able to get it to work? I'll close the issue if you did.

MLWhiz commented 11 months ago

yes, it works. thanks man

On Mon, Jul 17, 2023 at 1:54 AM Ashwin @.***> wrote:

@MLWhiz https://github.com/MLWhiz were you able to get it to work? I'll close the issue if you did.

— Reply to this email directly, view it on GitHub https://github.com/AshwinPathi/claude/issues/1#issuecomment-1637244390, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFK26IORMRVPDOY3BTHOUTXQSENFANCNFSM6AAAAAA2MBWMUE . You are receiving this because you were mentioned.Message ID: @.***>

-- Regards, Rahul Agarwal Machine Learning Engineer at Facebook Linkedin Profile: https://www.linkedin.com/in/rahul-agarwal-b8a29724/

AshwinPathi commented 11 months ago

@MLWhiz Cool. Looking forward to reading the article.