Skip to content

Models

Models (llm_configs) exporter.

ModelsExporter

ModelsExporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any
)

Bases: Exporter[ModelExtras]

Mdels exporter with structured extras.

Source code in waldiez/exporting/models/exporter.py
def __init__(
    self,
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any,
):
    """Initialize the models exporter."""
    super().__init__(context, **kwargs)

    self.flow_name = flow_name
    self.agents = agents
    self.agent_names = agent_names
    self.models = models
    self.model_names = model_names
    self.for_notebook = for_notebook
    self.cache_seed = cache_seed
    self.output_dir = Path(output_dir) if output_dir else None

    # Initialize extras with processed model content
    self._extras = self._create_model_extras()

extras property

extras: ModelExtras

Get the model extras.

generate_main_content

generate_main_content() -> Optional[str]

Generate the main models content (LLM configs).

Returns:

TypeDescription
Optional[str]

The main content string, or None if no content is available.

Source code in waldiez/exporting/models/exporter.py
def generate_main_content(self) -> Optional[str]:
    """Generate the main models content (LLM configs).

    Returns
    -------
    Optional[str]
        The main content string, or None if no content is available.
    """
    # handled in extras._contribute_specific_content(...)
    # also here for direct access
    if self.extras.has_specific_content():
        return self.extras.get_content()
    return None

get_agent_llm_config_arg

get_agent_llm_config_arg(agent: WaldiezAgent) -> str

Get LLM config argument for agent.

Parameters:

NameTypeDescriptionDefault
agentWaldiezAgent

The agent for which to get the LLM config argument.

required

Returns:

TypeDescription
str

The LLM config argument string for the agent, or "llm_config=False" if no models are configured.

Source code in waldiez/exporting/models/exporter.py
def get_agent_llm_config_arg(self, agent: WaldiezAgent) -> str:
    """Get LLM config argument for agent.

    Parameters
    ----------
    agent : WaldiezAgent
        The agent for which to get the LLM config argument.

    Returns
    -------
    str
        The LLM config argument string for the agent,
        or "llm_config=False" if no models are configured.
    """
    if not agent.data.model_ids:
        return "    llm_config=False,  # pyright: ignore\n"

    # Get model configs for this agent
    model_configs: list[str] = []
    for model_id in agent.data.model_ids:
        model_name = self.model_names.get(model_id)
        if model_name:
            model_configs.append(f"{model_name}_llm_config")
    tab = " " * 4
    if not model_configs:
        return f"{tab}llm_config=False, # pyright: ignore\n"

    config_list = f",\n{tab}{tab}{tab}".join(model_configs)
    llm_config = f"""{tab}llm_config=autogen.LLMConfig(
    config_list=[
        {config_list},
    ]"""
    #
    # Add cache seed if provided
    if self.cache_seed is not None:
        llm_config += f",\n{tab}{tab}cache_seed={self.cache_seed},\n"
    else:
        llm_config += f",\n{tab}{tab}cache_seed=None,\n"
    llm_config += f"{tab}),\n"

    return llm_config

get_api_key_loader_script

get_api_key_loader_script() -> str

Get the api key loader script.

Returns:

TypeDescription
str

The api key loader script.

Source code in waldiez/exporting/models/exporter.py
    def get_api_key_loader_script(self) -> str:
        """Get the api key loader script.

        Returns
        -------
        str
            The api key loader script.
        """
        comment = get_comment(
            "Load model API keys",
            for_notebook=self.config.for_notebook,
        )
        loader_script = f'''{comment}# NOTE:
# This section assumes that a file named "{self.flow_name}_api_keys"
# exists in the same directory as this file.
# This file contains the API keys for the models used in this flow.
# It should be .gitignored and not shared publicly.
# If this file is not present, you can either create it manually
# or change the way API keys are loaded in the flow.


def load_api_key_module(flow_name: str) -> ModuleType:
    """Load the api key module.

    Parameters
    ----------
    flow_name : str
        The flow name.

    Returns
    -------
    ModuleType
        The api keys loading module.
    """
    module_name = f"{{flow_name}}_api_keys"
    if module_name in sys.modules:
        return importlib.reload(sys.modules[module_name])
    return importlib.import_module(module_name)

__MODELS_MODULE__ = load_api_key_module("{self.flow_name}")


def get_{self.flow_name}_model_api_key(model_name: str) -> str:
    """Get the model api key.
    Parameters
    ----------
    model_name : str
        The model name.

    Returns
    -------
    str
        The model api key.
    """
    return __MODELS_MODULE__.get_{self.flow_name}_model_api_key(model_name)

'''
        return loader_script

create_models_exporter

create_models_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ModelsExporter

Create a models exporter.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use models.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
modelslist[WaldiezModel]

The models to export.

required
model_namesdict[str, str]

Mapping of model IDs to names.

required
for_notebookbool

Whether the export is for a notebook, by default False

False
cache_seedOptional[int]

The cache seed if any, by default None

None
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None

Returns:

TypeDescription
ModelsExporter

The created models exporter.

Source code in waldiez/exporting/models/factory.py
def create_models_exporter(
    # Factory function for models exporter creation
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ModelsExporter:
    """Create a models exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use models.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    models : list[WaldiezModel]
        The models to export.
    model_names : dict[str, str]
        Mapping of model IDs to names.
    for_notebook : bool, optional
        Whether the export is for a notebook, by default False
    cache_seed : Optional[int], optional
        The cache seed if any, by default None
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None

    Returns
    -------
    ModelsExporter
        The created models exporter.
    """
    if context is None:
        context = get_default_exporter_context()
    return ModelsExporter(
        flow_name=flow_name,
        agents=agents,
        agent_names=agent_names,
        models=models,
        model_names=model_names,
        for_notebook=for_notebook,
        cache_seed=cache_seed,
        output_dir=output_dir,
        context=context,
    )

exporter

Models exporter module.

ModelsExporter

ModelsExporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any
)

Bases: Exporter[ModelExtras]

Mdels exporter with structured extras.

Source code in waldiez/exporting/models/exporter.py
def __init__(
    self,
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any,
):
    """Initialize the models exporter."""
    super().__init__(context, **kwargs)

    self.flow_name = flow_name
    self.agents = agents
    self.agent_names = agent_names
    self.models = models
    self.model_names = model_names
    self.for_notebook = for_notebook
    self.cache_seed = cache_seed
    self.output_dir = Path(output_dir) if output_dir else None

    # Initialize extras with processed model content
    self._extras = self._create_model_extras()

extras property

extras: ModelExtras

Get the model extras.

generate_main_content

generate_main_content() -> Optional[str]

Generate the main models content (LLM configs).

Returns:

TypeDescription
Optional[str]

The main content string, or None if no content is available.

Source code in waldiez/exporting/models/exporter.py
def generate_main_content(self) -> Optional[str]:
    """Generate the main models content (LLM configs).

    Returns
    -------
    Optional[str]
        The main content string, or None if no content is available.
    """
    # handled in extras._contribute_specific_content(...)
    # also here for direct access
    if self.extras.has_specific_content():
        return self.extras.get_content()
    return None

get_agent_llm_config_arg

get_agent_llm_config_arg(agent: WaldiezAgent) -> str

Get LLM config argument for agent.

Parameters:

NameTypeDescriptionDefault
agentWaldiezAgent

The agent for which to get the LLM config argument.

required

Returns:

TypeDescription
str

The LLM config argument string for the agent, or "llm_config=False" if no models are configured.

Source code in waldiez/exporting/models/exporter.py
def get_agent_llm_config_arg(self, agent: WaldiezAgent) -> str:
    """Get LLM config argument for agent.

    Parameters
    ----------
    agent : WaldiezAgent
        The agent for which to get the LLM config argument.

    Returns
    -------
    str
        The LLM config argument string for the agent,
        or "llm_config=False" if no models are configured.
    """
    if not agent.data.model_ids:
        return "    llm_config=False,  # pyright: ignore\n"

    # Get model configs for this agent
    model_configs: list[str] = []
    for model_id in agent.data.model_ids:
        model_name = self.model_names.get(model_id)
        if model_name:
            model_configs.append(f"{model_name}_llm_config")
    tab = " " * 4
    if not model_configs:
        return f"{tab}llm_config=False, # pyright: ignore\n"

    config_list = f",\n{tab}{tab}{tab}".join(model_configs)
    llm_config = f"""{tab}llm_config=autogen.LLMConfig(
    config_list=[
        {config_list},
    ]"""
    #
    # Add cache seed if provided
    if self.cache_seed is not None:
        llm_config += f",\n{tab}{tab}cache_seed={self.cache_seed},\n"
    else:
        llm_config += f",\n{tab}{tab}cache_seed=None,\n"
    llm_config += f"{tab}),\n"

    return llm_config

get_api_key_loader_script

get_api_key_loader_script() -> str

Get the api key loader script.

Returns:

TypeDescription
str

The api key loader script.

Source code in waldiez/exporting/models/exporter.py
    def get_api_key_loader_script(self) -> str:
        """Get the api key loader script.

        Returns
        -------
        str
            The api key loader script.
        """
        comment = get_comment(
            "Load model API keys",
            for_notebook=self.config.for_notebook,
        )
        loader_script = f'''{comment}# NOTE:
# This section assumes that a file named "{self.flow_name}_api_keys"
# exists in the same directory as this file.
# This file contains the API keys for the models used in this flow.
# It should be .gitignored and not shared publicly.
# If this file is not present, you can either create it manually
# or change the way API keys are loaded in the flow.


def load_api_key_module(flow_name: str) -> ModuleType:
    """Load the api key module.

    Parameters
    ----------
    flow_name : str
        The flow name.

    Returns
    -------
    ModuleType
        The api keys loading module.
    """
    module_name = f"{{flow_name}}_api_keys"
    if module_name in sys.modules:
        return importlib.reload(sys.modules[module_name])
    return importlib.import_module(module_name)

__MODELS_MODULE__ = load_api_key_module("{self.flow_name}")


def get_{self.flow_name}_model_api_key(model_name: str) -> str:
    """Get the model api key.
    Parameters
    ----------
    model_name : str
        The model name.

    Returns
    -------
    str
        The model api key.
    """
    return __MODELS_MODULE__.get_{self.flow_name}_model_api_key(model_name)

'''
        return loader_script

factory

Factory function for creating a ModelsExporter instance.

create_models_exporter

create_models_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ModelsExporter

Create a models exporter.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use models.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
modelslist[WaldiezModel]

The models to export.

required
model_namesdict[str, str]

Mapping of model IDs to names.

required
for_notebookbool

Whether the export is for a notebook, by default False

False
cache_seedOptional[int]

The cache seed if any, by default None

None
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None

Returns:

TypeDescription
ModelsExporter

The created models exporter.

Source code in waldiez/exporting/models/factory.py
def create_models_exporter(
    # Factory function for models exporter creation
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    models: list[WaldiezModel],
    model_names: dict[str, str],
    for_notebook: bool = False,
    cache_seed: Optional[int] = None,
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ModelsExporter:
    """Create a models exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use models.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    models : list[WaldiezModel]
        The models to export.
    model_names : dict[str, str]
        Mapping of model IDs to names.
    for_notebook : bool, optional
        Whether the export is for a notebook, by default False
    cache_seed : Optional[int], optional
        The cache seed if any, by default None
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None

    Returns
    -------
    ModelsExporter
        The created models exporter.
    """
    if context is None:
        context = get_default_exporter_context()
    return ModelsExporter(
        flow_name=flow_name,
        agents=agents,
        agent_names=agent_names,
        models=models,
        model_names=model_names,
        for_notebook=for_notebook,
        cache_seed=cache_seed,
        output_dir=output_dir,
        context=context,
    )

processor

Model exporting utilities for Waldiez.

ModelProcessingResult dataclass

ModelProcessingResult(
    llm_configs_content: str = "",
    api_keys_file: Optional[Path] = None,
    needs_api_key_loader: bool = False,
)

Result from processing models.

ModelProcessor

ModelProcessor(
    flow_name: str,
    models: list[WaldiezModel],
    model_names: dict[str, str],
    serializer: Optional[Serializer] = None,
    output_dir: Optional[Path] = None,
)

Model processor for generating LLM configs and API key loading.

Source code in waldiez/exporting/models/processor.py
def __init__(
    self,
    flow_name: str,
    models: list[WaldiezModel],
    model_names: dict[str, str],
    serializer: Optional[Serializer] = None,
    output_dir: Optional[Path] = None,
):
    self.flow_name = flow_name
    self.models = models
    self.model_names = model_names
    self.serializer = serializer or DefaultSerializer()
    self.output_dir = output_dir

process

process() -> str

Process the flow models.

Returns:

TypeDescription
str

The string representation of all models' LLM configs.

Source code in waldiez/exporting/models/processor.py
def process(self) -> str:
    """Process the flow models.

    Returns
    -------
    str
        The string representation of all models' LLM configs.
    """
    content = ""
    for model in self.models:
        model_name = self.model_names[model.id]
        model_config = model.get_llm_config()

        # Remove api_key if present
        api_key = model_config.pop("api_key", None)
        model_dict_str = self.serializer.serialize(model_config, tabs=0)
        # and use the getter function to get it when needed
        if api_key:  # pragma: no branch
            extra_arg = (
                f'get_{self.flow_name}_model_api_key("{model_name}")'
            )
            # remove the \n}, from the end of the dict string
            model_dict_str = model_dict_str.rstrip("\n},")
            model_dict_str += f',\n    "api_key": {extra_arg}\n}}'
        content += (
            f"\n{model_name}_llm_config: dict[str, Any] = "
            f"{model_dict_str}\n"
        )

    # Write API keys file if output directory provided
    if self.output_dir:
        self.output_dir = Path(self.output_dir)
        self.output_dir.mkdir(parents=True, exist_ok=True)
        self._write_api_keys()

    return content