-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working templating with handlebars for both samples 1 and 2
- Loading branch information
1 parent
db4c9e4
commit f604287
Showing
11 changed files
with
198 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pyyaml | |
pydantic | ||
jinja2 | ||
pybars3 | ||
ruff | ||
ruff | ||
black |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Any | ||
|
||
from pybars import Compiler | ||
from pydantic import PrivateAttr | ||
from semantic_kernel.sk_pydantic import SKBaseModel | ||
|
||
|
||
def _message(this, options, **kwargs): | ||
# single message call, scope is messages object as context | ||
# in messages loop, scope is ChatMessage object as context | ||
if "role" in kwargs: | ||
return f'<message role="{kwargs["role"]}">{options["fn"](this)}</message>' | ||
|
||
|
||
# TODO: render functions are helpers | ||
|
||
|
||
class HandleBarsPromptTemplateHandler(SKBaseModel): | ||
template: str | ||
_template_compiler: Any = PrivateAttr() | ||
|
||
def __init__(self, template: str): | ||
super().__init__(template=template) | ||
compiler = Compiler() | ||
self._template_compiler = compiler.compile(self.template) | ||
|
||
def render(self, variables: dict) -> str: | ||
return self._template_compiler(variables, helpers={"message": _message}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.