0
0
Agentic-aiHow-ToBeginner ยท 4 min read

How to Build an Agent with Semantic Kernel: Simple Guide

To build an agent with Semantic Kernel, you create a kernel instance, add AI skills or functions, and then run the agent with input prompts. The kernel manages AI models and orchestrates tasks, enabling you to build intelligent agents easily.
๐Ÿ“

Syntax

The basic steps to build an agent with Semantic Kernel include:

  • Create Kernel: Initialize the Semantic Kernel object.
  • Add Skills: Load or define AI skills (functions) the agent can use.
  • Run Agent: Use the kernel to run the agent with input prompts and get responses.
python
from semantic_kernel import Kernel

# 1. Create kernel instance
kernel = Kernel()

# 2. Add skills (example: load a skill from folder)
skill = kernel.import_skill_from_directory("./skills", "MySkill")
kernel.register_skill(skill, "MySkill")

# 3. Run agent with prompt
result = kernel.run("MySkill", "FunctionName", input="Hello")
print(result)
๐Ÿ’ป

Example

This example shows how to build a simple agent that uses a greeting skill to respond to user input.

python
from semantic_kernel import Kernel

# Initialize kernel
kernel = Kernel()

# Define a simple skill function
class GreetingSkill:
    def greet(self, input: str) -> str:
        return f"Hello, {input}! Welcome to Semantic Kernel agent."

# Register the skill
greeting_skill = GreetingSkill()
kernel.register_skill(greeting_skill, "GreetingSkill")

# Run the agent with input
user_input = "Alice"
response = kernel.run("GreetingSkill", "greet", input=user_input)
print(response)
Output
Hello, Alice! Welcome to Semantic Kernel agent.
โš ๏ธ

Common Pitfalls

Common mistakes when building agents with Semantic Kernel include:

  • Not registering skills properly before running them.
  • Using incorrect function names when calling kernel.run().
  • Forgetting to initialize the kernel before adding skills.
  • Passing input in wrong format or missing required parameters.

Always check skill registration and function names carefully.

python
from semantic_kernel import Kernel

kernel = Kernel()

# Wrong: running skill before registering
# result = kernel.run("GreetingSkill", "greet", input="Bob")  # Error: skill not found

# Correct way:
class GreetingSkill:
    def greet(self, input: str) -> str:
        return f"Hi, {input}!"

greeting_skill = GreetingSkill()
kernel.register_skill(greeting_skill, "GreetingSkill")

result = kernel.run("GreetingSkill", "greet", input="Bob")
print(result)
Output
Hi, Bob!
๐Ÿ“Š

Quick Reference

StepDescription
Create KernelInitialize the Semantic Kernel instance to manage AI tasks.
Add SkillsLoad or define AI functions the agent can use.
Register SkillsMake skills available to the kernel with a name.
Run AgentCall kernel.run() with skill name, function, and input.
Get OutputReceive the agent's response as a string.
โœ…

Key Takeaways

Initialize Semantic Kernel before adding or running skills.
Register your AI skills with clear names to use them in the agent.
Use kernel.run() with correct skill and function names to get responses.
Check input format and parameters carefully to avoid errors.
Semantic Kernel simplifies building AI agents by managing skills and prompts.