cappr.openai.api#
Helpers functions which are compatible with the Python OpenAI API (before and after v1.0.0)
- cappr.openai.api.Model#
These are /v1/completions models where echo=True, logprobs=1 can be passed. On October 5, 2023, OpenAI deprecated this combination of arguments for the
gpt-3.5-turbo-instructmodel. So it cannot supported by CAPPr.alias of
Literal[‘babbage-002’, ‘davinci-002’]
- cappr.openai.api.gpt_chat_complete(texts: Sequence[str], model: str = 'gpt-3.5-turbo', client: OpenAI | None = None, show_progress_bar: bool | None = None, ask_if_ok: bool = False, api_key: str | None = None, system_msg: str = 'You are an assistant which classifies text.', max_tokens: int = 5, temperature: float = 0, **openai_chat_kwargs) list[dict[str, Any]][source]#
Wrapper around the OpenAI chat completion endpoint which sends texts 1-by-1 as individual chat messages in a chat. It retries requests that fail and displays a progress bar.
OpenAI API chat completion reference: https://platform.openai.com/docs/api-reference/chat
Warning
By default, the system_msg asks ChatGPT to perform text classification. And the temperature is set to 0.
- Parameters:
texts (Sequence[str]) – texts which are passed in one by one immediately after the system content as
{"role": "user", "content": text}model (str, optional) – one of the chat model names, by default “gpt-3.5-turbo”
client (OpenAI | None, optional) – an OpenAI client object. By default, the global client instance is used
show_progress_bar (bool | None, optional) – whether or not to show a progress bar. By default, it will be shown only if there are at least 5 texts
ask_if_ok (bool, optional) – whether or not to prompt you to manually give the go-ahead to run this function, after notifying you of the approximate cost of the OpenAI API calls. By default, False
api_key (str | None, optional) – your OpenAI API key. By default, it’s set to the OpenAI’s module attribute
openai.api_key, or the environment variableOPENAI_API_KEYsystem_msg (str, optional) – text which is passed in 1-by-1 immediately before every piece of user content in texts as
{"role": "system", "content": system_msg}. By default"You are an assistant which classifies text."max_tokens (int, optional) – maximum number of tokens to generate, by default 5
temperature (float, optional) – probability re-scaler to apply before sampling, by default 0
**openai_chat_kwargs – other arguments passed to the chat completion endpoint
- Returns:
list with the same length as texts. Each element is the
choicesmapping- Return type:
list[dict[str, Any]]
- cappr.openai.api.gpt_complete(texts: Sequence[str], model: Model, client: OpenAI | None = None, show_progress_bar: bool | None = None, progress_bar_desc: str = 'log-probs', ask_if_ok: bool = False, api_key: str | None = None, max_tokens: int = 0, **openai_completion_kwargs) list[dict[str, Any]][source]#
Wrapper around the OpenAI text completion endpoint which automatically batches texts for greater efficiency, retries requests that fail, and displays a progress bar.
OpenAI API text completion reference: https://platform.openai.com/docs/api-reference/completions
Warning
By default, no tokens will be generated/sampled.
- Parameters:
texts (Sequence[str]) – these are passed as the prompt argument in a text completion request
model (Model) – which text completion model to use
client (OpenAI | None, optional) – an OpenAI client object. By default, the global client instance is used
show_progress_bar (bool | None, optional) – whether or not to show a progress bar. By default, it will be shown only if there are at least 5 texts
progress_bar_desc (str, optional) – description of the progress bar if shown, by default
'log-probs'ask_if_ok (bool, optional) – whether or not to prompt you to manually give the go-ahead to run this function, after notifying you of the approximate cost of the OpenAI API calls. By default, False
api_key (str | None, optional) – your OpenAI API key. By default, it’s set to the OpenAI’s module attribute
openai.api_key, or the environment variableOPENAI_API_KEYmax_tokens (int, optional) – maximum number of tokens to generate, by default 0
**openai_completion_kwargs – other arguments passed to the text completion endpoint, e.g., logprobs=1
- Returns:
list with the same length as texts. Each element is the
choicesmapping- Return type:
list[dict[str, Any]]
- cappr.openai.api.openai_method_retry(openai_method: ~typing.Callable, max_num_tries: int = 5, sleep_sec: float = 10, retry_errors: tuple = (<class 'openai.InternalServerError'>, <class 'openai.RateLimitError'>), **openai_method_kwargs)[source]#
Wrapper around OpenAI API calls which automatically retries and sleeps if requests fail. Logs at level INFO when requests fail, and level ERROR if an exception is raised.
- Parameters:
openai_method (Callable) – a function or method whose inputs are openai_method_kwargs
max_num_tries (int, optional) – maximum number of times to retry the request before raising the exception, by default 5
sleep_sec (float, optional) – number of seconds to sleep before re-submitting the request, by default 10
retry_errors (tuple[Exception], optional) – if one of these exceptions is raised by the request, then retry, else the exception is immediately raised. By default, these are the ServiceUnavailableError/InternalServerError and RateLimitError.
- Returns:
openai_method(**openai_method_kwargs)
- Return type:
Any
- Raises:
Exception – if max_num_tries is exceeded or an exception not in retry_errors is raised