Skip to content

Commit

Permalink
Python: add restore method to chat_prompt_template (microsoft#2495)
Browse files Browse the repository at this point in the history
### 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.
-->
In order to add ways to save and restore a running conversation this
adds a restore method and a property to the ChatPromptTemplate class.

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->
1. Add messages property which provides a view to the role,
message.template tuple
2. Add restore classmethod that takes the same input as a regular init,
but adds a list of tuples, it then loops through that list and adds each
message.

### 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 😄

---------

Co-authored-by: Abby Harrison <54643756+awharrison-28@users.noreply.github.com>
Co-authored-by: Devis Lucato <dluc@users.noreply.github.com>
  • Loading branch information
3 people authored and SOE-YoungS committed Oct 31, 2023
1 parent e9cc0f8 commit 1025f44
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.

from logging import Logger
from typing import TYPE_CHECKING, List, Optional, Tuple
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple

from semantic_kernel.semantic_functions.prompt_template import PromptTemplate
from semantic_kernel.semantic_functions.prompt_template_config import (
Expand Down Expand Up @@ -61,3 +61,27 @@ async def render_messages_async(
rendered_messages.append(("user", latest_user_message))

return rendered_messages

@property
def messages(self) -> List[Dict[str, str]]:
"""Return the messages as a list of tuples of role and message."""
return [
{"role": role, "message": message._template}
for role, message in self._messages
]

@classmethod
def restore(
cls,
messages: List[Dict[str, str]],
template: str,
template_engine: PromptTemplatingEngine,
prompt_config: PromptTemplateConfig,
log: Optional[Logger] = None,
) -> "ChatPromptTemplate":
"""Restore a ChatPromptTemplate from a list of role and message pairs."""
chat_template = cls(template, template_engine, prompt_config, log)
for message in messages:
chat_template.add_message(message["role"], message["message"])

return chat_template

0 comments on commit 1025f44

Please sign in to comment.