forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AISummary.tsx
184 lines (166 loc) · 6.41 KB
/
AISummary.tsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft. All rights reserved.
import { Body1, Button, Caption1, Spinner, Subtitle1, Subtitle2, Title3 } from '@fluentui/react-components';
import { Card, CardHeader } from '@fluentui/react-components/unstable';
import { FC, useEffect, useState } from 'react';
import { useSemanticKernel } from '../hooks/useSemanticKernel';
import { IKeyConfig } from '../model/KeyConfig';
import { IChatMessage } from './chat/ChatThread';
interface IData {
uri: string;
chat: IChatMessage[];
keyConfig: IKeyConfig;
onBack: () => void;
}
const AISummary: FC<IData> = ({ uri, chat, keyConfig, onBack }) => {
const sk = useSemanticKernel(uri);
const [summary, setSummary] = useState<string>();
const [actionItems, setActionItems] = useState<string>();
const [topics, setTopics] = useState<string>();
const getSummary = async (ask: any) => {
try {
var result = await sk.invokeAsync(keyConfig, ask, 'ConversationSummarySkill', 'SummarizeConversation');
setSummary(result.value);
} catch (e) {
alert('Something went wrong.\n\nDetails:\n' + e);
}
};
const getActionItems = async (ask: any) => {
try {
var result = await sk.invokeAsync(keyConfig, ask, 'ConversationSummarySkill', 'GetConversationActionItems');
setActionItems(result.value);
} catch (e) {
alert('Something went wrong.\n\nDetails:\n' + e);
}
};
const formatActionItems = (actionItems: string): JSX.Element => {
var actionItemsJson = JSON.parse(
'[' +
actionItems
.split('}\n\n{')
.map((item, index, array) => {
if (array.length === 1) {
return item;
} else if (index === 0) {
return item + '}';
} else if (index === array.length - 1) {
return '{' + item;
} else {
return '{' + item + '}';
}
})
.join(',') +
']',
);
var actionItemsList = actionItemsJson.reduce((acc: any, cur: any) => {
return acc.concat(cur.actionItems);
}, []);
var actionItemsFormatted = actionItemsList.map((actionItem: any) => {
return (
<Card>
<CardHeader header={<Subtitle2>{actionItem.actionItem}</Subtitle2>} />
<Body1>
<b>Owner:</b> {actionItem.owner}
</Body1>
<Body1>
<b>Due Date:</b> {actionItem.dueDate}
</Body1>
</Card>
);
});
return (
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr 1fr', gap: 20 }}>{actionItemsFormatted}</div>
);
};
const getTopics = async (ask: any) => {
try {
var result = await sk.invokeAsync(keyConfig, ask, 'ConversationSummarySkill', 'GetConversationTopics');
setTopics(result.value);
} catch (e) {
alert('Something went wrong.\n\nDetails:\n' + e);
}
};
const formatTopics = (topics: string): JSX.Element => {
var topicsJson = JSON.parse(
'[' +
topics
.split('}\n\n{')
.map((item, index, array) => {
if (array.length === 1) {
return item;
} else if (index === 0) {
return item + '}';
} else if (index === array.length - 1) {
return '{' + item;
} else {
return '{' + item + '}';
}
})
.join(',') +
']',
);
var topicsList = topicsJson.reduce((acc: any, cur: any) => {
return acc.concat(cur.topics);
}, []);
var topicsFormatted = topicsList.map((topic: any) => {
return (
<Card appearance="outline" size="small">
<Caption1>{topic}</Caption1>
</Card>
);
});
return (
<div style={{ display: 'flex', flexDirection: 'row', flexWrap: 'wrap', gap: 10 }}>{topicsFormatted}</div>
);
};
useEffect(() => {
const fetchAsync = async () => {
var ask = {
value: chat.map((c) => (c.mine ? `I said: ${c.content}` : `${c.author} said: ${c.content}`)).join('\n'),
};
await getSummary(ask);
await getActionItems(ask);
await getTopics(ask);
};
fetchAsync();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [uri]);
return (
<div style={{ padding: 80, gap: 20, display: 'flex', flexDirection: 'column', alignItems: 'left' }}>
<Title3>AI summaries based on your chat</Title3>
<Body1>
We used skills to summarise the conversations from the chat, along with found topics and action items.
</Body1>
<br />
<Subtitle1>
<strong>Summary</strong>
</Subtitle1>
{summary === undefined ? (
<></>
) : (
<Body1>{summary.length > 0 ? summary : 'We could not create a summary.'}</Body1>
)}
{topics === undefined ? (
<></>
) : (
<Body1>{topics.length > 0 ? formatTopics(topics) : "We didn't find any topics."}</Body1>
)}
{summary === undefined || topics === undefined ? <Spinner /> : <div style={{ height: 20 }} />}
<Subtitle1>
<strong>Action Items</strong>
</Subtitle1>
{actionItems === undefined ? (
<Spinner />
) : (
<Body1>
{actionItems.length > 0 ? formatActionItems(actionItems) : "We didn't find any action items."}
</Body1>
)}
<br />
<br />
<Button style={{ width: 54 }} appearance="secondary" onClick={() => onBack()}>
Back
</Button>
</div>
);
};
export default AISummary;