Skip to content

Commit 1c2a505

Browse files
Jordan Henkeldluc
authored andcommitted
Python: remove Verify and ./diagnostics (microsoft#287)
### Motivation and Context In an effort to make the Python port more idiomatic, let's remove the `Verify` class and the `./diagnostics` sub-module. There are many more follow-on tasks to do here, but this is a good start (and already a large enough change). ### Description This PR does the following: 1. Removes the `Verify` class, and re-writes all instances of `Verify.*` 2. Adds a `validation.py` in the `./utils` sub-module to hand some of the more complex cases from `Verify` (checking that skills/functions/and function params have valid names) 3. Removes the rest of the `./diagnostics` sub-module (w/ a longer-term goal of removing all/most of our custom exception classes and, instead, using appropriate built-in error classes)
1 parent 407fb36 commit 1c2a505

26 files changed

Lines changed: 190 additions & 282 deletions

python/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
OPENAI_API_KEY=""
22
OPENAI_ORG_ID=""
33
AZURE_OPENAI_API_KEY=""
4-
AZURE_OPENAI_ENDPOINT=""
4+
AZURE_OPENAI_ENDPOINT=""

python/semantic_kernel/ai/ai_exception.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
from enum import Enum
44
from typing import Optional
55

6-
from semantic_kernel.diagnostics.sk_exception import SKException
76

8-
9-
class AIException(SKException):
7+
class AIException(Exception):
108
class ErrorCodes(Enum):
119
# Unknown error.
1210
UnknownError = -1

python/semantic_kernel/ai/open_ai/services/azure_chat_completion.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from semantic_kernel.ai.open_ai.services.open_ai_chat_completion import (
88
OpenAIChatCompletion,
99
)
10-
from semantic_kernel.diagnostics.verify import Verify
1110

1211

1312
class AzureChatCompletion(OpenAIChatCompletion):
@@ -22,12 +21,14 @@ def __init__(
2221
api_version: str = "2023-03-15-preview",
2322
logger: Optional[Logger] = None,
2423
) -> None:
25-
Verify.not_empty(deployment_name, "You must provide a deployment name")
26-
Verify.not_empty(api_key, "The Azure API key cannot be empty")
27-
Verify.not_empty(endpoint, "The Azure endpoint cannot be empty")
28-
Verify.starts_with(
29-
endpoint, "https://", "The Azure endpoint must start with https://"
30-
)
24+
if not deployment_name:
25+
raise ValueError("The deployment name cannot be `None` or empty")
26+
if not api_key:
27+
raise ValueError("The Azure API key cannot be `None` or empty`")
28+
if not endpoint:
29+
raise ValueError("The Azure endpoint cannot be `None` or empty")
30+
if not endpoint.startswith("https://"):
31+
raise ValueError("The Azure endpoint must start with https://")
3132

3233
self._endpoint = endpoint
3334
self._api_version = api_version

python/semantic_kernel/ai/open_ai/services/azure_text_completion.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from semantic_kernel.ai.open_ai.services.open_ai_text_completion import (
88
OpenAITextCompletion,
99
)
10-
from semantic_kernel.diagnostics.verify import Verify
1110

1211

1312
class AzureTextCompletion(OpenAITextCompletion):
@@ -22,12 +21,14 @@ def __init__(
2221
api_version: str = "2022-12-01",
2322
logger: Optional[Logger] = None,
2423
) -> None:
25-
Verify.not_empty(deployment_name, "You must provide a deployment name")
26-
Verify.not_empty(api_key, "The Azure API key cannot be empty")
27-
Verify.not_empty(endpoint, "The Azure endpoint cannot be empty")
28-
Verify.starts_with(
29-
endpoint, "https://", "The Azure endpoint must start with https://"
30-
)
24+
if not deployment_name:
25+
raise ValueError("The deployment name cannot be `None` or empty")
26+
if not api_key:
27+
raise ValueError("The Azure API key cannot be `None` or empty`")
28+
if not endpoint:
29+
raise ValueError("The Azure endpoint cannot be `None` or empty")
30+
if not endpoint.startswith("https://"):
31+
raise ValueError("The Azure endpoint must start with https://")
3132

3233
self._endpoint = endpoint
3334
self._api_version = api_version

python/semantic_kernel/ai/open_ai/services/azure_text_embedding.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from semantic_kernel.ai.open_ai.services.open_ai_text_embedding import (
88
OpenAITextEmbedding,
99
)
10-
from semantic_kernel.diagnostics.verify import Verify
1110

1211

1312
class AzureTextEmbedding(OpenAITextEmbedding):
@@ -22,12 +21,14 @@ def __init__(
2221
api_version: str = "2022-12-01",
2322
logger: Optional[Logger] = None,
2423
) -> None:
25-
Verify.not_empty(deployment_name, "You must provide a deployment name")
26-
Verify.not_empty(api_key, "The Azure API key cannot be empty")
27-
Verify.not_empty(endpoint, "The Azure endpoint cannot be empty")
28-
Verify.starts_with(
29-
endpoint, "https://", "The Azure endpoint must start with https://"
30-
)
24+
if not deployment_name:
25+
raise ValueError("The deployment name cannot be `None` or empty")
26+
if not api_key:
27+
raise ValueError("The Azure API key cannot be `None` or empty`")
28+
if not endpoint:
29+
raise ValueError("The Azure endpoint cannot be `None` or empty")
30+
if not endpoint.startswith("https://"):
31+
raise ValueError("The Azure endpoint must start with https://")
3132

3233
self._endpoint = endpoint
3334
self._api_version = api_version

python/semantic_kernel/ai/open_ai/services/open_ai_chat_completion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from semantic_kernel.ai.ai_exception import AIException
77
from semantic_kernel.ai.chat_completion_client_base import ChatCompletionClientBase
88
from semantic_kernel.ai.chat_request_settings import ChatRequestSettings
9-
from semantic_kernel.diagnostics.verify import Verify
109
from semantic_kernel.utils.null_logger import NullLogger
1110

1211

@@ -65,7 +64,8 @@ async def complete_chat_async(
6564
Returns:
6665
str -- The completed text.
6766
"""
68-
Verify.not_null(request_settings, "The request settings cannot be empty")
67+
if request_settings is None:
68+
raise ValueError("The request settings cannot be `None`")
6969

7070
if request_settings.max_tokens < 1:
7171
raise AIException(

python/semantic_kernel/ai/open_ai/services/open_ai_text_completion.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from semantic_kernel.ai.ai_exception import AIException
77
from semantic_kernel.ai.complete_request_settings import CompleteRequestSettings
88
from semantic_kernel.ai.text_completion_client_base import TextCompletionClientBase
9-
from semantic_kernel.diagnostics.verify import Verify
109
from semantic_kernel.utils.null_logger import NullLogger
1110

1211

@@ -65,8 +64,10 @@ async def complete_simple_async(
6564
Returns:
6665
str -- The completed text.
6766
"""
68-
Verify.not_empty(prompt, "The prompt is empty")
69-
Verify.not_null(request_settings, "The request settings cannot be empty")
67+
if not prompt:
68+
raise ValueError("The prompt cannot be `None` or empty")
69+
if request_settings is None:
70+
raise ValueError("The request settings cannot be `None`")
7071

7172
if request_settings.max_tokens < 1:
7273
raise AIException(

python/semantic_kernel/core_skills/text_memory_skill.py

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

3-
from semantic_kernel.diagnostics.verify import Verify
43
from semantic_kernel.orchestration.sk_context import SKContext
54
from semantic_kernel.skill_definition import sk_function, sk_function_context_parameter
65

@@ -44,19 +43,18 @@ async def recall_async(self, ask: str, context: SKContext) -> str:
4443
Returns:
4544
The nearest item from the memory store
4645
"""
47-
Verify.not_null(context.variables, "Context has no variables")
48-
assert context.variables is not None # for type checker
49-
Verify.not_null(context.memory, "Context has no memory")
50-
assert context.memory is not None # for type checker
46+
if context.variables is None:
47+
raise ValueError("Context has no variables")
48+
if context.memory is None:
49+
raise ValueError("Context has no memory")
5150

5251
collection = (
5352
context.variables[TextMemorySkill.COLLECTION_PARAM]
5453
if context.variables.contains_key(TextMemorySkill.COLLECTION_PARAM)
5554
else TextMemorySkill.DEFAULT_COLLECTION
5655
)
57-
Verify.not_empty(
58-
collection, "Memory collection not defined for TextMemorySkill"
59-
)
56+
if not collection:
57+
raise ValueError("Memory collection not defined for TextMemorySkill")
6058

6159
relevance = (
6260
context.variables[TextMemorySkill.RELEVANCE_PARAM]
@@ -104,26 +102,25 @@ async def save_async(self, text: str, context: SKContext):
104102
context -- Contains the 'collection' to save the information
105103
and unique 'key' to associate with the information
106104
"""
107-
Verify.not_null(context.variables, "Context has no variables")
108-
assert context.variables is not None # for type checker
109-
Verify.not_null(context.memory, "Context has no memory")
110-
assert context.memory is not None # for type checker
105+
if context.variables is None:
106+
raise ValueError("Context has no variables")
107+
if context.memory is None:
108+
raise ValueError("Context has no memory")
111109

112110
collection = (
113111
context.variables[TextMemorySkill.COLLECTION_PARAM]
114112
if context.variables.contains_key(TextMemorySkill.COLLECTION_PARAM)
115113
else TextMemorySkill.DEFAULT_COLLECTION
116114
)
117-
Verify.not_empty(
118-
collection, "Memory collection not defined for TextMemorySkill"
119-
)
115+
if not collection:
116+
raise ValueError("Memory collection not defined for TextMemorySkill")
120117

121118
key = (
122119
context.variables[TextMemorySkill.KEY_PARAM]
123120
if context.variables.contains_key(TextMemorySkill.KEY_PARAM)
124121
else None
125122
)
126-
Verify.not_empty(key, "Memory key not defined for TextMemorySkill")
127-
assert key is not None # for type checker
123+
if not key:
124+
raise ValueError("Memory key not defined for TextMemorySkill")
128125

129126
await context.memory.save_information_async(collection, text=text, id=key)

python/semantic_kernel/diagnostics/sk_exception.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

python/semantic_kernel/diagnostics/validation_exception.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)