forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConversationSummarySkill.cs
113 lines (100 loc) · 4.72 KB
/
ConversationSummarySkill.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Microsoft. All rights reserved.
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SkillDefinition;
using Microsoft.SemanticKernel.Text;
namespace Microsoft.SemanticKernel.Skills.Core;
/// <summary>
/// <para>Semantic skill that enables conversations summarization.</para>
/// </summary>
/// <example>
/// <code>
/// var kernel Kernel.Builder.Build();
/// kernel.ImportSkill(new ConversationSummarySkill(kernel));
/// </code>
/// </example>
public class ConversationSummarySkill
{
/// <summary>
/// The max tokens to process in a single semantic function call.
/// </summary>
private const int MaxTokens = 1024;
private readonly ISKFunction _summarizeConversationFunction;
private readonly ISKFunction _conversationActionItemsFunction;
private readonly ISKFunction _conversationTopicsFunction;
/// <summary>
/// Initializes a new instance of the <see cref="ConversationSummarySkill"/> class.
/// </summary>
/// <param name="kernel">Kernel instance</param>
public ConversationSummarySkill(IKernel kernel)
{
this._summarizeConversationFunction = kernel.CreateSemanticFunction(
SemanticFunctionConstants.SummarizeConversationDefinition,
skillName: nameof(ConversationSummarySkill),
description: "Given a section of a conversation transcript, summarize the part of the conversation.",
maxTokens: MaxTokens,
temperature: 0.1,
topP: 0.5);
this._conversationActionItemsFunction = kernel.CreateSemanticFunction(
SemanticFunctionConstants.GetConversationActionItemsDefinition,
skillName: nameof(ConversationSummarySkill),
description: "Given a section of a conversation transcript, identify action items.",
maxTokens: MaxTokens,
temperature: 0.1,
topP: 0.5);
this._conversationTopicsFunction = kernel.CreateSemanticFunction(
SemanticFunctionConstants.GetConversationTopicsDefinition,
skillName: nameof(ConversationSummarySkill),
description: "Analyze a conversation transcript and extract key topics worth remembering.",
maxTokens: MaxTokens,
temperature: 0.1,
topP: 0.5);
}
/// <summary>
/// Given a long conversation transcript, summarize the conversation.
/// </summary>
/// <param name="input">A long conversation transcript.</param>
/// <param name="context">The SKContext for function execution.</param>
[SKFunction, Description("Given a long conversation transcript, summarize the conversation.")]
public Task<SKContext> SummarizeConversationAsync(
[Description("A long conversation transcript.")] string input,
SKContext context)
{
List<string> lines = TextChunker.SplitPlainTextLines(input, MaxTokens);
List<string> paragraphs = TextChunker.SplitPlainTextParagraphs(lines, MaxTokens);
return this._summarizeConversationFunction
.AggregatePartitionedResultsAsync(paragraphs, context);
}
/// <summary>
/// Given a long conversation transcript, identify action items.
/// </summary>
/// <param name="input">A long conversation transcript.</param>
/// <param name="context">The SKContext for function execution.</param>
[SKFunction, Description("Given a long conversation transcript, identify action items.")]
public Task<SKContext> GetConversationActionItemsAsync(
[Description("A long conversation transcript.")] string input,
SKContext context)
{
List<string> lines = TextChunker.SplitPlainTextLines(input, MaxTokens);
List<string> paragraphs = TextChunker.SplitPlainTextParagraphs(lines, MaxTokens);
return this._conversationActionItemsFunction
.AggregatePartitionedResultsAsync(paragraphs, context);
}
/// <summary>
/// Given a long conversation transcript, identify topics.
/// </summary>
/// <param name="input">A long conversation transcript.</param>
/// <param name="context">The SKContext for function execution.</param>
[SKFunction, Description("Given a long conversation transcript, identify topics worth remembering.")]
public Task<SKContext> GetConversationTopicsAsync(
[Description("A long conversation transcript.")] string input,
SKContext context)
{
List<string> lines = TextChunker.SplitPlainTextLines(input, MaxTokens);
List<string> paragraphs = TextChunker.SplitPlainTextParagraphs(lines, MaxTokens);
return this._conversationTopicsFunction
.AggregatePartitionedResultsAsync(paragraphs, context);
}
}