A programming framework for agentic AI
fix: Update SK kernel from tool to use method. (#5469)
Leonardo Pinheiro50d7587a4649
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py b/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.pyindex 26e2092dc..0067ae30b 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/semantic_kernel/_sk_chat_completion_adapter.py@@ -67,7 +67,7 @@ class SKChatCompletionAdapter(ChatCompletionClient):Examples:- Anthropic models:+ Anthropic models with function calling:.. code-block:: bash@@ -79,13 +79,19 @@ class SKChatCompletionAdapter(ChatCompletionClient):import osfrom autogen_agentchat.agents import AssistantAgent- from autogen_core.models import UserMessage+ from autogen_agentchat.ui import Console+ from autogen_core.models import ModelFamily, UserMessagefrom autogen_ext.models.semantic_kernel import SKChatCompletionAdapterfrom semantic_kernel import Kernelfrom semantic_kernel.connectors.ai.anthropic import AnthropicChatCompletion, AnthropicChatPromptExecutionSettingsfrom semantic_kernel.memory.null_memory import NullMemory+ async def get_weather(city: str) -> str:+ \"\"\"Get the weather for a city.\"\"\"+ return f"The weather in {city} is 75 degrees."++async def main() -> None:sk_client = AnthropicChatCompletion(ai_model_id="claude-3-5-sonnet-20241022",@@ -96,24 +102,34 @@ class SKChatCompletionAdapter(ChatCompletionClient):temperature=0.2,)- model_client = SKChatCompletionAdapter(sk_client, kernel=Kernel(memory=NullMemory()), prompt_settings=settings)+ model_client = SKChatCompletionAdapter(+ sk_client,+ kernel=Kernel(memory=NullMemory()),+ prompt_settings=settings,+ model_info={+ "function_calling": True,+ "json_output": True,+ "vision": True,+ "family": ModelFamily.CLAUDE_3_5_SONNET,+ },+ )# Call the model directly.- model_result = await model_client.create(- messages=[UserMessage(content="What is the capital of France?", source="User")]- )- print(model_result)+ response = await model_client.create([UserMessage(content="What is the capital of France?", source="test")])+ print(response)# Create an assistant agent with the model client.- assistant = AssistantAgent("assistant", model_client=model_client)+ assistant = AssistantAgent(+ "assistant", model_client=model_client, system_message="You are a helpful assistant.", tools=[get_weather]+ )# Call the assistant with a task.- result = await assistant.run(task="What is the capital of France?")- print(result)+ await Console(assistant.run_stream(task="What is the weather in Paris and London?"))asyncio.run(main())- Google Gemini models:++ Google Gemini models with function calling:.. code-block:: bash@@ -125,7 +141,8 @@ class SKChatCompletionAdapter(ChatCompletionClient):import osfrom autogen_agentchat.agents import AssistantAgent- from autogen_core.models import UserMessage+ from autogen_agentchat.ui import Console+ from autogen_core.models import UserMessage, ModelFamilyfrom autogen_ext.models.semantic_kernel import SKChatCompletionAdapterfrom semantic_kernel import Kernelfrom semantic_kernel.connectors.ai.google.google_ai import (@@ -135,24 +152,31 @@ class SKChatCompletionAdapter(ChatCompletionClient):from semantic_kernel.memory.null_memory import NullMemory+ def get_weather(city: str) -> str:+ \"\"\"Get the weather for a city.\"\"\"+ return f"The weather in {city} is 75 degrees."++async def main() -> None:sk_client = GoogleAIChatCompletion(- gemini_model_id="gemini-1.5-flash",+ gemini_model_id="gemini-2.0-flash",api_key=os.environ["GEMINI_API_KEY"],)settings = GoogleAIChatPromptExecutionSettings(temperature=0.2,)+ kernel = Kernel(memory=NullMemory())+model_client = SKChatCompletionAdapter(sk_client,- kernel=Kernel(memory=NullMemory()),+ kernel=kernel,prompt_settings=settings,model_info={- "family": "gemini-1.5-flash",+ "family": ModelFamily.GEMINI_2_0_FLASH,"function_calling": True,"json_output": True,- "vision": False,+ "vision": True,},)@@ -163,14 +187,17 @@ class SKChatCompletionAdapter(ChatCompletionClient):print(model_result)# Create an assistant agent with the model client.- assistant = AssistantAgent("assistant", model_client=model_client)+ assistant = AssistantAgent(+ "assistant", model_client=model_client, tools=[get_weather], system_message="You are a helpful assistant."+ )# Call the assistant with a task.- result = await assistant.run(task="What is the capital of France?")- print(result)+ stream = assistant.run_stream(task="What is the weather in Paris and London?")+ await Console(stream)asyncio.run(main())+Ollama models:.. code-block:: bashdiff --git a/python/packages/autogen-ext/src/autogen_ext/tools/semantic_kernel/_kernel_function_from_tool.py b/python/packages/autogen-ext/src/autogen_ext/tools/semantic_kernel/_kernel_function_from_tool.pyindex 1c2fec0e3..8125aef9c 100644--- a/python/packages/autogen-ext/src/autogen_ext/tools/semantic_kernel/_kernel_function_from_tool.py+++ b/python/packages/autogen-ext/src/autogen_ext/tools/semantic_kernel/_kernel_function_from_tool.py@@ -1,133 +1,67 @@-from typing import TypeVar+from typing import Any, TypeVarfrom autogen_core import CancellationTokenfrom autogen_core.tools import BaseToolfrom pydantic import BaseModel-from semantic_kernel.exceptions import FunctionExecutionException-from semantic_kernel.filters.functions.function_invocation_context import FunctionInvocationContext-from semantic_kernel.functions.function_result import FunctionResult-from semantic_kernel.functions.kernel_function import KernelFunction-from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadatafrom semantic_kernel.functions.kernel_parameter_metadata import KernelParameterMetadata+from semantic_kernel.functions import KernelFunctionFromMethod, kernel_functionInputT = TypeVar("InputT", bound=BaseModel)OutputT = TypeVar("OutputT", bound=BaseModel)-class KernelFunctionFromTool(KernelFunction):- """- KernelFunctionFromTool is an adapter that allows using Autogen tools as Semantic Kernel functions.- This makes it possible to integrate Autogen tools into Semantic Kernel when using the Semantic Kernel's- chat completion adapter or agent.-- By leveraging this adapter, you can:- - Convert any Autogen BaseTool into a Semantic Kernel KernelFunction- - Register the converted tool with a Semantic Kernel plugin- - Execute the tool through Semantic Kernel's function invocation mechanism- - Access tool metadata (name, description, parameters) through Semantic Kernel's metadata system-- Args:- tool (BaseTool[InputT, OutputT]):- The Autogen tool to wrap. Must be a subclass of BaseTool with Pydantic models for input/output.- plugin_name (str | None):- Optional name of the plugin this function belongs to. Defaults to None.-- Example usage:- .. code-block:: python-- from pydantic import BaseModel- from autogen_core.tools import BaseTool- from autogen_core import CancellationToken- from autogen_ext.tools.semantic_kernel import KernelFunctionFromTool- from semantic_kernel.functions.kernel_plugin import KernelPlugin- from semantic_kernel.kernel import Kernel--- # 1) Define input/output models- class CalculatorArgs(BaseModel):- a: float- b: float--- class CalculatorResult(BaseModel):- result: float--- # 2) Create an Autogen tool- class CalculatorTool(BaseTool[CalculatorArgs, CalculatorResult]):- def __init__(self) -> None:- super().__init__(- args_type=CalculatorArgs,- return_type=CalculatorResult,- name="calculator",- description="Add two numbers together",- )-- async def run(self, args: CalculatorArgs, cancellation_token: CancellationToken) -> CalculatorResult:- return CalculatorResult(result=args.a + args.b)--- # 3) Convert to Semantic Kernel function- calc_tool = CalculatorTool()- kernel_function = KernelFunctionFromTool(calc_tool, plugin_name="math")-- # 4) Add to Semantic Kernel plugin/kernel- plugin = KernelPlugin(name="math")- plugin.functions[calc_tool.name] = kernel_function- kernel = Kernel()- kernel.add_plugin(plugin)- """-+class KernelFunctionFromTool(KernelFunctionFromMethod):def __init__(self, tool: BaseTool[InputT, OutputT], plugin_name: str | None = None):- # Build up KernelFunctionMetadata. You can also parse the tool's schema for parameters.- parameters = [- KernelParameterMetadata(- name="args",- description="JSON arguments for the tool",- default_value=None,- type="dict",- type_object=dict,- is_required=True,+ # Get the pydantic model types from the tool+ args_type = tool.args_type()+ return_type = tool.return_type()++ # 1) Define an async function that calls the tool+ @kernel_function(name=tool.name, description=tool.description)+ async def tool_method(**kwargs: dict[str, Any]) -> Any:+ return await tool.run_json(kwargs, cancellation_token=CancellationToken())++ # Parse schema for parameters+ parameters_meta: list[KernelParameterMetadata] = []+ properties = tool.schema.get("parameters", {}).get("properties", {})++ # Get the field types from the pydantic model+ field_types = args_type.model_fields++ for prop_name, prop_info in properties.items():+ assert prop_name in field_types, f"Property {prop_name} not found in Tool {tool.name}"+ assert isinstance(prop_info, dict), f"Property {prop_name} is not a dict in Tool {tool.name}"++ # Get the actual type from the pydantic model field+ field_type = field_types[prop_name]+ parameters_meta.append(+ KernelParameterMetadata(+ name=prop_name,+ description=field_type.description or "",+ default_value=field_type.get_default(),+ type=prop_info.get("type", "string"), # type: ignore+ type_object=field_type.annotation,+ is_required=field_type.is_required(),+ ))- ]- return_param = KernelParameterMetadata(++ # Create return parameter metadata+ return_parameter = KernelParameterMetadata(name="return",- description="Result from the tool",+ description=f"Result from '{tool.name}' tool",default_value=None,- type="str",- type_object=str,- is_required=False,+ type="object" if issubclass(return_type, BaseModel) else "string",+ type_object=return_type,+ is_required=True,)- metadata = KernelFunctionMetadata(- name=tool.name,- description=tool.description,- parameters=parameters,- return_parameter=return_param,- is_prompt=False,- is_asynchronous=True,+ # Initialize the parent class+ super().__init__(+ method=tool_method,plugin_name=plugin_name,+ parameters=parameters_meta,+ return_parameter=return_parameter,+ additional_metadata=None,)- super().__init__(metadata=metadata)- self._tool = tool- async def _invoke_internal(self, context: FunctionInvocationContext) -> None:- # Extract the "args" parameter from the context- if "args" not in context.arguments:- raise FunctionExecutionException("Missing 'args' in FunctionInvocationContext.arguments")- tool_args = context.arguments-- # Call your tool's run_json- result = await self._tool.run_json(tool_args, cancellation_token=CancellationToken())-- # Wrap in a FunctionResult- context.result = FunctionResult(- function=self.metadata,- value=result,- metadata={"used_arguments": tool_args},- )-- async def _invoke_internal_stream(self, context: FunctionInvocationContext) -> None:- # If you don't have a streaming mechanism in your tool, you can simply reuse _invoke_internal- # or raise NotImplementedError. For example:- await self._invoke_internal(context)+ self._tool = tool