anthropics / anthropic-sdk-python

MIT License
1.47k stars 172 forks source link

Error Code 400 #609

Closed Montana closed 3 months ago

Montana commented 3 months ago

Hey all,

I'm building a Python/Flask app and deploying it to Heroku. I've made sure my Environment Variables are correctly correlated and I have enough credits when I try to prompt:

Anthropic: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'prompt must start with "\n\nHuman:" turn after an optional system prompt'}}

Ths is from directly the Heroku logs:

heroku logs --tail

I really appreciate it!

Screenshot 2024-07-23 at 12 42 28 PM


Cheers, Montana

RobertCraigie commented 3 months ago

@Montana it looks like you're using the completions API, please switch to the messages API. https://github.com/anthropics/anthropic-sdk-python#usage

Montana commented 3 months ago

Hi @RobertCraigie,

I went back to completion via:

 client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
    try:
        response = client.completions.create(
            model="claude-2",
            max_tokens_to_sample=1000,
            prompt=f"{HUMAN_PROMPT} Please correct this Travis CI YAML file:\n\n{yml_content}\n\n{AI_PROMPT}"
        )
        logging.info("YAML correction successful")
        return response.completion.split("Key Points")[0].strip()  # montana remember to for loop
    except Exception as e:
        logging.error(f"Error correcting YAML: {str(e)}")
        return f"Error correcting YAML: {str(e)}"

def send_anthropic_prompt(prompt):
    client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
    try:
        response = client.completions.create(
            model="claude-2",
            max_tokens_to_sample=2000,
            prompt=f"{HUMAN_PROMPT} Please assist with the following:\n\n{prompt}\n\n{AI_PROMPT}"
        )
        logging.info("Received response from Anthropic")
        return response.completion
    except Exception as e:
        logging.error(f"Error sending prompt to Anthropic: {str(e)}")
        return "I apologize, but I'm currently unable to process your request due to a technical issue. Please try again later or contact support if the problem persists."

It works fine again, not sure what the issue is. API keys were fine. This is was working yesterday - but today I had to change the logic a bit and it worked.


Cheers, Montana