Files
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

2.3 KiB

Chat Completion Agents

The following samples demonstrate how to get started with Chat Completion Agents using Semantic Kernel.

Configuring a Chat Completion Agent

The ChatCompletionAgent relies on an underlying AI service connector. Depending on the AI service you choose, you may need to install additional packages. Refer to the official SK documentation for guidance on which extras are required.

Next, follow this configuration guide to set up your environment for running the sample code.

If you're developing outside the Semantic Kernel repository, it's recommended to place your .env file at the root of your project. When using VSCode, this allows the IDE to automatically load the .env file and make the environment variables available to your application.

This setup enables the following code to work without explicitly passing keyword arguments to the AI service constructor:

from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

agent = ChatCompletionAgent(
    service=AzureChatCompletion(),  # No explicit kwargs needed due to environment variable configuration
    name="Assistant",
    instructions="Answer questions about the world in one sentence.",
)

If you prefer to configure the service manually, you can do the following:

from semantic_kernel.agents import ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

agent = ChatCompletionAgent(
    service=AzureChatCompletion(
        api_key="your-api-key",
        endpoint="your-aoai-endpoint",
        deployment_name="your-deployment-name",
        api_version="2025-03-01-preview"  # Replace with your desired API version
    ),
    name="Assistant",
    instructions="Answer questions about the world in one sentence.",
)

For more information about the ChatCompletionAgent see Semantic Kernel's official documentation here.