Code Monkey home page Code Monkey logo

Comments (9)

rattrayalex avatar rattrayalex commented on June 22, 2024 1

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

from anthropic-sdk-python.

rattrayalex avatar rattrayalex commented on June 22, 2024 1

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).

from anthropic-sdk-python.

rattrayalex avatar rattrayalex commented on June 22, 2024

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

from anthropic-sdk-python.

shirubei avatar shirubei commented on June 22, 2024

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

from anthropic-sdk-python.

rattrayalex avatar rattrayalex commented on June 22, 2024

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
}
'

from anthropic-sdk-python.

shirubei avatar shirubei commented on June 22, 2024

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'}}

from anthropic-sdk-python.

shirubei avatar shirubei commented on June 22, 2024

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:
}'

from anthropic-sdk-python.

rattrayalex avatar rattrayalex commented on June 22, 2024

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.

from anthropic-sdk-python.

shirubei avatar shirubei commented on June 22, 2024

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".

from anthropic-sdk-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.