KernelServicesExtension Class

Kernel services extension.

Adds all services related entities to the Kernel.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Constructor

KernelServicesExtension(*, services: dict[str, AIServiceClientBase] = None, ai_service_selector: AIServiceSelector = None)

Keyword-Only Parameters

Name Description
services
Required
ai_service_selector
Required

Methods

add_service

Add a single service to the Kernel.

get_prompt_execution_settings_from_service_id

Get the specific request settings from the service, instantiated with the service_id and ai_model_id.

get_service

Get a service by service_id and type.

Type is optional and when not supplied, no checks are done. Type should be

TextCompletionClientBase, ChatCompletionClientBase, EmbeddingGeneratorBase or a subclass of one. You can also check for multiple types in one go, by using a tuple: (TextCompletionClientBase, ChatCompletionClientBase).

If type and service_id are both None, the first service is returned.

get_services_by_type

Get all services of a specific type.

remove_all_services

Removes the services from the Kernel, does not delete them.

remove_service

Delete a single service from the Kernel.

rewrite_services

Rewrite services to a dictionary.

select_ai_service

Uses the AI service selector to select a service for the function.

add_service

Add a single service to the Kernel.

add_service(service: AIServiceClientBase, overwrite: bool = False) -> None

Parameters

Name Description
service
Required
<xref:semantic_kernel.services.kernel_services_extension.AIServiceClientBase>

The service to add.

overwrite
<xref:<xref:semantic_kernel.services.kernel_services_extension.bool, optional>>

Whether to overwrite the service if it already exists. Defaults to False.

Default value: False

get_prompt_execution_settings_from_service_id

Get the specific request settings from the service, instantiated with the service_id and ai_model_id.

get_prompt_execution_settings_from_service_id(service_id: str, type: type[AI_SERVICE_CLIENT_TYPE] | None = None) -> PromptExecutionSettings

Parameters

Name Description
service_id
Required
type
Default value: None

get_service

Get a service by service_id and type.

Type is optional and when not supplied, no checks are done. Type should be

TextCompletionClientBase, ChatCompletionClientBase, EmbeddingGeneratorBase or a subclass of one. You can also check for multiple types in one go, by using a tuple: (TextCompletionClientBase, ChatCompletionClientBase).

If type and service_id are both None, the first service is returned.

get_service(service_id: str | None = None, type: type[AI_SERVICE_CLIENT_TYPE] | tuple[type[AI_SERVICE_CLIENT_TYPE], ...] | None = None) -> AIServiceClientBase

Parameters

Name Description
service_id
<xref:<xref:semantic_kernel.services.kernel_services_extension.str | None>>

The service id, if None, the default service is returned or the first service is returned.

Default value: None
type
<xref:Type>[<xref:AI_SERVICE_CLIENT_TYPE>]<xref: | tuple>[type[<xref:AI_SERVICE_CLIENT_TYPE>],<xref: ...>]<xref: | None>

The type of the service, if None, no checks are done on service type.

Default value: None

Returns

Type Description

The service, should be a class derived from AIServiceClientBase.

Exceptions

Type Description

If no service is found that matches the type or id.

get_services_by_type

Get all services of a specific type.

get_services_by_type(type: type[AI_SERVICE_CLIENT_TYPE] | tuple[type[AI_SERVICE_CLIENT_TYPE], ...] | None) -> dict[str, AIServiceClientBase]

Parameters

Name Description
type
Required

remove_all_services

Removes the services from the Kernel, does not delete them.

remove_all_services() -> None

remove_service

Delete a single service from the Kernel.

remove_service(service_id: str) -> None

Parameters

Name Description
service_id
Required

rewrite_services

Rewrite services to a dictionary.

rewrite_services(services: AI_SERVICE_CLIENT_TYPE | list[AI_SERVICE_CLIENT_TYPE] | dict[str, AI_SERVICE_CLIENT_TYPE] | None = None) -> dict[str, AI_SERVICE_CLIENT_TYPE]

Parameters

Name Description
services
Default value: None

select_ai_service

Uses the AI service selector to select a service for the function.

select_ai_service(function: KernelFunction | None = None, arguments: KernelArguments | None = None, type: type[AI_SERVICE_CLIENT_TYPE] | tuple[type[AI_SERVICE_CLIENT_TYPE], ...] | None = None) -> tuple[AIServiceClientBase, PromptExecutionSettings]

Parameters

Name Description
function
<xref:<xref:semantic_kernel.services.kernel_services_extension.KernelFunction | None>>

The function used.

Default value: None
arguments
<xref:<xref:semantic_kernel.services.kernel_services_extension.KernelArguments | None>>

The arguments used.

Default value: None
type
<xref:Type>[<xref:AI_SERVICE_CLIENT_TYPE>]<xref: | tuple>[type[<xref:AI_SERVICE_CLIENT_TYPE>],<xref: ...>]<xref: | None>

The type of service to select. Defaults to None.

Default value: None

Attributes

ai_service_selector

ai_service_selector: AIServiceSelector

services

services: dict[str, AIServiceClientBase]