autogen_ext.tools.semantic_kernel#
- pydantic model KernelFunctionFromTool[source]#
Bases:
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
- Parameters:
- Example usage:
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)
Show JSON schema
{ "title": "KernelFunctionFromTool", "type": "object", "properties": { "metadata": { "$ref": "#/$defs/KernelFunctionMetadata" }, "invocation_duration_histogram": { "default": null, "title": "Invocation Duration Histogram" }, "streaming_duration_histogram": { "default": null, "title": "Streaming Duration Histogram" } }, "$defs": { "KernelFunctionMetadata": { "description": "The kernel function metadata.", "properties": { "name": { "pattern": "^[0-9A-Za-z_]+$", "title": "Name", "type": "string" }, "plugin_name": { "anyOf": [ { "pattern": "^[0-9A-Za-z_]+$", "type": "string" }, { "type": "null" } ], "default": null, "title": "Plugin Name" }, "description": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Description" }, "parameters": { "items": { "$ref": "#/$defs/KernelParameterMetadata" }, "title": "Parameters", "type": "array" }, "is_prompt": { "title": "Is Prompt", "type": "boolean" }, "is_asynchronous": { "anyOf": [ { "type": "boolean" }, { "type": "null" } ], "default": true, "title": "Is Asynchronous" }, "return_parameter": { "anyOf": [ { "$ref": "#/$defs/KernelParameterMetadata" }, { "type": "null" } ], "default": null }, "additional_properties": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Additional Properties" } }, "required": [ "name", "is_prompt" ], "title": "KernelFunctionMetadata", "type": "object" }, "KernelParameterMetadata": { "description": "The kernel parameter metadata.", "properties": { "name": { "anyOf": [ { "pattern": "^[0-9A-Za-z_]+$", "type": "string" }, { "type": "null" } ], "title": "Name" }, "description": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Description" }, "default_value": { "anyOf": [ {}, { "type": "null" } ], "default": null, "title": "Default Value" }, "type": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": "str", "title": "Type" }, "is_required": { "anyOf": [ { "type": "boolean" }, { "type": "null" } ], "default": false, "title": "Is Required" }, "type_object": { "anyOf": [ {}, { "type": "null" } ], "default": null, "title": "Type Object" }, "schema_data": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Schema Data" }, "include_in_function_choices": { "default": true, "title": "Include In Function Choices", "type": "boolean" } }, "required": [ "name" ], "title": "KernelParameterMetadata", "type": "object" } }, "required": [ "metadata" ] }
- Fields: