flaml.autogen is a subpackage for automating generation tasks. It uses flaml.tune to find good hyperparameter configurations under budget constraints.
Such optimization has several benefits:
The cost of using foundation models for text generation is typically measured in terms of the number of tokens in the input and output combined. From the perspective of an application builder using foundation models, the use case is to maximize the utility of the generated text under an inference budget constraint (e.g., measured by the average dollar cost needed to solve a coding problem). This can be achieved by optimizing the hyperparameters of the inference,
which can significantly affect both the utility and the cost of the generated text.
The tunable hyperparameters include:
The cost and utility of text generation are intertwined with the joint effect of these hyperparameters.
There are also complex interactions among subsets of the hyperparameters. For example,
the temperature and top_p are not recommended to be altered from their default values together because they both control the randomness of the generated text, and changing both at the same time can result in conflicting effects; n and best_of are rarely tuned together because if the application can process multiple outputs, filtering on the server side causes unnecessary information loss; both n and max_tokens will affect the total number of tokens generated, which in turn will affect the cost of the request.
These interactions and trade-offs make it difficult to manually determine the optimal hyperparameter settings for a given text generation task.
The tuning can be performed with the following information:
Collect a diverse set of instances. They can be stored in an iterable of dicts. For example, each instance dict can contain "problem" as a key and the description str of a math problem as the value; and "solution" as a key and the solution str as the value.
The evaluation function should take a list of responses, and other keyword arguments corresponding to the keys in each validation data instance as input, and output a dict of metrics. For example,
def success_metrics(responses: List[str], problem: str, solution: str) -> Dict:
# select a response from the list of responses
# check whether the answer is correct
return {"success": True or False}
flaml.autogen offers some example evaluation functions for common tasks such as code generation and math problem solving.
The metric to optimize is usually an aggregated metric over all the tuning data instances. For example, users can specify "success" as the metric and "max" as the optimization mode. By default, the aggregation function is taking the average. Users can provide a customized aggregation function if needed.
Users can specify the (optional) search range for each hyperparameter.
flaml.tune.choice.{problem} will be replaced by the "problem" field of each data instance.flaml.tune.randint, flaml.tune.qrandint, flaml.tune.lograndint or flaml.qlograndint. By default, max_tokens is searched in [50, 1000); n is searched in [1, 100); and best_of is fixed to 1.flaml.tune.uniform or flaml.tune.loguniform etc.flaml.tune.uniform etc. Not tuned by default.One can specify an inference budget and an optimization budget.
The inference budget refers to the average inference cost per data instance.
The optimization budget refers to the total budget allowed in the tuning process. Both are measured by dollars and follow the price per 1000 tokens.
Now, you can use flaml.oai.Completion.tune for tuning. For example,
from flaml import oai
config, analysis = oai.Completion.tune(
data=tune_data,
metric="success",
mode="max",
eval_func=eval_func,
inference_budget=0.05,
optimization_budget=3,
num_samples=-1,
)
num_samples is the number of configurations to sample. -1 means unlimited (until optimization budget is exhausted).
The returned config contains the optimized configuration and analysis contains an ExperimentAnalysis object for all the tried configurations and results.
One can use flaml.oai.Completion.create to performance inference. It materializes a prompt using a given context. For example,
response = oai.Completion.create(problme=problem, **config)
responses = oai.Completion.extract_text(response)
# Extract a list of str responses
flaml.oai.Completion is compatible with both openai.Completion and openai.ChatCompletion. So models such as "text-davinci-003", "gpt-3.5-turbo" and "gpt-4" can share a common API. When only tuning the chat-based models, flaml.oai.ChatCompletion can be used.
flaml.oai.Completion also offers some additional utilities including a test function to conveniently evaluate the configuration over test data, a cost function to calculate the cost of an API call, and caching and error handling. It also supports both OpenAI API and Azure OpenAI API.
Interested in trying it yourself? Please check the following notebook examples: