From e08961760f8344c35170f4befa927fe957897968 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Mon, 18 Nov 2024 13:02:55 -0600 Subject: [PATCH 1/4] updated pre-commits --- python/.pre-commit-config.yaml | 8 +++++--- python/pyproject.toml | 2 +- python/uv.lock | 4 ++-- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/python/.pre-commit-config.yaml b/python/.pre-commit-config.yaml index 1efb48ece334..0f72ca9616fc 100644 --- a/python/.pre-commit-config.yaml +++ b/python/.pre-commit-config.yaml @@ -32,19 +32,21 @@ repos: - id: pyupgrade args: [--py310-plus] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.7.1 + rev: v0.7.4 hooks: - id: ruff args: [ --fix, --exit-non-zero-on-fix ] - id: ruff-format - repo: https://github.com/astral-sh/uv-pre-commit # uv version. - rev: 0.4.30 + rev: 0.5.2 hooks: # Update the uv lockfile - id: uv-lock + files: python/pyproject.toml + args: [--project, python] - repo: https://github.com/PyCQA/bandit - rev: 1.7.8 + rev: 1.7.10 hooks: - id: bandit args: ["-c", "python/pyproject.toml"] diff --git a/python/pyproject.toml b/python/pyproject.toml index 6d420efcb6e1..aeb4379a5414 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -136,7 +136,7 @@ dev-dependencies = [ "snoop ~= 0.4", "mypy >= 1.10", "types-PyYAML ~= 6.0.12.20240311", - "ruff ~= 0.5" + "ruff ~= 0.7" ] environments = [ "sys_platform == 'darwin'", diff --git a/python/uv.lock b/python/uv.lock index dde2b5e554d7..8b288a410307 100644 --- a/python/uv.lock +++ b/python/uv.lock @@ -4653,7 +4653,7 @@ wheels = [ [[package]] name = "semantic-kernel" -version = "1.14.0" +version = "1.15.0" source = { editable = "." } dependencies = [ { name = "aiohttp", marker = "sys_platform == 'darwin' or sys_platform == 'linux' or sys_platform == 'win32'" }, @@ -4826,7 +4826,7 @@ dev = [ { name = "pytest-asyncio", specifier = "~=0.23" }, { name = "pytest-cov", specifier = ">=5.0" }, { name = "pytest-xdist", extras = ["psutil"], specifier = "~=3.6" }, - { name = "ruff", specifier = "~=0.5" }, + { name = "ruff", specifier = "~=0.7" }, { name = "snoop", specifier = "~=0.4" }, { name = "types-pyyaml", specifier = "~=6.0.12.20240311" }, ] From 3b16a83dafa6cebc4efbe8eed46e5443b3b2d420 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Mon, 18 Nov 2024 13:15:56 -0600 Subject: [PATCH 2/4] fixed new ruff checks --- python/semantic_kernel/connectors/search/__init__.py | 0 python/semantic_kernel/connectors/utils/document_loader.py | 2 +- .../functions/kernel_function_from_prompt.py | 6 ++++-- python/semantic_kernel/kernel_types.py | 2 +- .../prompt_template/prompt_template_config.py | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 python/semantic_kernel/connectors/search/__init__.py diff --git a/python/semantic_kernel/connectors/search/__init__.py b/python/semantic_kernel/connectors/search/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/python/semantic_kernel/connectors/utils/document_loader.py b/python/semantic_kernel/connectors/utils/document_loader.py index 2cc35075ed89..de72bdad6d98 100644 --- a/python/semantic_kernel/connectors/utils/document_loader.py +++ b/python/semantic_kernel/connectors/utils/document_loader.py @@ -19,7 +19,7 @@ class DocumentLoader: async def from_uri( url: str, http_client: AsyncClient, - auth_callback: Callable[..., None | Awaitable[dict[str, str]]] | None, + auth_callback: Callable[..., Awaitable[dict[str, str]] | None] | None, user_agent: str | None = HTTP_USER_AGENT, ): """Load the manifest from the given URL.""" diff --git a/python/semantic_kernel/functions/kernel_function_from_prompt.py b/python/semantic_kernel/functions/kernel_function_from_prompt.py index fb96ab5f3b71..8a2cdd1b63b3 100644 --- a/python/semantic_kernel/functions/kernel_function_from_prompt.py +++ b/python/semantic_kernel/functions/kernel_function_from_prompt.py @@ -63,8 +63,10 @@ def __init__( template_format: TEMPLATE_FORMAT_TYPES = KERNEL_TEMPLATE_FORMAT_NAME, prompt_template: PromptTemplateBase | None = None, prompt_template_config: PromptTemplateConfig | None = None, - prompt_execution_settings: None - | (PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings]) = None, + prompt_execution_settings: PromptExecutionSettings + | list[PromptExecutionSettings] + | dict[str, PromptExecutionSettings] + | None = None, ) -> None: """Initializes a new instance of the KernelFunctionFromPrompt class. diff --git a/python/semantic_kernel/kernel_types.py b/python/semantic_kernel/kernel_types.py index 5bbfdb5fe3d6..17c9021ba440 100644 --- a/python/semantic_kernel/kernel_types.py +++ b/python/semantic_kernel/kernel_types.py @@ -10,6 +10,6 @@ T = TypeVar("T") OneOrMany = Union[T, Sequence[T]] -OptionalOneOrMany = Union[None, T, Sequence[T]] +OptionalOneOrMany = Union[T, Sequence[T], None] __all__ = ["AI_SERVICE_CLIENT_TYPE", "OneOrMany", "OptionalOneOrMany"] diff --git a/python/semantic_kernel/prompt_template/prompt_template_config.py b/python/semantic_kernel/prompt_template/prompt_template_config.py index 2fdf8e04e32c..4651f933c22a 100644 --- a/python/semantic_kernel/prompt_template/prompt_template_config.py +++ b/python/semantic_kernel/prompt_template/prompt_template_config.py @@ -52,7 +52,7 @@ def check_input_variables(self): @classmethod def rewrite_execution_settings( cls, - settings: None | (PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings]), + settings: PromptExecutionSettings | list[PromptExecutionSettings] | dict[str, PromptExecutionSettings] | None, ) -> dict[str, PromptExecutionSettings]: """Rewrite execution settings to a dictionary.""" if not settings: From 2db039d0c54ba44cdd30502f8825101581a4d47d Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Mon, 18 Nov 2024 13:25:46 -0600 Subject: [PATCH 3/4] add timeouts to httpx --- .../samples/concepts/plugins/openai_plugin_azure_key_vault.py | 2 +- python/samples/learn_resources/plugins/GithubPlugin/github.py | 2 +- .../connectors/openapi_plugin/openapi_runner.py | 2 +- python/semantic_kernel/connectors/search/bing/bing_search.py | 2 +- .../semantic_kernel/connectors/search/google/google_search.py | 2 +- .../connectors/search_engine/bing_connector.py | 2 +- .../connectors/search_engine/google_connector.py | 2 +- python/semantic_kernel/functions/kernel_plugin.py | 4 +++- 8 files changed, 10 insertions(+), 8 deletions(-) diff --git a/python/samples/concepts/plugins/openai_plugin_azure_key_vault.py b/python/samples/concepts/plugins/openai_plugin_azure_key_vault.py index cb142209eb5b..f206d7794f78 100644 --- a/python/samples/concepts/plugins/openai_plugin_azure_key_vault.py +++ b/python/samples/concepts/plugins/openai_plugin_azure_key_vault.py @@ -241,7 +241,7 @@ async def main() -> None: openai_spec = load_and_update_openai_spec() - http_client = httpx.AsyncClient() + http_client = httpx.AsyncClient(timeout=5) await kernel.add_plugin_from_openai( plugin_name="AzureKeyVaultPlugin", diff --git a/python/samples/learn_resources/plugins/GithubPlugin/github.py b/python/samples/learn_resources/plugins/GithubPlugin/github.py index ae3a32a5227e..4f06fe9bdd62 100644 --- a/python/samples/learn_resources/plugins/GithubPlugin/github.py +++ b/python/samples/learn_resources/plugins/GithubPlugin/github.py @@ -102,7 +102,7 @@ def create_client(self) -> httpx.AsyncClient: "Authorization": f"Bearer {self.settings.token}", "X-GitHub-Api-Version": "2022-11-28", } - return httpx.AsyncClient(base_url=self.settings.base_url, headers=headers) + return httpx.AsyncClient(base_url=self.settings.base_url, headers=headers, timeout=5) @staticmethod def build_query(path: str, key: str, value: str) -> str: diff --git a/python/semantic_kernel/connectors/openapi_plugin/openapi_runner.py b/python/semantic_kernel/connectors/openapi_plugin/openapi_runner.py index 09869445dc05..540ee6fc7845 100644 --- a/python/semantic_kernel/connectors/openapi_plugin/openapi_runner.py +++ b/python/semantic_kernel/connectors/openapi_plugin/openapi_runner.py @@ -178,7 +178,7 @@ async def make_request(client: httpx.AsyncClient): if hasattr(self, "http_client") and self.http_client is not None: return await make_request(self.http_client) - async with httpx.AsyncClient() as client: + async with httpx.AsyncClient(timeout=5) as client: return await make_request(client) return await fetch() diff --git a/python/semantic_kernel/connectors/search/bing/bing_search.py b/python/semantic_kernel/connectors/search/bing/bing_search.py index eaea2aac8e2f..21a5ffb7c54c 100644 --- a/python/semantic_kernel/connectors/search/bing/bing_search.py +++ b/python/semantic_kernel/connectors/search/bing/bing_search.py @@ -168,7 +168,7 @@ async def _inner_search(self, query: str, options: TextSearchOptions) -> BingSea "user_agent": SEMANTIC_KERNEL_USER_AGENT, } try: - async with AsyncClient() as client: + async with AsyncClient(timeout=5) as client: response = await client.get(url, headers=headers, params=params) response.raise_for_status() return BingSearchResponse.model_validate_json(response.text) diff --git a/python/semantic_kernel/connectors/search/google/google_search.py b/python/semantic_kernel/connectors/search/google/google_search.py index 924b929496fd..6219283029f2 100644 --- a/python/semantic_kernel/connectors/search/google/google_search.py +++ b/python/semantic_kernel/connectors/search/google/google_search.py @@ -159,7 +159,7 @@ async def _inner_search(self, query: str, options: TextSearchOptions) -> GoogleS full_url = f"{CUSTOM_SEARCH_URL}{self._build_query(query, options)}" headers = {"user_agent": SEMANTIC_KERNEL_USER_AGENT} try: - async with AsyncClient() as client: + async with AsyncClient(timeout=5) as client: response = await client.get(full_url, headers=headers) response.raise_for_status() return GoogleSearchResponse.model_validate_json(response.text) diff --git a/python/semantic_kernel/connectors/search_engine/bing_connector.py b/python/semantic_kernel/connectors/search_engine/bing_connector.py index 93dea06217b1..3236a9bc0794 100644 --- a/python/semantic_kernel/connectors/search_engine/bing_connector.py +++ b/python/semantic_kernel/connectors/search_engine/bing_connector.py @@ -79,7 +79,7 @@ async def search(self, query: str, num_results: int = 1, offset: int = 0) -> lis headers = {"Ocp-Apim-Subscription-Key": self._settings.api_key.get_secret_value()} try: - async with AsyncClient() as client: + async with AsyncClient(timeout=5) as client: response = await client.get(request_url, headers=headers) response.raise_for_status() data = response.json() diff --git a/python/semantic_kernel/connectors/search_engine/google_connector.py b/python/semantic_kernel/connectors/search_engine/google_connector.py index a0b286e20819..9999638a1014 100644 --- a/python/semantic_kernel/connectors/search_engine/google_connector.py +++ b/python/semantic_kernel/connectors/search_engine/google_connector.py @@ -88,7 +88,7 @@ async def search(self, query: str, num_results: int = 1, offset: int = 0) -> lis logger.info("Sending GET request to Google Search API.") try: - async with AsyncClient() as client: + async with AsyncClient(timeout=5) as client: response = await client.get(request_url) response.raise_for_status() data = response.json() diff --git a/python/semantic_kernel/functions/kernel_plugin.py b/python/semantic_kernel/functions/kernel_plugin.py index 14469ac86760..296d8beb562e 100644 --- a/python/semantic_kernel/functions/kernel_plugin.py +++ b/python/semantic_kernel/functions/kernel_plugin.py @@ -420,7 +420,9 @@ async def from_openai( openai_manifest = plugin_str elif plugin_url is not None: # Load plugin from the URL - http_client = execution_parameters.http_client if execution_parameters.http_client else httpx.AsyncClient() + http_client = ( + execution_parameters.http_client if execution_parameters.http_client else httpx.AsyncClient(timeout=5) + ) openai_manifest = await DocumentLoader.from_uri( url=plugin_url, http_client=http_client, auth_callback=None, user_agent=execution_parameters.user_agent ) From bf7db8fbb2ddf1d66113145563f8f70395efeb24 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Mon, 18 Nov 2024 13:27:59 -0600 Subject: [PATCH 4/4] timeout in sessions --- .../core_plugins/sessions_python_tool/sessions_python_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py index c2fba26a5ff9..7467ac6cd03b 100644 --- a/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py +++ b/python/semantic_kernel/core_plugins/sessions_python_tool/sessions_python_plugin.py @@ -63,7 +63,7 @@ def __init__( settings = SessionsPythonSettings() if not http_client: - http_client = AsyncClient() + http_client = AsyncClient(timeout=5) if auth_callback is None: auth_callback = self._default_auth_callback(aca_settings)