LLMRecommender

class baybe.recommenders.pure.llm.llm.LLMRecommender[source]

Bases: PureRecommender, SerialMixin

Recommender 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

allow_recommending_already_measured

Deprecated!

allow_recommending_pending_experiments

Deprecated!

allow_repeated_recommendations

Deprecated!

compatibility

Class variable reflecting the search space compatibility.

supports_discrete_subset_generating_constraints

Class variable indicating whether the recommender supports discrete subset-generating constraints.

model

The LiteLLM model identifier to use for recommendations.

experiment_description

Textual description of the experiment.

litellm_args

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_dict(dictionary: dict)

Create an object from its dictionary representation.

Parameters:

dictionary (dict) – The dictionary representation.

Return type:

TypeVar(_T, bound= SerialMixin)

Returns:

The reconstructed object.

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 Path object pointing to a JSON file.

  • A file-like object with a read() method.

Raises:

ValueError – If source is 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_size is smaller than 1.

to_dict()

Create an object’s dictionary representation.

Return type:

dict

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 Path object 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 if sink is a file path or Path object.

  • **kwargs (Any) – Additional keyword arguments to pass to json.dumps().

Raises:

FileExistsError – If sink points to an already existing file but overwrite is False.

Return type:

str

Returns:

The JSON representation as a string.

property allow_recommending_already_measured: NoReturn

Deprecated!

property allow_recommending_pending_experiments: NoReturn

Deprecated!

property allow_repeated_recommendations: NoReturn

Deprecated!

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.metadata on the objective passed to recommend() 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).

model: str

The LiteLLM model identifier to use for recommendations.

supports_discrete_subset_generating_constraints: ClassVar[bool] = True

Class variable indicating whether the recommender supports discrete subset-generating constraints.