oai.completion
Completion
(openai<1) A class for OpenAI completion API.
It also supports: ChatCompletion, Azure OpenAI API.
set_cache
Set cache path.
Arguments:
seed
int, Optional - The integer identifier for the pseudo seed. Results corresponding to different seeds will be cached in different places.cache_path
str, Optional - The root path for the cache. The complete cache path will be {cache_path_root}/{seed}.
clear_cache
Clear cache.
Arguments:
seed
int, Optional - The integer identifier for the pseudo seed. If omitted, all caches under cache_path_root will be cleared.cache_path
str, Optional - The root path for the cache. The complete cache path will be {cache_path_root}/{seed}.
tune
Tune the parameters for the OpenAI API call.
TODO: support parallel tuning with ray or spark. TODO: support agg_method as in test
Arguments:
data
list - The list of data points.metric
str - The metric to optimize.mode
str - The optimization mode, “min” or “max.eval_func
Callable - The evaluation function for responses. The function should take a list of responses and a data point as input, and return a dict of metrics. For example,
log_file_name
str, optional - The log file.inference_budget
float, optional - The inference budget, dollar per instance.optimization_budget
float, optional - The optimization budget, dollar in total.num_samples
int, optional - The number of samples to evaluate. -1 means no hard restriction in the number of trials and the actual number is decided by optimization_budget. Defaults to 1.logging_level
optional - logging level. Defaults to logging.WARNING.**config
dict - The search space to update over the default search. For prompt, please provide a string/Callable or a list of strings/Callables.- If prompt is provided for chat models, it will be converted to messages under role “user”.
- Do not provide both prompt and messages for chat models, but provide either of them.
- A string template will be used to generate a prompt for each data instance
using
prompt.format(**data)
. - A callable template will be used to generate a prompt for each data instance
using
prompt(data)
. For stop, please provide a string, a list of strings, or a list of lists of strings. For messages (chat models only), please provide a list of messages (for a single chat prefix) or a list of lists of messages (for multiple choices of chat prefix to choose from). Each message should be a dict with keys “role” and “content”. The value of “content” can be a string/Callable template.
Returns:
dict
- The optimized hyperparameter setting.tune.ExperimentAnalysis
- The tuning results.
create
Make a completion for a given context.
Arguments:
context
Dict, Optional - The context to instantiate the prompt. It needs to contain keys that are used by the prompt template or the filter function. E.g.,prompt="Complete the following sentence: \{prefix}, context=\{"prefix": "Today I feel"}
. The actual prompt will be: “Complete the following sentence: Today I feel”. More examples can be found at templating.use_cache
bool, Optional - Whether to use cached responses.config_list
List, Optional - List of configurations for the completion to try. The first one that does not raise an error will be used. Only the differences from the default config need to be provided. E.g.,
filter_func
Callable, Optional - A function that takes in the context and the response and returns a boolean to indicate whether the response is valid. E.g.,
raise_on_ratelimit_or_timeout
bool, Optional - Whether to raise RateLimitError or Timeout when all configs fail. When set to False, -1 will be returned when all configs fail.allow_format_str_template
bool, Optional - Whether to allow format string template in the config.**config
- Configuration for the openai API call. This is used as parameters for calling openai API. The “prompt” or “messages” parameter can contain a template (str or Callable) which will be instantiated with the context. Besides the parameters for the openai API call, it can also contain:max_retry_period
(int): the total time (in seconds) allowed for retrying failed requests.retry_wait_time
(int): the time interval to wait (in seconds) before retrying a failed request.cache_seed
(int) for the cache. This is useful when implementing “controlled randomness” for the completion.
Returns:
Responses from OpenAI API, with additional fields.
cost
: the total cost. Whenconfig_list
is provided, the response will contain a few more fields:config_id
: the index of the config in the config_list that is used to generate the response.pass_filter
: whether the response passes the filter function. None if no filter is provided.
test
Evaluate the responses created with the config for the OpenAI API call.
Arguments:
data
list - The list of test data points.eval_func
Callable - The evaluation function for responses per data instance. The function should take a list of responses and a data point as input, and return a dict of metrics. You need to either provide a valid callable eval_func; or do not provide one (set None) but call the test function after calling the tune function in which a eval_func is provided. In the latter case we will use the eval_func provided via tune function. Defaults to None.
use_cache
bool, Optional - Whether to use cached responses. Defaults to True.agg_method
str, Callable or a dict of Callable - Result aggregation method (across multiple instances) for each of the metrics. Defaults to ‘avg’. An example agg_method in str:
An example agg_method in a Callable:
An example agg_method in a dict of Callable:
return_responses_and_per_instance_result
bool - Whether to also return responses and per instance results in addition to the aggregated results.logging_level
optional - logging level. Defaults to logging.WARNING.**config
dict - parameters passed to the openai api callcreate()
.
Returns:
None when no valid eval_func is provided in either test or tune;
Otherwise, a dict of aggregated results, responses and per instance results if return_responses_and_per_instance_result
is True;
Otherwise, a dict of aggregated results (responses and per instance results are not returned).
cost
Compute the cost of an API call.
Arguments:
response
dict - The response from OpenAI API.
Returns:
The cost in USD. 0 if the model is not supported.
extract_text
Extract the text from a completion or chat response.
Arguments:
response
dict - The response from OpenAI API.
Returns:
A list of text in the responses.
extract_text_or_function_call
Extract the text or function calls from a completion or chat response.
Arguments:
response
dict - The response from OpenAI API.
Returns:
A list of text or function calls in the responses.
logged_history
Return the book keeping dictionary.
print_usage_summary
Return the usage summary.
start_logging
Start book keeping.
Arguments:
history_dict
Dict - A dictionary for book keeping. If no provided, a new one will be created.compact
bool - Whether to keep the history dictionary compact. Compact history contains one key per conversation, and the value is a dictionary like:
where “created_at” is the index of API calls indicating the order of all the calls, and “cost” is the cost of each call. This example shows that the conversation is based on two API calls. The compact format is useful for condensing the history of a conversation. If compact is False, the history dictionary will contain all the API calls: the key is the index of the API call, and the value is a dictionary like:
where request_dict is the request sent to OpenAI API, and response_dict is the response. For a conversation containing two API calls, the non-compact history dictionary will be like:
The first request’s messages plus the response is equal to the second request’s messages. For a conversation with many turns, the non-compact history dictionary has a quadratic size while the compact history dict has a linear size.
reset_counter
bool - whether to reset the counter of the number of API calls.
stop_logging
End book keeping.
ChatCompletion
(openai<1) A class for OpenAI API ChatCompletion. Share the same API as Completion.