Anthropic#

class llms.anthropic.AntropicLLM(*, models: Dict = {'claude-2': 100000}, llm_model: Any = None, api_key: str = '', HUMAN_PROMPT: str = '', AI_PROMPT: str = '')[source]#

description:

This code implements Anthropic LLM API. This class uses the Anthropic service to connect to a language model for generating text based on user queries. Anthropic API

_parse_response(response: Dict) str[source]#

Parse the response object and return the generated completion text.

Parameters:

response (object) – The response object.

Returns:

The generated completion text.

Return type:

str

_prepare_prompt(prompt) Any[source]#

Prepare the prompt by combining the human and AI prompts with the input prompt.

Parameters:

prompt (str) – The input prompt.

Returns:

The prepared prompt.

Return type:

Any

generate(query: str, **kwargs: Any) str[source]#

Generate a response based on the provided query. This calls anthropic API to generate the text.

Parameters:
  • query (str) – The query to generate a response for.

  • **kwargs (Any) – Additional keyword arguments.

Returns:

The generated response.

Return type:

str

Raises:

ValueError – If the model name is not specified or is not supported.

get_model_names() List[str][source]#

Get a list of available model names.

Returns:

A list of available model names.

Return type:

List[str]

is_max_token(model_name, query) bool[source]#

Check if the token count of the query exceeds the maximum token count for the specified model.

Parameters:
  • model_name (str) – The name of the model.

  • query (str) – The query to check.

Returns:

True if the token count exceeds the maximum, False otherwise.

Return type:

bool

classmethod validate_environment(values: Dict) Dict[source]#

Validate that api key and python package exists in environment.

This method validates the environment by checking the existence of the API key and required Python packages. It retrieves the API key from either the “anthropic_api_key” key in the “values” dictionary or from the “ANTHROPIC_API_KEY” environment variable. It also imports the required packages and assigns the appropriate values to the class attributes.

Parameters:
  • cls (object) – The class itself.

  • values (Dict) – The dictionary containing the values for validation.

Returns:

The validated dictionary with updated values.

Return type:

Dict

Raises:

ValueError – If the anthropic python package cannot be imported.