How to beginner · 3 min read

How to use Semantic Kernel chat completion

Quick answer
Use the semantic_kernel Python package to create a Kernel instance and add an AI chat service like OpenAIChatCompletion. Then call kernel.chat.generate_async or kernel.chat.generate with your prompt to get chat completions.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install semantic-kernel openai

Setup

Install the required packages and set your OpenAI API key as an environment variable.

  • Install Semantic Kernel and OpenAI SDK: pip install semantic-kernel openai
  • Set environment variable: export OPENAI_API_KEY=your_api_key (Linux/macOS) or setx OPENAI_API_KEY your_api_key (Windows)
bash
pip install semantic-kernel openai

Step by step

This example shows how to create a Kernel, add the OpenAI chat completion service, and generate a chat response synchronously.

python
import os
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

# Initialize kernel
kernel = sk.Kernel()

# Add OpenAI chat completion service
kernel.add_service(OpenAIChatCompletion(
    service_id="chat",
    api_key=os.environ["OPENAI_API_KEY"],
    ai_model_id="gpt-4o-mini"
))

# Define prompt
prompt = "Explain the benefits of Semantic Kernel in AI development."

# Generate chat completion synchronously
response = kernel.chat.generate(
    service_id="chat",
    prompt=prompt
)

print("Response:", response.text)
output
Response: Semantic Kernel simplifies AI development by providing a modular framework to integrate AI models, manage prompts, and orchestrate workflows efficiently.

Common variations

You can use asynchronous calls, switch models, or integrate other AI providers.

  • Async usage: Use await kernel.chat.generate_async(...) inside an async function.
  • Different model: Change ai_model_id to another OpenAI model like gpt-4o.
  • Other AI providers: Add services for Anthropic or Azure OpenAI similarly.
python
import asyncio

async def async_chat():
    response = await kernel.chat.generate_async(
        service_id="chat",
        prompt="What is LangChain?"
    )
    print("Async response:", response.text)

asyncio.run(async_chat())
output
Async response: LangChain is a framework for building applications with large language models, enabling chaining and orchestration of prompts and tools.

Troubleshooting

  • If you see KeyError for OPENAI_API_KEY, ensure the environment variable is set correctly.
  • If the model is not found, verify the ai_model_id matches a valid OpenAI model name.
  • For network errors, check your internet connection and firewall settings.

Key Takeaways

  • Use semantic_kernel.Kernel and add an AI chat service to enable chat completions.
  • Call kernel.chat.generate for synchronous or generate_async for asynchronous chat responses.
  • Set your OpenAI API key in the environment variable OPENAI_API_KEY before running code.
Verified 2026-04 · gpt-4o-mini
Verify ↗