Predictors
Module for LLM predictors.
get_predictor(downstream_llm=None, type='first_occurrence', *args, **kwargs)
Factory function to create and return a predictor instance.
This function supports three types of predictors: 1. DummyPredictor: A mock predictor for testing purposes when no downstream_llm is provided. 2. FirstOccurrenceClassificator: A predictor that classifies based on first occurrence of the label. 3. MarkerBasedClassificator: A predictor that classifies based on a marker.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
downstream_llm
|
The language model to use for prediction. If None, returns a DummyPredictor. |
None
|
|
type
|
Literal['first_occurrence', 'marker']
|
The type of predictor to create: - "first_occurrence" (default) for FirstOccurrenceClassificator - "marker" for MarkerBasedClassificator |
'first_occurrence'
|
*args
|
Variable length argument list passed to the predictor constructor. |
()
|
|
**kwargs
|
Arbitrary keyword arguments passed to the predictor constructor. |
{}
|
Returns:
Type | Description |
---|---|
An instance of DummyPredictor, FirstOccurrenceClassificator, or MarkerBasedClassificator. |
Source code in promptolution/predictors/__init__.py
base_predictor
Base module for predictors.
BasePredictor
Abstract base class for predictors in the promptolution library.
This class defines the interface that all concrete predictor implementations should follow.
Attributes:
Name | Type | Description |
---|---|---|
llm |
The language model used for generating predictions. |
Methods:
Name | Description |
---|---|
predict |
An abstract method that should be implemented by subclasses to make predictions based on prompts and input data. |
Source code in promptolution/predictors/base_predictor.py
__init__(llm)
Initialize the BasePredictor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
BaseLLM
|
The language model to use for predictions. |
required |
classes
|
List[str]
|
The list of valid class labels. |
required |
predict(prompts, xs, return_seq=False)
Abstract method to make predictions based on prompts and input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompts
|
List[str]
|
List of prompts to use for prediction. |
required |
xs
|
ndarray
|
Array of input data. |
required |
return_seq
|
bool
|
whether to return the generating sequence |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Array of predictions. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If not implemented by a subclass. |
Source code in promptolution/predictors/base_predictor.py
DummyPredictor
Bases: BasePredictor
A dummy predictor implementation for testing purposes.
This predictor generates random predictions from the list of possible classes.
Attributes:
Name | Type | Description |
---|---|---|
model_id |
str
|
Always set to "dummy". |
classes |
List[str]
|
List of possible class labels. |
Methods:
Name | Description |
---|---|
predict |
Generates random predictions for the given prompts and input data. |
Source code in promptolution/predictors/base_predictor.py
__init__(model_id, classes, *args, **kwargs)
Initialize the DummyPredictor.
Parameters
model_id : str Model identifier string. classes : list List of possible class labels.
Source code in promptolution/predictors/base_predictor.py
predict(prompts, xs)
Generate random predictions for the given prompts and input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompts
|
List[str]
|
List of prompts (ignored in this implementation). |
required |
xs
|
ndarray
|
Array of input data (only the length is used). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: 2D array of random predictions, shape (len(prompts), len(xs)). |
Source code in promptolution/predictors/base_predictor.py
classificator
Module for classification predictors.
FirstOccurrenceClassificator
Bases: BasePredictor
A predictor class for classification tasks using language models.
This class takes a language model and a list of classes, and provides a method to predict classes for given prompts and input data. The class labels are extracted by matching the words in the prediction with the list of valid class labels. The first occurrence of a valid class label in the prediction is used as the predicted class. If no valid class label is found, the first class label in the list is used as the default prediction.
Attributes:
Name | Type | Description |
---|---|---|
llm |
The language model used for generating predictions. |
|
classes |
List[str]
|
The list of valid class labels. |
Inherits from
BasePredictor: The base class for predictors in the promptolution library.
Source code in promptolution/predictors/classificator.py
__init__(llm, classes, *args, **kwargs)
Initialize the Classificator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
The language model to use for predictions. |
required | |
classes
|
List[str]
|
The list of valid class labels. |
required |
Source code in promptolution/predictors/classificator.py
MarkerBasedClassificator
Bases: BasePredictor
A predictor class for classification tasks using language models.
This class takes a language model and a list of classes, and provides a method to predict classes for given prompts and input data. The class labels are extracted.
Attributes:
Name | Type | Description |
---|---|---|
llm |
The language model used for generating predictions. |
|
classes |
List[str]
|
The list of valid class labels. |
marker |
str
|
The marker to use for extracting the class label. |
Inherits from
BasePredictor: The base class for predictors in the promptolution library.
Source code in promptolution/predictors/classificator.py
__init__(llm, classes=None, begin_marker='<final_answer>', end_marker='</final_answer>', *args, **kwargs)
Initialize the Classificator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
The language model to use for predictions. |
required | |
classes
|
List[str]
|
The list of valid class labels. If None, does not force any class. |
None
|
begin_marker
|
str
|
The marker to use for extracting the class label. |
'<final_answer>'
|
end_marker
|
str
|
The marker to use for extracting the class label. |
'</final_answer>'
|
*args,
|
**kwargs
|
Additional arguments for the BasePredictor. |
required |