Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid API Key when using claude 2.0 #81

Closed
shirubei opened this issue Jul 21, 2023 · 9 comments
Closed

Invalid API Key when using claude 2.0 #81

shirubei opened this issue Jul 21, 2023 · 9 comments

Comments

@shirubei
Copy link

Windows 11 with Pythno 3.10 and used these codes . It turned out to an "Invalid API Key" error.
But I'm sure the api_key is good because I could get good response via unofficial API call (from other github repository).

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT

anthropic = Anthropic(api_key = "sk-ant-XXXXXX")

def getResponse(prompt):
msg=f"{HUMAN_PROMPT} {prompt} {AI_PROMPT}"
print(msg)
completion = anthropic.completions.create(
model = "claude-2",
max_tokens_to_sample = 30000,
prompt = msg,
)
res = completion.completion
print(res)
return res

if name == "main":
getResponse("Hello, Claude")

the last 3 lines of error messages:
File "D:\Python310\anthropic\lib\site-packages\anthropic_base_client.py", line 761, in _request
raise self._make_status_error_from_response(request, err.response) from None
anthropic.AuthenticationError: Error code: 401 - {'error': {'type': 'authentication_error', 'message': 'Invalid API Key'}}

Appreciate your help. Thanks.

@rattrayalex
Copy link
Collaborator

Hmm, that's surprising. Can you share the request ID for one working request and one non-working request?

@shirubei
Copy link
Author

Thanks for your reply. I have no idea on how to get request ID. Would you please show me those steps ? @rattrayalex

@rattrayalex
Copy link
Collaborator

rattrayalex commented Jul 22, 2023

In this library, it's like this:

from anthropic import Anthropic, HUMAN_PROMPT, AI_PROMPT, APIStatusError

anthropic = Anthropic(api_key="sk-ant-XXXXXX")

def getResponse(prompt):
  msg=f"{HUMAN_PROMPT} {prompt} {AI_PROMPT}"
  print(msg)
  try:
    completion = anthropic.completions.create(
      model="claude-2",
      max_tokens_to_sample=30000,
      prompt=msg,
    )
  except APIStatusError as e:
    print('request-id:', e.response.headers['request-id'])
    raise e
    
  res = completion.completion
  print(res)
  return res

Outside of this library, you can make a request with curl and view the headers with -i, eg:

curl -i --request POST \
     --url https://api.anthropic.com/v1/complete \
     --header 'accept: application/json' \
     --header 'anthropic-version: 2023-06-01' \
     --header 'content-type: application/json' \
     --header 'x-api-key: sk-ant-XXXXXX' \
     --data '
{
  "model": "claude-2",
  "prompt": "\n\nHuman: hello? \n\nAssistant:"
  "max_tokens_to_sample": 30000
}
'

@shirubei
Copy link
Author

shirubei commented Jul 22, 2023

Thanks again.
I run this cmd:
curl -i --request POST --url https://api.anthropic.com/v1/complete --header 'accept: application/json' --header 'anthropic-version: 2023-06-01' --header 'content-type: application/json' --header 'x-api-key: sk-ant-XXXXXXXXXXXXXX' --data '{"model": "claude-2", "prompt": "\n\nHuman: hello? \n\nAssistant:", "max_tokens_to_sample": 30000 }'

and output from this curl cmd :
HTTP/1.1 401 Unauthorized
x-should-retry: false
content-type: application/json
request-id: c695aad92cc6b1485940db6f011c62e0221a0c0ef393e1e8af072c36f9486c56
x-cloud-trace-context: b2c7938dcfd6d330d45966c4a88f856f
date: Sat, 22 Jul 2023 01:00:48 GMT
server: Google Frontend
Content-Length: 75
via: 1.1 google
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

{"error":{"type":"authentication_error","message":"x-api-key is required"}}curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (7) Failed to connect to 0.0.117.48 port 80 after 0 ms: Couldn't connect to server
curl: (3) unmatched close brace/bracket in URL position 1:
}'

Output from python code provided by you (same as the top thread). @rattrayalex
request-id: 4d086267225616b89df3ef3d2290fa28ee5f8bc154a67da9d6decabe678d7117
anthropic.AuthenticationError: Error code: 401 - {'error': {'type': 'authentication_error', 'message': 'Invalid API Key'}}

@rattrayalex
Copy link
Collaborator

I think you may need to quote the URL in your curl request, eg; --url "https://api.anthropic.com/v1/complete"

@shirubei
Copy link
Author

Thanks.
curl -i --request POST --url "https://api.anthropic.com/v1/complete" --header 'accept: application/json' --header 'anthropic-version: 2023-06-01' --header 'content-type: application/json' --header 'x-api-key: sk-ant-XXXXXXXXX' --data '{"model": "claude-2", "prompt": "\n\nHuman: hello? \n\nAssistant:", "max_tokens_to_sample": 30000 }'

and output:
HTTP/1.1 401 Unauthorized
x-should-retry: false
content-type: application/json
request-id: dbdc92f7a5adb86bf970a008a717ef40b2e7b982780e36f748da5d4bfdb011a6
x-cloud-trace-context: 0875662a362bbdffa584377e50e022e0
date: Sat, 22 Jul 2023 01:22:16 GMT
server: Google Frontend
Content-Length: 75
via: 1.1 google
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

{"error":{"type":"authentication_error","message":"x-api-key is required"}}curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: application
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (7) Failed to connect to 0.0.117.48 port 80 after 0 ms: Couldn't connect to server
curl: (3) unmatched close brace/bracket in URL position 1:
}'

@rattrayalex
Copy link
Collaborator

It looks like you're having some trouble sending a valid curl request. Once you're able to show that you can make a successful curl request (or other request) with the exact same API key and can share a request-id header from that request, we can reopen this issue.

@shirubei
Copy link
Author

shirubei commented Jul 22, 2023

Just want to make sure if there's any ip restrict on your side.
I run the same cmd on a remote machine and it turned out successfully. @rattrayalex

request-id: 5d91bd3b74ebca7c4b167f1c181525123ef7eeac2b58ea3b1ccdd33ae9516b40

PS:I made a mistake. This one also failed with same error "Invalid API Key".

@rattrayalex
Copy link
Collaborator

rattrayalex commented Jul 22, 2023

Got it, thank you. Were you able to try the request from the SDK on the same remote machine?

You can use the ANTHROPIC_API_KEY environment variable (instead of passing an api_key kwarg) to ensure it's the exact same one as you use in the curl request (with --header "x-api-key: $ANTHROPIC_API_KEY", of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants