forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Python] Conversation summary skill porting part I (microsoft#853)
### Motivation and Context Porting conversation summary skill. Integration test is added as well. Action item and topic will be ported later. --------- Co-authored-by: Po-Wei Huang <[email protected]> Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Abby Harrison <[email protected]> Co-authored-by: Devis Lucato <[email protected]> Co-authored-by: Devis Lucato <[email protected]>
- Loading branch information
1 parent
5731030
commit 9fccbe4
Showing
5 changed files
with
180 additions
and
1 deletion.
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
63 changes: 63 additions & 0 deletions
63
python/semantic_kernel/core_skills/conversation_summary_skill.py
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,63 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
from semantic_kernel.kernel import Kernel | ||
from semantic_kernel.orchestration.sk_context import SKContext | ||
from semantic_kernel.skill_definition import sk_function | ||
from semantic_kernel.text import text_chunker | ||
from semantic_kernel.text.function_extension import aggregate_chunked_results_async | ||
|
||
|
||
class ConversationSummarySkill: | ||
""" | ||
Semantic skill that enables conversations summarization. | ||
""" | ||
|
||
# The max tokens to process in a single semantic function call. | ||
_max_tokens = 1024 | ||
|
||
_summarize_conversation_prompt_template = ( | ||
"BEGIN CONTENT TO SUMMARIZE:\n" | ||
"{{" + "$INPUT" + "}}\n" | ||
"END CONTENT TO SUMMARIZE.\n" | ||
"Summarize the conversation in 'CONTENT TO SUMMARIZE',\ | ||
identifying main points of discussion and any conclusions that were reached.\n" | ||
"Do not incorporate other general knowledge.\n" | ||
"Summary is in plain text, in complete sentences, with no markup or tags.\n" | ||
"\nBEGIN SUMMARY:\n" | ||
) | ||
|
||
def __init__(self, kernel: Kernel): | ||
self._summarizeConversationFunction = kernel.create_semantic_function( | ||
ConversationSummarySkill._summarize_conversation_prompt_template, | ||
skill_name=ConversationSummarySkill.__name__, | ||
description="Given a section of a conversation transcript, summarize the part of the conversation.", | ||
max_tokens=ConversationSummarySkill._max_tokens, | ||
temperature=0.1, | ||
top_p=0.5, | ||
) | ||
|
||
@sk_function( | ||
description="Given a long conversation transcript, summarize the conversation.", | ||
name="SummarizeConversation", | ||
input_description="A long conversation transcript.", | ||
) | ||
async def summarize_conversation_async( | ||
self, input: str, context: SKContext | ||
) -> SKContext: | ||
""" | ||
Given a long conversation transcript, summarize the conversation. | ||
:param input: A long conversation transcript. | ||
:param context: The SKContext for function execution. | ||
:return: SKContext with the summarized conversation result. | ||
""" | ||
lines = text_chunker._split_text_lines( | ||
input, ConversationSummarySkill._max_tokens, True | ||
) | ||
paragraphs = text_chunker._split_text_paragraph( | ||
lines, ConversationSummarySkill._max_tokens | ||
) | ||
|
||
return await aggregate_chunked_results_async( | ||
self._summarizeConversationFunction, paragraphs, context | ||
) |
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
55 changes: 55 additions & 0 deletions
55
python/tests/integration/completions/test_conversation_summary_skill.py
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,55 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
import os | ||
|
||
import e2e_text_completion | ||
import pytest | ||
|
||
import semantic_kernel as sk | ||
import semantic_kernel.connectors.ai.open_ai as sk_oai | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_azure_summarize_conversation_using_skill(): | ||
kernel = sk.Kernel() | ||
|
||
if "Python_Integration_Tests" in os.environ: | ||
deployment_name = os.environ["AzureOpenAI__DeploymentName"] | ||
api_key = os.environ["AzureOpenAI__ApiKey"] | ||
endpoint = os.environ["AzureOpenAI__Endpoint"] | ||
else: | ||
# Load credentials from .env file | ||
deployment_name, api_key, endpoint = sk.azure_openai_settings_from_dot_env() | ||
deployment_name = "text-davinci-003" | ||
|
||
kernel.add_text_completion_service( | ||
"text_completion", | ||
sk_oai.AzureTextCompletion(deployment_name, endpoint, api_key), | ||
) | ||
|
||
await e2e_text_completion.summarize_conversation_using_skill(kernel) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_oai_summarize_conversation_using_skill(): | ||
kernel = sk.Kernel() | ||
|
||
if "Python_Integration_Tests" in os.environ: | ||
api_key = os.environ["OpenAI__ApiKey"] | ||
org_id = None | ||
else: | ||
# Load credentials from .env file | ||
api_key, org_id = sk.openai_settings_from_dot_env() | ||
|
||
kernel.add_chat_service( | ||
"davinci-003", | ||
sk_oai.OpenAITextCompletion("text-davinci-003", api_key, org_id=org_id), | ||
) | ||
|
||
await e2e_text_completion.summarize_conversation_using_skill(kernel) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(test_oai_summarize_conversation_using_skill()) | ||
asyncio.run(test_azure_summarize_conversation_using_skill()) |