Files
semantic-kernel/python/semantic_kernel/prompt_template/utils/template_function_helpers.py
T
Eduard van Valkenburg ba9af0e07b Python: mypy coverage enhancement (#6250)
### 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.
-->
extend mypy coverage for prompt_templates, template_engine and some
other smaller folders.

still todo: connectors, memory and planner folders.

### 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 😄
2024-05-31 16:37:19 +00:00

70 lines
2.3 KiB
Python

# Copyright (c) Microsoft. All rights reserved.
import asyncio
import logging
from collections.abc import Callable
from html import escape
from typing import TYPE_CHECKING, Any
import nest_asyncio
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.prompt_template.const import (
HANDLEBARS_TEMPLATE_FORMAT_NAME,
JINJA2_TEMPLATE_FORMAT_NAME,
TEMPLATE_FORMAT_TYPES,
)
if TYPE_CHECKING:
from semantic_kernel.functions.kernel_function import KernelFunction
from semantic_kernel.kernel import Kernel
logger: logging.Logger = logging.getLogger(__name__)
def create_template_helper_from_function(
function: "KernelFunction",
kernel: "Kernel",
base_arguments: "KernelArguments",
template_format: TEMPLATE_FORMAT_TYPES,
allow_dangerously_set_content: bool = False,
) -> Callable[..., Any]:
"""Create a helper function for both the Handlebars and Jinja2 templating engines from a kernel function."""
if template_format not in [JINJA2_TEMPLATE_FORMAT_NAME, HANDLEBARS_TEMPLATE_FORMAT_NAME]:
raise ValueError(f"Invalid template format: {template_format}")
if not getattr(asyncio, "_nest_patched", False):
nest_asyncio.apply()
def func(*args, **kwargs):
arguments = KernelArguments()
if base_arguments and base_arguments.execution_settings:
arguments.execution_settings = base_arguments.execution_settings # pragma: no cover
arguments.update(base_arguments)
arguments.update(kwargs)
if len(args) > 0 and template_format == HANDLEBARS_TEMPLATE_FORMAT_NAME:
this = args[0]
actual_args = args[1:]
else:
this = None
actual_args = args
if this is not None:
logger.debug(f"Handlebars context with `this`: {this}")
else:
logger.debug("Jinja2 context or Handlebars context without `this`")
logger.debug(
f"Invoking function {function.metadata.fully_qualified_name} "
f"with args: {actual_args} and kwargs: {kwargs} and this: {this}."
)
result = asyncio.run(function.invoke(kernel=kernel, arguments=arguments))
if allow_dangerously_set_content:
return result
return escape(str(result))
return func