-
I have a need to compose a series of function calls that occur in a specific order:
The 3rd function innately calls the second in the prompt template, and this seems to work as expected. Is this workflow possible? If not, are there known or suggested workarounds? A pseudocode example: kernel = Kernel()
INTENTIONS = [...]
IntentionEnum = Enum("Intention", INTENTIONS)
# 1: identify user intention
intention_cfg = PromptTemplateConfig(
name="identify_intention",
description="identify user intention",
template="""
{{$chat_history}}
<message role="system"><text>
Identify user intention from {{$intentions}}
</text></message>
""",
template_format="semantic-kernel",
input_variables=[
InputVariable(name="chat_history, description="the chat history", is_required=False, default=""),
],
)
intention = kernel.add_function(
plugin_name="demo",
function_name=intention_cfg.name,
description=intention_cfg.description,
prompt_template_config=intention_cfg,
...
)
# 2. dynamically render fewshot examples
@kernel_function
def fewshot_examples(
intention: Annotated[IntentionEnum, "the user's intention"],
) -> Annotated[str, "fewshot examples for the generation prompt"]:
"""Dynamically render examples."""
from jinja2 import Template
template = """
<examples>
{% for example in examples %}
<example>
{{example}}
</example>
{% endfor %}
</examples>
""".strip()
# EXAMPLES is dict(intention, [fewshot-examples])
examples = Template(template).render(examples=EXAMPLES[intention])
return examples
fewshot_examples = kernel.add_function(
plugin_name="demo,
function_name="fewshot_examples",
description="Dynamically render fewshot examples based on identified intention",
function=fewshot_examples,
)
# 3. complete some action based on 1,2
action_cfg = PromptTemplateConfig(
name="take_action",
description="complete the action implied by the user intention",
template="""
{{$chat_history}}
<message role="system"><text>
Based on user's request and {{$intention}}, do <X, Y, Z>.
<examples>
{{demo.fewshot_examples $intention}}
</examples>
</text></message>
""",
template_format="semantic-kernel",
input_variables=[
InputVariable(name="chat_history, description="the chat history", is_required=False, default=""),
InputVariable(name="intention, description="the user's intention", is_required=True),
],
)
action = kernel.add_function(
plugin_name="demo",
function_name=action_cfg.name,
description=action_cfg.description,
prompt_template_config=action_cfg,
...
) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @ahgraber, thanks for your question. We don't have a specific sample right now showing this in Python, but we do have a "chaining" example in SK .Net. Perhaps you could have a look to see if it can help your use-case? |
Beta Was this translation helpful? Give feedback.
Hi @ahgraber, thanks for your question. We don't have a specific sample right now showing this in Python, but we do have a "chaining" example in SK .Net. Perhaps you could have a look to see if it can help your use-case?
https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Functions/MethodFunctions_Advanced.cs