Module: mellea.backends.watsonx

A generic WatsonX.ai compatible backend that wraps around the watson_machine_learning library.

Classes

class mellea.backends.watsonx.WatsonxAIBackend(model_id: str | ModelIdentifier = model_ids.IBM_GRANITE_3_3_8B, formatter: Formatter | None = None, base_url: str | None = None, model_options: dict | None = None, api_key: str | None = None, project_id: str | None = None, **kwargs)

A generic backend class for watsonx SDK.

Constructor

A generic watsonx backend that wraps around the ibm_watsonx_ai sdk.

Arguments

  • model_id: Model id. Defaults to model_ids.IBM_GRANITE_3_3_8B.
  • formatter: input formatter. Defaults to TemplateFormatter in init.
  • base_url: url for watson ML deployment. Defaults to env(WATSONX_URL).
  • model_options: Global model options to pass to the model. Defaults to None.
  • api_key: watsonx API key. Defaults to None.
  • project_id: watsonx project ID. Defaults to None.

Methods

mellea.backends.watsonx.WatsonxAIBackend._get_watsonx_model_id()
Gets the watsonx model id from the model_id that was provided in the constructor. Raises AssertionError if the ModelIdentifier does not provide a watsonx_name.
mellea.backends.watsonx.WatsonxAIBackend.filter_chat_completions_kwargs(model_options: dict)
Filter kwargs to only include valid watsonx chat.completions.create parameters.
mellea.backends.watsonx.WatsonxAIBackend._simplify_and_merge(model_options: dict[str, Any] | None, is_chat_context: bool)
Simplifies model_options to use the Mellea specific ModelOption.Option and merges the backend’s model_options with those passed into this call. Rules:
  • Within a model_options dict, existing keys take precedence. This means remapping to mellea specific keys will maintain the value of the mellea specific key if one already exists.
  • When merging, the keys/values from the dictionary passed into this function take precedence.
Because this function simplifies and then merges, non-Mellea keys from the passed in model_options will replace Mellea specific keys from the backend’s model_options.

Arguments

  • model_options: the model_options for this call
a new dict
mellea.backends.watsonx.WatsonxAIBackend._make_backend_specific_and_remove(model_options: dict[str, Any], is_chat_context: bool)
Maps specified Mellea specific keys to their backend specific version and removes any remaining Mellea keys.

Arguments

  • model_options: the model_options for this call
a new dict
mellea.backends.watsonx.WatsonxAIBackend.generate_from_context(action: Component | CBlock, ctx: Context, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, generate_logs: list[GenerateLog] | None = None, tool_calls: bool = False)
See generate_from_chat_context.
mellea.backends.watsonx.WatsonxAIBackend.generate_from_chat_context(action: Component | CBlock, ctx: Context, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, generate_logs: list[GenerateLog] | None = None, tool_calls: bool = False)
Generates a new completion from the provided Context using this backend’s Formatter.
mellea.backends.watsonx.WatsonxAIBackend._generate_from_raw(actions: list[Component | CBlock], format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, generate_logs: list[GenerateLog] | None = None)
Generates a completion text. Gives the input provided to the model without templating.
mellea.backends.watsonx.WatsonxAIBackend._extract_model_tool_requests(tools: dict[str, Callable], chat_response: dict)