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

Anthropic SDK available for python #17

Open
bent-verbiage opened this issue Apr 2, 2024 · 0 comments
Open

Anthropic SDK available for python #17

bent-verbiage opened this issue Apr 2, 2024 · 0 comments

Comments

@bent-verbiage
Copy link

bent-verbiage commented Apr 2, 2024

Not set up right now to make it a pull request, but for those who care: You can use the anthropic python sdk now.

!pip install -U anthropic
import anthropic

client = anthropic.Anthropic(
    api_key=ANTHROPIC_API_KEY,
)


def ask_claude(system_prompt, messages, model="claude-3-opus-20240229", max_tokens=2000):
    try:
        message = client.messages.create(
            model=model,
            max_tokens=max_tokens,
            temperature=0.5,
            system=system_prompt,
            messages=messages
        )
        # count_tokens(model=model, in_tokens=message.usage.input_tokens, out_tokens=message.usage.output_tokens)
    except anthropic.APIConnectionError as e:
        print("The server could not be reached")
        print(e.__cause__)  # an underlying Exception, likely raised within httpx.
    except anthropic.RateLimitError as e:
        print("A 429 status code was received; we should back off a bit.")
    except anthropic.APIStatusError as e:
        print("Another non-200-range status code was received")
        print(e.status_code)
        print(e.response)
        
    return message.content[0].text
@bent-verbiage bent-verbiage changed the title Anthropic SDK available for python - simplifies code Anthropic SDK available for python Apr 2, 2024
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

1 participant