forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopicSelection.tsx
149 lines (133 loc) · 5.77 KB
/
TopicSelection.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
// Copyright (c) Microsoft. All rights reserved.
import { Body1, Button, Input, Label, Radio, RadioGroup, Spinner, Title3 } from '@fluentui/react-components';
import { FC, useState } from 'react';
import { useSemanticKernel } from '../hooks/useSemanticKernel';
import { IAsk, IAskInput } from '../model/Ask';
import { IKeyConfig } from '../model/KeyConfig';
interface IData {
uri: string;
keyConfig: IKeyConfig;
onTopicSelected: (title: string, detail: string) => void;
onBack: () => void;
}
interface ITopicWithSummary {
title: string;
description: string;
}
const TopicSelection: FC<IData> = ({ uri, keyConfig, onTopicSelected, onBack }) => {
const sk = useSemanticKernel(uri);
const [topic, setTopic] = useState<string>('');
const [topics, setTopics] = useState<ITopicWithSummary[]>();
const [fetchingTopics, setFetchingTopics] = useState<boolean>();
const [selectedTopicIndex, setSelectedTopicIndex] = useState<number>(0);
const fetchTopics = async () => {
if (topic === undefined) {
return;
}
setFetchingTopics(true);
var askInputs: IAskInput[] = [
{
key: 'numIdeas',
value: '4',
},
];
var ask: IAsk = { value: topic, inputs: askInputs };
try {
var result = await sk.invokeAsync(keyConfig, ask, 'childrensbookskill', 'bookideas');
var jsonValue = (result.value as string).substring((result.value as string).indexOf('['));
var results = JSON.parse(jsonValue);
var topics: ITopicWithSummary[] = [];
for (var r of results) {
topics.push({
title: r.title,
description: r.description,
});
}
setTopics(topics);
} catch (e) {
alert('Something went wrong.\n\nDetails:\n' + e);
}
setFetchingTopics(false);
};
return (
<div style={{ padding: 80, gap: 20, display: 'flex', flexDirection: 'column', alignItems: 'left' }}>
<Title3>Enter a topic to generate ideas</Title3>
<Body1>
Start by entering a topic and a list of ideas will be generated for a children's book. Select your
favorite idea to move to the next step, creating the book.
</Body1>
<Body1>
<strong>What's the book topic on your mind?</strong>
</Body1>
<div style={{ gap: 20, display: 'flex', flexDirection: 'row', alignItems: 'left' }}>
<Input
style={{ minWidth: 500, height: 36 }}
size="medium"
appearance="outline"
onKeyUp={(e) => {
if (e.key === 'Enter') {
fetchTopics();
}
}}
value={topic}
onChange={(e, d) => setTopic(d.value)}
placeholder="Type a book topic for a children's book. Example: cup, dragon, cookies..."
/>
<Button appearance="primary" onClick={() => fetchTopics()}>
Get Ideas
</Button>
</div>
{fetchingTopics ? (
<Spinner />
) : (
<div style={{ gap: 20, display: 'flex', flexDirection: 'row', alignItems: 'left', flexWrap: 'wrap' }}>
{topics === undefined ? (
<></>
) : (
<div style={{ gap: 20, display: 'flex', flexDirection: 'column' }}>
<Label weight="semibold">
Here are the generated ideas. Which one would you like to choose for your book?
</Label>
<RadioGroup
required
value={'' + selectedTopicIndex}
onChange={(_, data) => setSelectedTopicIndex(+data.value)}
>
<div style={{ gap: 20, display: 'flex', flexDirection: 'column' }}>
{topics?.map((t, idx) => (
<Radio
key={idx}
value={'' + idx}
label={
<div style={{ gap: 10, display: 'flex', flexDirection: 'column' }}>
<Body1>{t.title}</Body1>
<Body1>{t.description}</Body1>
</div>
}
/>
))}
</div>
</RadioGroup>
</div>
)}
</div>
)}
<div style={{ display: 'flex', flexDirection: 'row', alignItems: 'left', gap: 20 }}>
<Button appearance="secondary" onClick={() => onBack()}>
Back
</Button>
<Button
appearance="primary"
disabled={topics === undefined}
onClick={() => {
if (topics !== undefined)
onTopicSelected(topics[selectedTopicIndex].title, topics[selectedTopicIndex].description);
}}
>
Create Book
</Button>
</div>
</div>
);
};
export default TopicSelection;