python --version
pip install semantic-kernel
export OPENAI_API_KEY=sk-...
Initialize a Semantic Kernel instance, add an AI service, and define a native plugin with Python functions that the kernel can call.
1 import asyncio 2 from semantic_kernel import Kernel 3 from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion 4 from semantic_kernel.functions import kernel_function 5 6 kernel = Kernel() 7 kernel.add_service(OpenAIChatCompletion(service_id="chat", ai_model_id="gpt-4o-mini")) 8 9 class MathPlugin: 10 @kernel_function(name="add", description="Add two numbers") 11 def add(self, a: float, b: float) -> float: 12 return a + b 13 14 @kernel_function(name="multiply", description="Multiply two numbers") 15 def multiply(self, a: float, b: float) -> float: 16 return a * b 17 18 kernel.add_plugin(MathPlugin(), plugin_name="math") 19 20 async def main(): 21 result = await kernel.invoke("math", "add", a=3.5, b=2.5) 22 print(f"3.5 + 2.5 = {result}") 23 24 asyncio.run(main()) 25
Sign in to share your feedback and join the discussion.