Helpers
Helper functions for the usage of the libary.
get_exemplar_selector(name, task, predictor)
Factory function to get an exemplar selector based on the given name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
The name of the exemplar selector to instantiate. |
required |
task
|
BaseTask
|
The task object to be passed to the selector. |
required |
predictor
|
BasePredictor
|
The predictor object to be passed to the selector. |
required |
Returns:
Name | Type | Description |
---|---|---|
BaseExemplarSelector |
BaseExemplarSelector
|
An instance of the requested exemplar selector. |
Raises:
Type | Description |
---|---|
ValueError
|
If the requested selector name is not found. |
Source code in promptolution/helpers.py
get_llm(model_id=None, config=None)
Factory function to create and return a language model instance based on the provided model_id.
This function supports three types of language models: 1. LocalLLM: For running models locally. 2. VLLM: For running models using the vLLM library. 3. APILLM: For API-based models (default if not matching other types).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_id
|
str
|
Identifier for the model to use. Special cases: - "local-{model_name}" for LocalLLM - "vllm-{model_name}" for VLLM - Any other string for APILLM |
None
|
config
|
ExperimentConfig
|
"ExperimentConfig" overwriting defaults. |
None
|
Returns:
Type | Description |
---|---|
BaseLLM
|
An instance of LocalLLM, or APILLM based on the model_id. |
Source code in promptolution/helpers.py
get_optimizer(predictor, meta_llm, task, optimizer=None, meta_prompt=None, task_description=None, config=None)
Creates and returns an optimizer instance based on provided parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predictor
|
BasePredictor
|
The predictor used for prompt evaluation |
required |
meta_llm
|
BaseLLM
|
The language model used for generating meta-prompts |
required |
task
|
BaseTask
|
The task object used for evaluating prompts |
required |
optimizer
|
Literal['evopromptde', 'evopromptga', 'opro']
|
String identifying which optimizer to use |
None
|
meta_prompt
|
str
|
Meta prompt text for the optimizer |
None
|
task_description
|
str
|
Description of the task for the optimizer |
None
|
config
|
ExperimentConfig
|
Configuration object with default parameters |
None
|
Returns:
Type | Description |
---|---|
BaseOptimizer
|
An optimizer instance |
Raises:
Type | Description |
---|---|
ValueError
|
If an unknown optimizer type is specified |
Source code in promptolution/helpers.py
get_predictor(downstream_llm=None, type='marker', *args, **kwargs)
Factory function to create and return a predictor instance.
This function supports three types of predictors: 1. FirstOccurrenceClassifier: A predictor that classifies based on first occurrence of the label. 2. MarkerBasedClassifier: A predictor that classifies based on a marker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
downstream_llm
|
The language model to use for prediction. |
None
|
|
type
|
Literal['first_occurrence', 'marker']
|
The type of predictor to create: - "first_occurrence" for FirstOccurrenceClassifier - "marker" (default) for MarkerBasedClassifier |
'marker'
|
*args
|
Variable length argument list passed to the predictor constructor. |
()
|
|
**kwargs
|
Arbitrary keyword arguments passed to the predictor constructor. |
{}
|
Returns:
Type | Description |
---|---|
BasePredictor
|
An instance of FirstOccurrenceClassifier or MarkerBasedClassifier. |
Source code in promptolution/helpers.py
get_task(df, config)
Get the task based on the provided DataFrame and configuration.
So far only ClassificationTask is supported.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
config
|
ExperimentConfig
|
Configuration for the experiment. |
required |
Returns:
Name | Type | Description |
---|---|---|
BaseTask |
BaseTask
|
An instance of a task class based on the provided DataFrame and configuration. |
Source code in promptolution/helpers.py
run_evaluation(df, config, prompts)
Run the evaluation phase of the experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
config
|
Config
|
Configuration object for the experiment. |
required |
prompts
|
List[str]
|
List of prompts to evaluate. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing the prompts and their scores. |
Source code in promptolution/helpers.py
run_experiment(df, config)
Run a full experiment based on the provided configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Input DataFrame containing the data. |
required |
config
|
Config
|
Configuration object for the experiment. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame: A DataFrame containing the prompts and their scores. |
Source code in promptolution/helpers.py
run_optimization(df, config)
Run the optimization phase of the experiment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
Config
|
Configuration object for the experiment. |
required |
Returns:
Type | Description |
---|---|
List[str]
|
List[str]: The optimized list of prompts. |