ChatAnthropic

ChatAnthropic(
    system_prompt=None,
    turns=None,
    model=None,
    api_key=None,
    max_tokens=4096,
    kwargs=None,
)

Chat with an Anthropic Claude model.

Anthropic provides a number of chat based models under the Claude moniker.

Prerequisites

API key

Note that a Claude Pro membership does not give you the ability to call models via the API. You will need to go to the developer console to sign up (and pay for) a developer account that will give you an API key that you can use with this package.

Python requirements

ChatAnthropic requires the anthropic package: pip install "chatlas[anthropic]".

Examples

import os
from chatlas import ChatAnthropic

chat = ChatAnthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
chat.chat("What is the capital of France?")

Parameters

Name Type Description Default
system_prompt Optional[str] A system prompt to set the behavior of the assistant. None
turns Optional[list[Turn]] A list of turns to start the chat with (i.e., continuing a previous conversation). If not provided, the conversation begins from scratch. Do not provide non-None values for both turns and system_prompt. Each message in the list should be a dictionary with at least role (usually system, user, or assistant, but tool is also possible). Normally there is also a content field, which is a string. None
model 'Optional[ModelParam]' The model to use for the chat. The default, None, will pick a reasonable default, and warn you about it. We strongly recommend explicitly choosing a model for all but the most casual use. None
api_key Optional[str] The API key to use for authentication. You generally should not supply this directly, but instead set the ANTHROPIC_API_KEY environment variable. None
max_tokens int Maximum number of tokens to generate before stopping. 4096
kwargs Optional['ChatClientArgs'] Additional arguments to pass to the anthropic.Anthropic() client constructor. None

Returns

Name Type Description
Chat A Chat object.

Note

Pasting an API key into a chat constructor (e.g., ChatAnthropic(api_key="...")) is the simplest way to get started, and is fine for interactive use, but is problematic for code that may be shared with others.

Instead, consider using environment variables or a configuration file to manage your credentials. One popular way to manage credentials is to use a .env file to store your credentials, and then use the python-dotenv package to load them into your environment.

pip install python-dotenv
# .env
ANTHROPIC_API_KEY=...
from chatlas import ChatAnthropic
from dotenv import load_dotenv

load_dotenv()
chat = ChatAnthropic()
chat.console()

Another, more general, solution is to load your environment variables into the shell before starting Python (maybe in a .bashrc, .zshrc, etc. file):

export ANTHROPIC_API_KEY=...