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) orsetx OPENAI_API_KEY your_api_key(Windows)
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.
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_idto another OpenAI model likegpt-4o. - Other AI providers: Add services for Anthropic or Azure OpenAI similarly.
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
KeyErrorforOPENAI_API_KEY, ensure the environment variable is set correctly. - If the model is not found, verify the
ai_model_idmatches a valid OpenAI model name. - For network errors, check your internet connection and firewall settings.
Key Takeaways
- Use
semantic_kernel.Kerneland add an AI chat service to enable chat completions. - Call
kernel.chat.generatefor synchronous orgenerate_asyncfor asynchronous chat responses. - Set your OpenAI API key in the environment variable
OPENAI_API_KEYbefore running code.