mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
### 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. --> - Added static code checking to pre-commit (check-ast and nbqa-check-ast) - Added Bandit security checking to pre-commit - Added pre-commit step to python-lint workflow, if it works, can delete seperate mypy, ruff and black tests ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### 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 😄
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import asyncio
|
|
import datetime
|
|
|
|
from azure.core.credentials import AccessToken
|
|
from azure.core.exceptions import ClientAuthenticationError
|
|
from azure.identity import DefaultAzureCredential
|
|
|
|
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion
|
|
from semantic_kernel.core_plugins.sessions_python_tool.sessions_python_plugin import SessionsPythonTool
|
|
from semantic_kernel.exceptions.function_exceptions import FunctionExecutionException
|
|
from semantic_kernel.kernel import Kernel
|
|
|
|
auth_token: AccessToken | None = None
|
|
|
|
ACA_TOKEN_ENDPOINT: str = "https://acasessions.io/.default" # nosec
|
|
|
|
|
|
async def auth_callback() -> str:
|
|
"""Auth callback for the SessionsPythonTool.
|
|
This is a sample auth callback that shows how to use Azure's DefaultAzureCredential
|
|
to get an access token.
|
|
"""
|
|
global auth_token
|
|
current_utc_timestamp = int(datetime.datetime.now(datetime.timezone.utc).timestamp())
|
|
|
|
if not auth_token or auth_token.expires_on < current_utc_timestamp:
|
|
credential = DefaultAzureCredential()
|
|
|
|
try:
|
|
auth_token = credential.get_token(ACA_TOKEN_ENDPOINT)
|
|
except ClientAuthenticationError as cae:
|
|
err_messages = getattr(cae, "messages", [])
|
|
raise FunctionExecutionException(
|
|
f"Failed to retrieve the client auth token with messages: {' '.join(err_messages)}"
|
|
) from cae
|
|
|
|
return auth_token.token
|
|
|
|
|
|
async def main():
|
|
kernel = Kernel()
|
|
|
|
service_id = "python-code-interpreter"
|
|
chat_service = AzureChatCompletion(
|
|
service_id=service_id,
|
|
)
|
|
kernel.add_service(chat_service)
|
|
|
|
python_code_interpreter = SessionsPythonTool(auth_callback=auth_callback)
|
|
|
|
sessions_tool = kernel.add_plugin(python_code_interpreter, "PythonCodeInterpreter")
|
|
|
|
code = "import json\n\ndef add_numbers(a, b):\n return a + b\n\nargs = '{\"a\": 1, \"b\": 1}'\nargs_dict = json.loads(args)\nprint(add_numbers(args_dict['a'], args_dict['b']))" # noqa: E501
|
|
result = await kernel.invoke(sessions_tool["execute_code"], code=code)
|
|
|
|
print(result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|