pythonadvanced

Semantic Kernel Plugin in Python

Build a Semantic Kernel plugin with kernel functions that can be invoked by an AI planner.

python
import asyncio
from semantic_kernel import Kernel
from semantic_kernel.functions import kernel_function
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

class MathPlugin:
    @kernel_function(description='Adds two numbers together')
    def add(self, a: float, b: float) -> str:
        return str(a + b)

    @kernel_function(description='Multiplies two numbers')
    def multiply(self, a: float, b: float) -> str:
        return str(a * b)

async def main() -> None:
    kernel = Kernel()
    kernel.add_service(OpenAIChatCompletion(ai_model_id='gpt-4o-mini'))
    kernel.add_plugin(MathPlugin(), plugin_name='Math')

    result = await kernel.invoke(kernel.plugins['Math']['add'], a=15.0, b=27.0)
    print('15 + 27 =', result)

asyncio.run(main())

Use Cases

  • AI orchestration
  • plugin architecture
  • enterprise AI

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.