A programming framework for agentic AI
improve component config, add description support in dump_component (#5203)
Victor Dibia6359b6a7bece
diff --git a/python/packages/autogen-core/src/autogen_core/_component_config.py b/python/packages/autogen-core/src/autogen_core/_component_config.pyindex f64165703..ce768ddaf 100644--- a/python/packages/autogen-core/src/autogen_core/_component_config.py+++ b/python/packages/autogen-core/src/autogen_core/_component_config.py@@ -33,6 +33,9 @@ class ComponentModel(BaseModel):description: str | None = None"""Description of the component."""+ label: str | None = None+ """Human readable label for the component. If missing the component assumes the class name of the provider."""+config: dict[str, Any]"""The schema validated config field is passed to a given class's implmentation of :py:meth:`autogen_core.ComponentConfigImpl._from_config` to create a new instance of the component class."""@@ -95,6 +98,10 @@ class ComponentToConfig(Generic[ToConfigT]):"""The version of the component, if schema incompatibilities are introduced this should be updated."""component_provider_override: ClassVar[str | None] = None"""Override the provider string for the component. This should be used to prevent internal module names being a part of the module name."""+ component_description: ClassVar[str | None] = None+ """A description of the component. If not provided, the docstring of the class will be used."""+ component_label: ClassVar[str | None] = None+ """A human readable label for the component. If not provided, the component class name will be used."""def _to_config(self) -> ToConfigT:"""Dump the configuration that would be requite to create a new instance of a component matching the configuration of this instance.@@ -132,13 +139,22 @@ class ComponentToConfig(Generic[ToConfigT]):if not hasattr(self, "component_type"):raise AttributeError("component_type not defined")+ description = self.component_description+ if description is None and self.__class__.__doc__:+ # use docstring as description+ docstring = self.__class__.__doc__.strip()+ for marker in ["\n\nArgs:", "\n\nParameters:", "\n\nAttributes:", "\n\n"]:+ docstring = docstring.split(marker)[0]+ description = docstring.strip()+obj_config = self._to_config().model_dump(exclude_none=True)model = ComponentModel(provider=provider,component_type=self.component_type,version=self.component_version,component_version=self.component_version,- description=None,+ description=description,+ label=self.component_label or self.__class__.__name__,config=obj_config,)return modeldiff --git a/python/packages/autogen-core/tests/test_component_config.py b/python/packages/autogen-core/tests/test_component_config.pyindex 1f78e907a..36125d128 100644--- a/python/packages/autogen-core/tests/test_component_config.py+++ b/python/packages/autogen-core/tests/test_component_config.py@@ -33,6 +33,15 @@ class MyComponent(ComponentBase[MyConfig], Component[MyConfig]):return cls(info=config.info)+class ComponentWithDescription(MyComponent):+ component_description = "Explicit description"+ component_label = "Custom Component"+++class ComponentWithDocstring(MyComponent):+ """A component using just docstring."""++def test_custom_component() -> None:comp = MyComponent("test")comp2 = MyComponent.load_component(comp.dump_component())@@ -350,3 +359,12 @@ async def test_function_tool() -> None:cancelled_token.cancel()with pytest.raises(Exception, match="Cancelled"):await loaded_async.run_json({"x": 1.0, "y": 2.0}, cancelled_token)+++def test_component_descriptions() -> None:+ """Test different ways of setting component descriptions."""+ assert MyComponent("test").dump_component().description is None+ assert ComponentWithDocstring("test").dump_component().description == "A component using just docstring."+ assert ComponentWithDescription("test").dump_component().description == "Explicit description"+ assert ComponentWithDescription("test").dump_component().label == "Custom Component"