MagnivOrg / prompt-layer-library

🍰 PromptLayer - Maintain a log of your prompts and OpenAI API requests. Track, debug, and replay old completions.
https://www.promptlayer.com
Apache License 2.0
477 stars 42 forks source link

Issues with Claude Error Handling #78

Closed BedirT closed 9 months ago

BedirT commented 9 months ago

Hey,

It seems like the API doesn't properly wrap around the error calls from Claude. Here is the suggested method. I get an error when I try to use it with promptlayer:

except anthropic.APIConnectionError as e:
TypeError: catching classes that do not inherit from BaseException is not allowed
Jped commented 9 months ago

@BedirT do you have a code snippet or steps I can use to recreate this?

Jped commented 9 months ago

Recreating it by:

BedirT commented 9 months ago

Here

import anthropic
import promptlayer
import os

import logging
log = logging.getLogger(__name__)

# promptlayer.api_key = os.environ.get("PROMPTLAYER_API_KEY")
# anthropic = promptlayer.anthropic
anthropic_api_key = os.environ["ANTHROPIC_API_KEY"]

anthropic_ = anthropic.Anthropic(api_key=anthropic_api_key)

try:
    response = anthropic_.completions.create(
        prompt='Hello there',
        max_tokens_to_sample=1000000, # above 100k to get error
        model='claude-2'
    )
except anthropic.APIConnectionError as e:
    log.error("The server could not be reached")
    log.error(e.__cause__)  # an underlying Exception, likely raised within httpx.
except anthropic.RateLimitError as e:
    log.error("A 429 status code was received; we should back off a bit.")
except anthropic.APIStatusError as e:
    log.error("Another non-200-range status code was received")
    log.error(e.status_code)
    log.error(e.response)
Jped commented 9 months ago

@BedirT can you upgrade the python library and try again it should be fixed

BedirT commented 9 months ago

Thanks its fixed, though you might want to remove this :)

https://github.com/MagnivOrg/prompt-layer-library/commit/001f974753c8588f7f839e0322bb29ea0b8574ce#r128073255

Jped commented 9 months ago

🤦‍♂️ Thanks, just removed that ...