LLMRecommender¶
- class baybe.recommenders.pure.llm.llm.LLMRecommender[source]¶
Bases:
PureRecommender,SerialMixinRecommender that uses a language model to suggest new experimental points.
Public methods
__init__(model, experiment_description[, ...])Method generated by attrs for class LLMRecommender.
from_dict(dictionary)Create an object from its dictionary representation.
from_json(source, /)Create an object from its JSON representation.
recommend(batch_size, searchspace[, ...])Generate recommendations using the language model.
to_dict()Create an object's dictionary representation.
to_json([sink, overwrite])Create an object's JSON representation.
Public attributes and properties
Deprecated!
Deprecated!
Deprecated!
Class variable reflecting the search space compatibility.
Class variable indicating whether the recommender supports discrete subset-generating constraints.
The LiteLLM model identifier to use for recommendations.
Textual description of the experiment.
Additional arguments to pass to LiteLLM (e.g.
temperature,max_tokens).- __init__(model: str, experiment_description: str, litellm_args=NOTHING, *, allow_repeated_recommendations: bool | None = None, allow_recommending_already_measured: bool | None = None, allow_recommending_pending_experiments: bool | None = None)¶
Method generated by attrs for class LLMRecommender.
For details on the parameters, see Public attributes and properties.
- classmethod from_json(source: str | Path | SupportsRead[str], /)¶
Create an object from its JSON representation.
- Parameters:
source (
Union[str,Path,SupportsRead[str]]) –The JSON source. Can be:
A string containing JSON content.
A file path or
Pathobject pointing to a JSON file.A file-like object with a
read()method.
- Raises:
ValueError – If
sourceis not one of the allowed types.- Return type:
TypeVar(_T, bound= SerialMixin)- Returns:
The reconstructed object.
- recommend(batch_size: int, searchspace: SearchSpace, objective: Objective | None = None, measurements: DataFrame | None = None, pending_experiments: DataFrame | None = None)[source]¶
Generate recommendations using the language model.
- Parameters:
batch_size (
int) – The number of recommendations to generate.searchspace (
SearchSpace) – The search space to generate recommendations for.objective (
Optional[Objective]) – Optional objective to include in the prompt.measurements (
Optional[DataFrame]) – Optional measurements to include in the prompt.pending_experiments (
Optional[DataFrame]) – Optional pending experiments to include in the prompt.
- Return type:
DataFrame- Returns:
A DataFrame containing the recommendations as individual rows.
- Raises:
IncompatibilityError – If the search space or objective contains custom components that cannot be described to the language model.
LLMAuthenticationError – If authentication with the provider fails.
LLMCallError – If the call to the language model fails (e.g. network, rate limiting, timeout) before a response is produced.
LLMResponseError – If the response cannot be turned into a valid recommendation batch even after a recovery attempt.
ValueError – If
batch_sizeis smaller than 1.
- to_dict()¶
Create an object’s dictionary representation.
- Return type:
- Returns:
The dictionary representation of the object.
- to_json(sink: str | Path | SupportsWrite[str] | None = None, /, *, overwrite: bool = False, **kwargs: Any)¶
Create an object’s JSON representation.
- Parameters:
sink (
Union[str,Path,SupportsWrite[str],None]) –The JSON sink. Can be:
None(only returns the JSON string).A file path or
Pathobject pointing to a location where to write the JSON content.A file-like object with a
write()method.
overwrite (
bool) – Boolean flag indicating if to overwrite the file if it already exists. Only relevant ifsinkis a file path orPathobject.**kwargs (
Any) – Additional keyword arguments to pass tojson.dumps().
- Raises:
FileExistsError – If
sinkpoints to an already existing file butoverwriteisFalse.- Return type:
- Returns:
The JSON representation as a string.
-
compatibility:
ClassVar[SearchSpaceType] = 'HYBRID'¶ Class variable reflecting the search space compatibility.
-
experiment_description:
str¶ Textual description of the experiment.
For best results, also set
baybe.objectives.base.Objective.metadataon the objective passed torecommend()to give the language model a description of what to optimize and per-target context such as units.
-
litellm_args:
dict[str,Any]¶ Additional arguments to pass to LiteLLM (e.g.
temperature,max_tokens).API credentials must not be passed here — they would be stored in plain text and appear in logs and serialized campaign files. Configure them via the environment variables that LiteLLM reads automatically based on the model prefix (e.g.
OPENAI_API_KEY,ANTHROPIC_API_KEY).