pythonadvanced
Semantic Kernel Plugin in Python
Build a Semantic Kernel plugin with kernel functions that can be invoked by an AI planner.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
Best for: prompt chaining
#langchain#openai
pythonintermediate
Async OpenAI Client in Python
Use the AsyncOpenAI client with asyncio to run concurrent chat completions without blocking.
Best for: concurrent LLM calls
#openai#async
pythonintermediate
LangChain Tool-Using Agent
Build a LangChain agent with custom tools for web search, calculator, and Python REPL.
Best for: AI agents
#langchain#agent
pythonbeginner
Anthropic Streaming with Python
Stream Claude responses token by token using the Anthropic Python SDK with context manager.
Best for: streaming responses
#anthropic#claude