Open AI#

class src.openCHA.llms.openai.OpenAILLM(*, models: Dict = {'ada': 2049, 'babbage': 2049, 'code-cushman-001': 2048, 'code-cushman-002': 2048, 'code-davinci-001': 8001, 'code-davinci-002': 8001, 'curie': 2049, 'davinci': 2049, 'gpt-3.5-turbo': 4096, 'gpt-3.5-turbo-0301': 4096, 'gpt-3.5-turbo-0613': 4096, 'gpt-3.5-turbo-1106': 16385, 'gpt-3.5-turbo-16k': 16385, 'gpt-3.5-turbo-16k-0613': 16385, 'gpt-4': 8192, 'gpt-4-0314': 8192, 'gpt-4-0613': 8192, 'gpt-4-32k': 32768, 'gpt-4-32k-0314': 32768, 'gpt-4-32k-0613': 32768, 'text-ada-001': 2049, 'text-babbage-001': 2040, 'text-curie-001': 2049, 'text-davinci-002': 4097, 'text-davinci-003': 4097}, api_key: str = '', llm_model: Any = None, max_tokens: int = 150)[source]#

Description:

An implementation of the OpenAI APIs. OpenAI API

_parse_response(response) 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.

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.

It calculates the number of tokens from tokenizing the input query and compares it with the maximum allowed tokens for the model. If the number of tokens is greater than the maximum, it returns True.

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 is defined as a validation model for the class and checks the required environment values for using OpenAILLM. If the “openai_api_key” key exists in the input, its value is assigned to the “api_key” variable. Additionally, it checksthe existence of the openai library, and if it’s not found, it raises an error.

Parameters:
  • cls (type) – 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.