-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ai_service_client_base.py
44 lines (30 loc) · 1.81 KB
/
ai_service_client_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Copyright (c) Microsoft. All rights reserved.
from abc import ABC
from typing import Annotated
from pydantic import Field, StringConstraints
from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings
from semantic_kernel.kernel_pydantic import KernelBaseModel
class AIServiceClientBase(KernelBaseModel, ABC):
"""Base class for all AI Services.
Has a ai_model_id and service_id, any other fields have to be defined by the subclasses.
The ai_model_id can refer to a specific model, like 'gpt-35-turbo' for OpenAI,
or can just be a string that is used to identify the model in the service.
The service_id is used in Semantic Kernel to identify the service, if empty the ai_model_id is used.
"""
ai_model_id: Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)]
service_id: str = Field("")
def model_post_init(self, __context: object | None = None):
"""Update the service_id if it is not set."""
if not self.service_id:
self.service_id = self.ai_model_id
def get_prompt_execution_settings_class(self) -> "PromptExecutionSettings":
"""Get the request settings class."""
return PromptExecutionSettings # pragma: no cover
def instantiate_prompt_execution_settings(self, **kwargs) -> "PromptExecutionSettings":
"""Create a request settings object.
All arguments are passed to the constructor of the request settings object.
"""
return self.get_prompt_execution_settings_class()(**kwargs)
def get_prompt_execution_settings_from_settings(self, settings: PromptExecutionSettings) -> PromptExecutionSettings:
"""Get the request settings from a settings object."""
return self.get_prompt_execution_settings_class().from_prompt_execution_settings(settings)