-
Notifications
You must be signed in to change notification settings - Fork 3k
/
conversation_summary_plugin.py
77 lines (65 loc) · 3.26 KB
/
conversation_summary_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright (c) Microsoft. All rights reserved.
from typing import TYPE_CHECKING, Annotated
if TYPE_CHECKING:
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel
from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig
class ConversationSummaryPlugin:
"""
Semantic plugin that enables conversations summarization.
"""
from semantic_kernel.functions.kernel_function_decorator import kernel_function
# The max tokens to process in a single semantic function call.
_max_tokens = 1024
_summarize_conversation_prompt_template = (
"BEGIN CONTENT TO SUMMARIZE:\n{{"
+ "$input"
+ "}}\nEND CONTENT TO SUMMARIZE.\nSummarize the conversation in 'CONTENT TO"
" SUMMARIZE', identifying main points of discussion and any"
" conclusions that were reached.\nDo not incorporate other general"
" knowledge.\nSummary is in plain text, in complete sentences, with no markup"
" or tags.\n\nBEGIN SUMMARY:\n"
)
def __init__(
self, kernel: "Kernel", prompt_template_config: "PromptTemplateConfig", return_key: str = "summary"
) -> None:
"""
Initializes a new instance of the ConversationSummaryPlugin class.
:param kernel: The kernel instance.
:param prompt_template_config: The prompt template configuration.
:param return_key: The key to use for the return value.
"""
self.return_key = return_key
self._summarizeConversationFunction = kernel.add_function(
prompt=ConversationSummaryPlugin._summarize_conversation_prompt_template,
plugin_name=ConversationSummaryPlugin.__name__,
function_name="SummarizeConversation",
prompt_template_config=prompt_template_config,
)
@kernel_function(
description="Given a long conversation transcript, summarize the conversation.",
name="SummarizeConversation",
)
async def summarize_conversation(
self,
input: Annotated[str, "A long conversation transcript."],
kernel: Annotated["Kernel", "The kernel instance."],
arguments: Annotated["KernelArguments", "Arguments used by the kernel."],
) -> Annotated[
"KernelArguments", "KernelArguments with the summarized conversation result in key self.return_key."
]:
"""
Given a long conversation transcript, summarize the conversation.
:param input: A long conversation transcript.
:param kernel: The kernel for function execution.
:param arguments: Arguments used by the kernel.
:return: KernelArguments with the summarized conversation result in key self.return_key.
"""
from semantic_kernel.text import text_chunker
from semantic_kernel.text.function_extension import aggregate_chunked_results
lines = text_chunker._split_text_lines(input, ConversationSummaryPlugin._max_tokens, True)
paragraphs = text_chunker._split_text_paragraph(lines, ConversationSummaryPlugin._max_tokens)
arguments[self.return_key] = await aggregate_chunked_results(
self._summarizeConversationFunction, paragraphs, kernel, arguments
)
return arguments