| Title: | Adaptive Optimization of Prompts, Models and Generation Strategies |
|---|---|
| Description: | Provides provider-agnostic tools for jointly comparing and optimizing prompts, language-model providers, and generation strategies for generative artificial intelligence workflows. Candidate configurations can be evaluated using user-supplied scoring functions, cost and latency measurements, robustness perturbations, Pareto-front screening, budget and latency constraints, prompt evolution, adaptive routing, self-consistency, and text-output ensembles. The core workflow is designed to run offline with deterministic mock providers, while external model application programming interfaces can be connected through user-defined provider functions. Evolutionary search concepts are described by Goldberg (1989, ISBN:0201157675), and multi-objective optimization concepts are related to Deb, Pratap, Agarwal and Meyarivan (2002) <doi:10.1109/4235.996017>. |
| Authors: | Leila Marvian Mashhad [aut, cre] |
| Maintainer: | Leila Marvian Mashhad <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-10-13 11:26:45 UTC |
| Source: | https://github.com/cran/AutoGenAI |
Provider-agnostic tools for comparing and optimizing generative AI configurations with offline reproducible workflows.
The package focuses on joint prompt-model-strategy evaluation, Pareto screening, robustness testing, routing, ensembles, and deployment constraints.
optimize_ai, autogenai_example
Provides exact-text consensus utilities for repeated generations and multiple providers.
consensus_vote(outputs, weights = NULL) ai_ensemble(providers, prompt, input, params = list(), method = c("majority", "weighted"), weights = NULL) self_consistency(provider, prompt, input, params = list(), samples = 3L)consensus_vote(outputs, weights = NULL) ai_ensemble(providers, prompt, input, params = list(), method = c("majority", "weighted"), weights = NULL) self_consistency(provider, prompt, input, params = list(), samples = 3L)
outputs |
Text outputs to combine. |
weights |
Optional numeric voting weights. |
providers |
Provider list. |
prompt, input, params
|
Generation inputs. |
method |
Majority or weighted voting. |
provider |
Single provider for repeated sampling. |
samples |
Number of repeated generations. |
Consensus text or a list containing consensus and member outputs.
Creates provider objects. Real services are connected by user-supplied generation functions; mock providers require no network access.
ai_provider( name, generate, input_cost_per_1k = 0, output_cost_per_1k = 0, metadata = list() ) mock_provider( name = "mock-balanced", profile = c("balanced", "strict", "cheap"), seed = 1L )ai_provider( name, generate, input_cost_per_1k = 0, output_cost_per_1k = 0, metadata = list() ) mock_provider( name = "mock-balanced", profile = c("balanced", "strict", "cheap"), seed = 1L )
name |
Provider name. |
generate |
Function with arguments |
input_cost_per_1k, output_cost_per_1k
|
Estimated monetary cost per 1,000 tokens. |
metadata |
Optional provider metadata. |
profile |
Offline mock behavior profile. |
seed |
Integer seed stored with the mock provider. |
An object of class autogen_provider.
p <- mock_provider() p p$generate("Classify sentiment", "I love this", list(temperature = 0))p <- mock_provider() p p$generate("Classify sentiment", "I love this", list(temperature = 0))
Defines a provider-independent task. The default scorer performs case-insensitive exact matching after whitespace normalization.
ai_task( name, instruction, scorer = default_scorer, structured = c("none", "json"), metadata = list() ) default_scorer(prediction, truth, ...)ai_task( name, instruction, scorer = default_scorer, structured = c("none", "json"), metadata = list() ) default_scorer(prediction, truth, ...)
name |
Task name. |
instruction |
Base task instruction. |
scorer |
Function mapping prediction and truth to a numeric quality score. |
structured |
Whether JSON output is expected. |
metadata |
Optional metadata. |
prediction |
Predicted text. |
truth |
Reference value. |
... |
Additional arguments ignored by the default scorer. |
ai_task() returns an autogen_task; default_scorer() returns a numeric score.
Returns a small sentiment benchmark, a task, three deterministic mock providers, and candidate prompts.
autogenai_example()autogenai_example()
A list with task, data, providers, and prompts.
ex <- autogenai_example() names(ex)ex <- autogenai_example() names(ex)
Print, summarize, plot, and predict with an optimized AutoGenAI configuration.
## S3 method for class 'autogenai' print(x, ...) ## S3 method for class 'autogenai' summary(object, ...) ## S3 method for class 'autogenai' plot(x, ...) ## S3 method for class 'autogenai' predict(object, newdata, providers = NULL, abstain_threshold = NULL, ...)## S3 method for class 'autogenai' print(x, ...) ## S3 method for class 'autogenai' summary(object, ...) ## S3 method for class 'autogenai' plot(x, ...) ## S3 method for class 'autogenai' predict(object, newdata, providers = NULL, abstain_threshold = NULL, ...)
x, object
|
An object returned by |
... |
Additional arguments passed to plotting methods where applicable. |
newdata |
A data frame with |
providers |
Provider list containing the selected provider. |
abstain_threshold |
Optional minimum benchmark quality required for prediction. |
The print and plot methods return the object invisibly; summary returns a list; predict returns character outputs.
Evaluates one or many prompt-provider-generation configurations while recording quality, latency, cost, token estimates, and success.
evaluate_config(task, data, provider, prompt, temperature = 0, strategy = c("single", "self_consistency"), samples = 1L, scorer = NULL) benchmark_ai(task, data, providers, prompts, temperatures = 0, strategies = "single", samples = 1L, scorer = NULL) optimize_generation(task, data, providers, prompts, temperatures = c(0, 0.2, 0.7), strategies = c("single", "self_consistency"), samples = c(1L, 3L), scorer = NULL)evaluate_config(task, data, provider, prompt, temperature = 0, strategy = c("single", "self_consistency"), samples = 1L, scorer = NULL) benchmark_ai(task, data, providers, prompts, temperatures = 0, strategies = "single", samples = 1L, scorer = NULL) optimize_generation(task, data, providers, prompts, temperatures = c(0, 0.2, 0.7), strategies = c("single", "self_consistency"), samples = c(1L, 3L), scorer = NULL)
task |
An |
data |
Data frame containing |
provider, providers
|
One provider or a list of providers. |
prompt, prompts
|
Prompt text or candidate prompts. |
temperature, temperatures
|
Generation temperature values passed to providers. |
strategy, strategies
|
Single generation or self-consistency. |
samples |
Number of repeated generations for self-consistency. |
scorer |
Optional scorer overriding the task scorer. |
evaluate_config() returns row-level results. benchmark_ai() and optimize_generation() return lists with raw and summarized results.
ex <- autogenai_example() b <- benchmark_ai(ex$task, ex$data, ex$providers, ex$prompts[1:2]) head(b$summary)ex <- autogenai_example() b <- benchmark_ai(ex$task, ex$data, ex$providers, ex$prompts[1:2]) head(b$summary)
Jointly evaluates prompt, provider, and generation settings and supports utility, Pareto, budget, latency, and abstention decisions.
optimize_ai(task, data, providers, prompts, temperatures = c(0, 0.2), strategies = c("single", "self_consistency"), samples = c(1L, 3L), objective = c(quality = 0.55, robustness = 0.15, cost = 0.15, latency = 0.10, structured = 0.05), method = c("grid", "random"), max_configs = Inf, scorer = NULL, robustness_perturbations = NULL, seed = 1L) pareto_ai(x, objectives = c(quality = "max", cost = "min", latency = "min")) select_configuration( x, preference = c("balanced", "highest_quality", "cheapest", "fastest") ) budget_optimize(x, max_cost, preference = c("balanced", "highest_quality")) latency_optimize(x, max_latency, preference = c("balanced", "highest_quality")) abstain_decision(score, threshold = 0.8)optimize_ai(task, data, providers, prompts, temperatures = c(0, 0.2), strategies = c("single", "self_consistency"), samples = c(1L, 3L), objective = c(quality = 0.55, robustness = 0.15, cost = 0.15, latency = 0.10, structured = 0.05), method = c("grid", "random"), max_configs = Inf, scorer = NULL, robustness_perturbations = NULL, seed = 1L) pareto_ai(x, objectives = c(quality = "max", cost = "min", latency = "min")) select_configuration( x, preference = c("balanced", "highest_quality", "cheapest", "fastest") ) budget_optimize(x, max_cost, preference = c("balanced", "highest_quality")) latency_optimize(x, max_latency, preference = c("balanced", "highest_quality")) abstain_decision(score, threshold = 0.8)
task, data, providers, prompts, temperatures, strategies, samples, scorer
|
See |
objective |
Named utility weights. Robustness is reserved for explicit stress testing and is not silently imputed. |
method |
Grid or random configuration search. |
max_configs |
Maximum configurations retained or randomly sampled. |
robustness_perturbations |
Optional perturbation types evaluated for every retained configuration and incorporated into the utility when a robustness weight is present. |
seed |
Random seed. |
x |
An |
objectives |
Named vector specifying whether each Pareto objective is maximized or minimized. |
preference |
Selection rule. |
max_cost |
Maximum mean cost per request. |
max_latency |
Maximum mean latency in seconds. |
score |
Reliability or quality score. |
threshold |
Acceptance threshold. |
optimize_ai() returns an object of class autogenai; selection functions return data frames or decision labels.
ex <- autogenai_example() fit <- optimize_ai(ex$task, ex$data, ex$providers, ex$prompts, temperatures = 0, strategies = "single") fit pareto_ai(fit) select_configuration(fit)ex <- autogenai_example() fit <- optimize_ai(ex$task, ex$data, ex$providers, ex$prompts, temperatures = 0, strategies = "single") fit pareto_ai(fit) select_configuration(fit)
Creates, mutates, crosses, and evolves prompts, and computes stability-oriented prompt and provider indices.
prompt_candidates(base_prompt, n = 6L) mutate_prompt(prompt, strength = 1L, seed = NULL) crossover_prompt(prompt_a, prompt_b, seed = NULL) evolve_prompt(task, data, providers, base_prompt, generations = 3L, population = 6L, temperature = 0, scorer = NULL, seed = 1L) prompt_portability(results, lambda = 0.25) model_generalization(results, category_col = "category", lambda = 0.25)prompt_candidates(base_prompt, n = 6L) mutate_prompt(prompt, strength = 1L, seed = NULL) crossover_prompt(prompt_a, prompt_b, seed = NULL) evolve_prompt(task, data, providers, base_prompt, generations = 3L, population = 6L, temperature = 0, scorer = NULL, seed = 1L) prompt_portability(results, lambda = 0.25) model_generalization(results, category_col = "category", lambda = 0.25)
base_prompt, prompt, prompt_a, prompt_b
|
Prompt text. |
n |
Number of candidate prompts. |
strength |
Number of mutation clauses added. |
seed |
Optional random seed. |
task, data, providers, temperature, scorer
|
Evaluation inputs. |
generations |
Number of evolutionary generations. |
population |
Population size. |
results |
AutoGenAI results or a compatible data frame. |
lambda |
Penalty applied to cross-group standard deviation. |
category_col |
Column defining task categories for model generalization. |
Text, candidate vectors, an evolution object, or ranked data frames depending on the function.
Applies reproducible text perturbations and measures performance under changed inputs.
perturb_text(x, type = c("whitespace", "case", "typo", "distractor", "prefix"), seed = 1L) stress_test(task, data, provider, prompt, perturbations = c("whitespace", "case", "typo", "distractor"), temperature = 0, scorer = NULL, seed = 1L) robustness_score(x)perturb_text(x, type = c("whitespace", "case", "typo", "distractor", "prefix"), seed = 1L) stress_test(task, data, provider, prompt, perturbations = c("whitespace", "case", "typo", "distractor"), temperature = 0, scorer = NULL, seed = 1L) robustness_score(x)
x |
Text or a stress-test result. |
type |
Perturbation type. |
seed |
Random seed. |
task, data, provider, prompt, temperature, scorer
|
Evaluation inputs. |
perturbations |
Perturbations to apply. |
Perturbed text, a stress-test data frame, or a numeric robustness score.
Learns a lightweight one-vs-rest logistic router from text-shape features and empirical provider winners.
train_router(log, input_col = "input", provider_col = "provider", quality_col = "quality", task_id_col = NULL) route_model(router, input, return_probabilities = FALSE) update_router(router, new_log) router_regret(log, chosen_col = "chosen_provider", provider_col = "provider", quality_col = "quality", task_id_col = "task_id")train_router(log, input_col = "input", provider_col = "provider", quality_col = "quality", task_id_col = NULL) route_model(router, input, return_probabilities = FALSE) update_router(router, new_log) router_regret(log, chosen_col = "chosen_provider", provider_col = "provider", quality_col = "quality", task_id_col = "task_id")
log |
Evaluation log containing inputs, providers, and quality. |
input_col, provider_col, quality_col, task_id_col
|
Column names. |
router |
Router returned by |
input |
New text inputs. |
return_probabilities |
Return normalized one-vs-rest probabilities together with selected providers. |
new_log |
Additional routing observations. |
chosen_col |
Column containing the provider chosen by a router. |
A router, provider names, updated router, or regret data frame.