Skip to content

Commit d120910

Browse files
Python: allow settings to be created directly (#11468)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> I've always hated having to add the .create to a settings object, this removes that, you can just use the regular init, added benefit is that it has the proper fields in the docstrings for each implemented settings object! ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> Adds a __new__ method to the base settings class, which takes the prefix and if supplied the env_file and encoding, then calls model_rebuild and then the init. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 69cd5b3 commit d120910

File tree

131 files changed

+635
-558
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+635
-558
lines changed

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ usearch = [
124124
"pyarrow >= 12.0,< 20.0"
125125
]
126126
weaviate = [
127-
"weaviate-client>=4.10,<4.13",
127+
"weaviate-client>=4.10,<5.0",
128128
]
129129
pandas = [
130130
"pandas ~= 2.2"

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_as_kernel_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async def main() -> None:
6767
# The filter is used for demonstration purposes to show the function invocation.
6868
kernel.add_filter("function_invocation", function_invocation_filter)
6969

70-
ai_agent_settings = AzureAIAgentSettings.create()
70+
ai_agent_settings = AzureAIAgentSettings()
7171

7272
async with (
7373
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_azure_ai_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737
async def main() -> None:
38-
ai_agent_settings = AzureAIAgentSettings.create()
38+
ai_agent_settings = AzureAIAgentSettings()
3939

4040
async with (
4141
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_file_manipulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
async def main() -> None:
21-
ai_agent_settings = AzureAIAgentSettings.create()
21+
ai_agent_settings = AzureAIAgentSettings()
2222

2323
async with (
2424
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_message_callback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def handle_intermediate_steps(message: ChatMessageContent) -> None:
4949

5050

5151
async def main() -> None:
52-
ai_agent_settings = AzureAIAgentSettings.create()
52+
ai_agent_settings = AzureAIAgentSettings()
5353

5454
async with (
5555
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_message_callback_streaming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async def handle_streaming_intermediate_steps(message: ChatMessageContent) -> No
4747

4848

4949
async def main() -> None:
50-
ai_agent_settings = AzureAIAgentSettings.create()
50+
ai_agent_settings = AzureAIAgentSettings()
5151

5252
async with (
5353
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_prompt_templating.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def invoke_agent_with_template(template_str: str, template_format: str, de
5252
# Configure the prompt template
5353
prompt_config = PromptTemplateConfig(template=template_str, template_format=template_format)
5454

55-
ai_agent_settings = AzureAIAgentSettings.create()
55+
ai_agent_settings = AzureAIAgentSettings()
5656

5757
async with (
5858
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_streaming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_item_price(
3636

3737

3838
async def main() -> None:
39-
ai_agent_settings = AzureAIAgentSettings.create()
39+
ai_agent_settings = AzureAIAgentSettings()
4040

4141
async with (
4242
DefaultAzureCredential() as creds,

python/samples/concepts/agents/azure_ai_agent/azure_ai_agent_structured_outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Planet(BaseModel):
3535

3636

3737
async def main():
38-
ai_agent_settings = AzureAIAgentSettings.create()
38+
ai_agent_settings = AzureAIAgentSettings()
3939
async with (
4040
DefaultAzureCredential() as creds,
4141
AzureAIAgent.create_client(

python/samples/concepts/on_your_data/azure_chat_gpt_with_data_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# }
3636

3737
# Create the data source settings
38-
azure_ai_search_settings = AzureAISearchSettings.create()
38+
azure_ai_search_settings = AzureAISearchSettings()
3939

4040
az_source = AzureAISearchDataSource.from_azure_ai_search_settings(azure_ai_search_settings=azure_ai_search_settings)
4141
extra = ExtraBody(data_sources=[az_source])

python/samples/concepts/on_your_data/azure_chat_gpt_with_data_api_function_calling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
kernel = sk.Kernel()
2828

2929
# Create the data source settings
30-
azure_ai_search_settings = AzureAISearchSettings.create()
30+
azure_ai_search_settings = AzureAISearchSettings()
3131
az_source = AzureAISearchDataSource(parameters=azure_ai_search_settings.model_dump())
3232
extra = ExtraBody(data_sources=[az_source])
3333
req_settings = AzureChatPromptExecutionSettings(service_id="chat-gpt", extra_body=extra, tool_choice="auto")

python/samples/concepts/on_your_data/azure_chat_gpt_with_data_api_vector_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Bonded by their love for the natural world and shared curiosity, they uncovered a
2525
# groundbreaking phenomenon in glaciology that could potentially reshape our understanding of climate change.
2626

27-
azure_ai_search_settings = AzureAISearchSettings.create()
27+
azure_ai_search_settings = AzureAISearchSettings()
2828
azure_ai_search_settings = azure_ai_search_settings.model_dump()
2929

3030
# This example index has fields "title", "chunk", and "vector".

python/samples/concepts/rag/self-critique_rag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def populate_memory(memory: SemanticTextMemory) -> None:
2929
async def main() -> None:
3030
kernel = Kernel()
3131

32-
azure_ai_search_settings = AzureAISearchSettings.create()
32+
azure_ai_search_settings = AzureAISearchSettings()
3333
vector_size = 1536
3434

3535
# Setting up OpenAI services for text completion and text embedding

python/samples/concepts/setup/chat_completion_services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ def get_deepseek_chat_completion_service_and_request_settings() -> tuple[
393393
OpenAISettings,
394394
)
395395

396-
openai_settings = OpenAISettings.create()
396+
openai_settings = OpenAISettings()
397397
if not openai_settings.api_key:
398398
raise ServiceInitializationError("The DeepSeek API key is required.")
399399
if not openai_settings.chat_model_id:

python/samples/demos/booking_restaurant/restaurant_booking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
kernel.add_service(ai_service)
2626

2727
try:
28-
booking_sample_settings = BookingSampleSettings.create()
28+
booking_sample_settings = BookingSampleSettings()
2929
except ValidationError as e:
3030
raise ServiceInitializationError("Failed to initialize the booking sample settings.") from e
3131

python/samples/demos/telemetry/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from samples.demos.telemetry.telemetry_sample_settings import TelemetrySampleSettings
3333

3434
# Load settings
35-
settings = TelemetrySampleSettings.create()
35+
settings = TelemetrySampleSettings()
3636

3737
# Create a resource to represent the service/sample
3838
resource = Resource.create({ResourceAttributes.SERVICE_NAME: "TelemetryExample"})

python/samples/getting_started/00-getting-started.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"\n",
127127
"from samples.service_settings import ServiceSettings\n",
128128
"\n",
129-
"service_settings = ServiceSettings.create()\n",
129+
"service_settings = ServiceSettings()\n",
130130
"\n",
131131
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
132132
"selectedService = (\n",

python/samples/getting_started/01-basic-loading-the-kernel.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
"\n",
134134
"from samples.service_settings import ServiceSettings\n",
135135
"\n",
136-
"service_settings = ServiceSettings.create()\n",
136+
"service_settings = ServiceSettings()\n",
137137
"\n",
138138
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
139139
"selectedService = (\n",

python/samples/getting_started/02-running-prompts-from-file.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230
"\n",
231231
"from samples.service_settings import ServiceSettings\n",
232232
"\n",
233-
"service_settings = ServiceSettings.create()\n",
233+
"service_settings = ServiceSettings()\n",
234234
"\n",
235235
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
236236
"selectedService = (\n",
@@ -260,7 +260,7 @@
260260
"\n",
261261
"from samples.service_settings import ServiceSettings\n",
262262
"\n",
263-
"service_settings = ServiceSettings.create()\n",
263+
"service_settings = ServiceSettings()\n",
264264
"\n",
265265
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
266266
"selectedService = (\n",

python/samples/getting_started/03-prompt-function-inline.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
"\n",
166166
"from samples.service_settings import ServiceSettings\n",
167167
"\n",
168-
"service_settings = ServiceSettings.create()\n",
168+
"service_settings = ServiceSettings()\n",
169169
"\n",
170170
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
171171
"selectedService = (\n",

python/samples/getting_started/04-kernel-arguments-chat.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"\n",
124124
"from samples.service_settings import ServiceSettings\n",
125125
"\n",
126-
"service_settings = ServiceSettings.create()\n",
126+
"service_settings = ServiceSettings()\n",
127127
"\n",
128128
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
129129
"selectedService = (\n",

python/samples/getting_started/05-using-the-planner.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
"\n",
129129
"from samples.service_settings import ServiceSettings\n",
130130
"\n",
131-
"service_settings = ServiceSettings.create()\n",
131+
"service_settings = ServiceSettings()\n",
132132
"\n",
133133
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
134134
"selectedService = (\n",

python/samples/getting_started/06-memory-and-embeddings.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
"\n",
134134
"from samples.service_settings import ServiceSettings\n",
135135
"\n",
136-
"service_settings = ServiceSettings.create()\n",
136+
"service_settings = ServiceSettings()\n",
137137
"\n",
138138
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
139139
"selectedService = (\n",

python/samples/getting_started/08-native-function-inline.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
"\n",
152152
"from samples.service_settings import ServiceSettings\n",
153153
"\n",
154-
"service_settings = ServiceSettings.create()\n",
154+
"service_settings = ServiceSettings()\n",
155155
"\n",
156156
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
157157
"selectedService = (\n",

python/samples/getting_started/09-groundedness-checking.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
"\n",
186186
"from samples.service_settings import ServiceSettings\n",
187187
"\n",
188-
"service_settings = ServiceSettings.create()\n",
188+
"service_settings = ServiceSettings()\n",
189189
"\n",
190190
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
191191
"selectedService = (\n",

python/samples/getting_started/10-multiple-results-per-prompt.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
"\n",
131131
"from samples.service_settings import ServiceSettings\n",
132132
"\n",
133-
"service_settings = ServiceSettings.create()\n",
133+
"service_settings = ServiceSettings()\n",
134134
"\n",
135135
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
136136
"selectedService = (\n",

python/samples/getting_started/11-streaming-completions.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
"\n",
124124
"from samples.service_settings import ServiceSettings\n",
125125
"\n",
126-
"service_settings = ServiceSettings.create()\n",
126+
"service_settings = ServiceSettings()\n",
127127
"\n",
128128
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
129129
"selectedService = (\n",

python/samples/getting_started_with_agents/azure_ai_agent/step1_azure_ai_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
async def main() -> None:
31-
ai_agent_settings = AzureAIAgentSettings.create()
31+
ai_agent_settings = AzureAIAgentSettings()
3232

3333
async with (
3434
DefaultAzureCredential() as creds,

python/samples/getting_started_with_agents/azure_ai_agent/step2_azure_ai_agent_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_item_price(
4343

4444

4545
async def main() -> None:
46-
ai_agent_settings = AzureAIAgentSettings.create()
46+
ai_agent_settings = AzureAIAgentSettings()
4747

4848
async with (
4949
DefaultAzureCredential() as creds,

python/samples/getting_started_with_agents/azure_ai_agent/step3_azure_ai_agent_group_chat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ async def should_agent_terminate(self, agent, history):
4545

4646

4747
async def main():
48-
ai_agent_settings = AzureAIAgentSettings.create()
48+
ai_agent_settings = AzureAIAgentSettings()
4949

5050
async with (
5151
DefaultAzureCredential() as creds,

python/samples/getting_started_with_agents/azure_ai_agent/step4_azure_ai_agent_code_interpreter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
async def main() -> None:
20-
ai_agent_settings = AzureAIAgentSettings.create()
20+
ai_agent_settings = AzureAIAgentSettings()
2121

2222
async with (
2323
DefaultAzureCredential() as creds,

python/samples/getting_started_with_agents/azure_ai_agent/step5_azure_ai_agent_file_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
async def main() -> None:
26-
ai_agent_settings = AzureAIAgentSettings.create()
26+
ai_agent_settings = AzureAIAgentSettings()
2727

2828
async with (
2929
DefaultAzureCredential() as creds,

python/samples/getting_started_with_agents/azure_ai_agent/step6_azure_ai_agent_openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
async def main() -> None:
27-
ai_agent_settings = AzureAIAgentSettings.create()
27+
ai_agent_settings = AzureAIAgentSettings()
2828

2929
async with (
3030
DefaultAzureCredential() as creds,

python/samples/service_settings.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
from typing import Literal
4+
35
from semantic_kernel.kernel_pydantic import KernelBaseSettings
46

57

@@ -13,8 +15,8 @@ class ServiceSettings(KernelBaseSettings):
1315
are missing.
1416
1517
Args:
16-
global_llm_service (str | None): The LLM service to use for the samples, either "OpenAI" or "AzureOpenAI"
18+
global_llm_service: The LLM service to use for the samples, either "OpenAI" or "AzureOpenAI"
1719
If not provided, defaults to "AzureOpenAI".
1820
"""
1921

20-
global_llm_service: str | None = None
22+
global_llm_service: Literal["OpenAI", "AzureOpenAI"] = "AzureOpenAI"

python/samples/sk_service_configurator.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ def add_service(
2929
Kernel: The configured kernel
3030
"""
3131
try:
32-
settings = ServiceSettings.create(
32+
settings = ServiceSettings(
3333
env_file_path=env_file_path,
3434
env_file_encoding=env_file_encoding,
3535
)
3636
except ValidationError as ex:
3737
raise ServiceInitializationError("Unable to configure learn resources settings.", ex) from ex
3838

39-
if not settings.global_llm_service:
39+
if "global_llm_service" not in settings.model_fields_set:
4040
print("GLOBAL_LLM_SERVICE not set, trying to use Azure OpenAI.")
41-
settings.global_llm_service = "AzureOpenAI"
4241

4342
# The service_id is used to identify the service in the kernel.
4443
# This can be updated to a custom value if needed.

python/semantic_kernel/agents/azure_ai/azure_ai_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def create_client(
255255
AIProjectClient: The Azure AI Project client
256256
"""
257257
if conn_str is None:
258-
ai_agent_settings = AzureAIAgentSettings.create()
258+
ai_agent_settings = AzureAIAgentSettings()
259259
if not ai_agent_settings.project_connection_string:
260260
raise AgentInitializationException("Please provide a valid Azure AI connection string.")
261261
conn_str = ai_agent_settings.project_connection_string.get_secret_value()

python/semantic_kernel/agents/bedrock/bedrock_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async def create_and_prepare_agent(
207207
An instance of BedrockAgent with the created agent.
208208
"""
209209
try:
210-
bedrock_agent_settings = BedrockAgentSettings.create(
210+
bedrock_agent_settings = BedrockAgentSettings(
211211
agent_resource_role_arn=agent_resource_role_arn,
212212
foundation_model=foundation_model,
213213
env_file_path=env_file_path,

python/semantic_kernel/agents/open_ai/azure_assistant_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def setup_resources(
5757
An Azure OpenAI client instance and the configured deployment name (model)
5858
"""
5959
try:
60-
azure_openai_settings = AzureOpenAISettings.create(
60+
azure_openai_settings = AzureOpenAISettings(
6161
api_key=api_key,
6262
base_url=base_url,
6363
endpoint=endpoint,

python/semantic_kernel/agents/open_ai/azure_responses_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def setup_resources(
6565
An Azure OpenAI client instance and the configured deployment name (model)
6666
"""
6767
try:
68-
azure_openai_settings = AzureOpenAISettings.create(
68+
azure_openai_settings = AzureOpenAISettings(
6969
api_key=api_key,
7070
base_url=base_url,
7171
endpoint=endpoint,

python/semantic_kernel/agents/open_ai/open_ai_assistant_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def setup_resources(
272272
An OpenAI client instance and the configured model name
273273
"""
274274
try:
275-
openai_settings = OpenAISettings.create(
275+
openai_settings = OpenAISettings(
276276
chat_model_id=ai_model_id,
277277
api_key=api_key,
278278
org_id=org_id,

python/semantic_kernel/agents/open_ai/openai_responses_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def setup_resources(
332332
An OpenAI client instance and the configured Response model name
333333
"""
334334
try:
335-
openai_settings = OpenAISettings.create(
335+
openai_settings = OpenAISettings(
336336
responses_model_id=ai_model_id,
337337
api_key=api_key,
338338
org_id=org_id,

0 commit comments

Comments
 (0)