|
| 1 | +from azure.ai.agents.models import ToolDefinition, AgentsResponseFormat |
| 2 | +from semantic_kernel.functions.kernel_function import KernelFunction |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +class KernelFunctionToolWrapper(ToolDefinition): |
| 6 | + def __init__( |
| 7 | + self, |
| 8 | + kernel_function: KernelFunction, |
| 9 | + name: Optional[str] = None, |
| 10 | + description: Optional[str] = None |
| 11 | + ): |
| 12 | + self._kernel_function = kernel_function |
| 13 | + self.name = name or kernel_function.name |
| 14 | + self.description = description or kernel_function.description or "SK-wrapped function" |
| 15 | + |
| 16 | + async def invoke(self, request) -> AgentsResponseFormat: |
| 17 | + args = request.args or {} |
| 18 | + input_value = args.get("input", "") |
| 19 | + result = await self._kernel_function.invoke_async(input_value) |
| 20 | + return AgentsResponseFormat(output=result if isinstance(result, str) else str(result))#AgentsResponseFormat(output=str(result)) |
| 21 | + |
| 22 | + @property |
| 23 | + def _data(self) -> dict: |
| 24 | + """Required for Azure Agent framework and planner compatibility.""" |
| 25 | + return { |
| 26 | + "name": self.name, |
| 27 | + "description": self.description, |
| 28 | + "parameters": { |
| 29 | + "type": "object", |
| 30 | + "properties": { |
| 31 | + "input": { |
| 32 | + "type": "string", |
| 33 | + "description": "Input string for the function" |
| 34 | + } |
| 35 | + }, |
| 36 | + "required": ["input"] |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +def create_tool_from_kernel_function( |
| 42 | + kernel_function: KernelFunction, |
| 43 | + name: Optional[str] = None, |
| 44 | + description: Optional[str] = None |
| 45 | +) -> ToolDefinition: |
| 46 | + return KernelFunctionToolWrapper(kernel_function, name, description) |
0 commit comments