Login Sign Up

Getting Started with Microsoft Semantic Kernel

Semantic Kernel (SK) is an open-source project from Microsoft designed for building AI-powered applications, often referred to as agents. At its core, SK enables the creation of semantic plugins, which are essentially wrappers for functions and skills. These plugins can integrate with OpenAI plugins and allow for seamless orchestration of multiple API calls.

Installing and Setting Up Semantic Kernel

SK is compatible with Python, Java, and C#. This cross-language compatibility allows plugins to be developed in one language and consumed in another. However, native function sharing across languages is currently unsupported.

How the Semantic Kernel integrates as a plugin and can also consume plugins
How the Semantic Kernel integrates as a plugin and can also consume plugins

Installation Steps

To install SK, follow these steps in a terminal within VS Code:

Uninstall previous SK versions

pip uninstall semantic-kernel
Clone repository
git clone https://github.com/microsoft/semantic-kernel.git 

Navigate to the source folder

cd semantic-kernel/python

Install SK as an editable package

pip install -e .

Running a Basic SK Example

Once installed, you can create a chat completion service using OpenAI or Azure OpenAI. Below is an example script (chat_service_setup.py) that demonstrates this:

import semantic_kernel as sk
import asyncio

selected_service = "OpenAI"
kernel = sk.Kernel()

if selected_service == "OpenAI":
    from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
    api_key, org_id = sk.openai_settings_from_dot_env()
    service_id = "oai_chat_gpt"
    kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id="gpt-3.5-turbo-1106", api_key=api_key, org_id=org_id))

async def run_prompt():
    result = await kernel.invoke_prompt(prompt="recommend a movie about time travel")
    print(result)
asyncio.run(run_prompt())

Using Semantic Functions and Context Variables

Semantic functions in SK can use contextual variables to enhance prompts. Below is an example (contextual_recommendation.py) demonstrating a semantic function with placeholders:

prompt = """
system:
You have vast knowledge of everything and can recommend anything based on given criteria.

user:
Please recommend a {{$format}} with the subject {{$subject}} and {{$genre}}.
Include the following custom information: {{$custom}}
"""

prompt_template_config = sk.PromptTemplateConfig(
    template=prompt,
    name="recommendation",
    template_format="semantic-kernel",
    input_variables=[
        sk.InputVariable(name="format", description="The format to recommend", is_required=True),
        sk.InputVariable(name="subject", description="The subject to recommend", is_required=True),
        sk.InputVariable(name="genre", description="The genre to recommend", is_required=True),
        sk.InputVariable(name="custom", description="Custom information to enhance the recommendation", is_required=True),
    ]
)

recommend_function = kernel.create_function_from_prompt(prompt_template_config, function_name="Recommend_Movies", plugin_name="Recommendation")

async def run_recommendation(subject, format, genre, custom):
    recommendation = await kernel.invoke(recommend_function, sk.KernelArguments(subject=subject, format=format, genre=genre, custom=custom))
    print(recommendation)

asyncio.run(run_recommendation("time travel", "movie", "medieval", "must be a comedy"))

In general, semantic Kernel simplifies AI application development by enabling structured function orchestration through semantic functions and contextual variables

In the next steps, you can explore: how SK allows these functions to be registered as plugins for even greater flexibility.