-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
89 lines (80 loc) · 2.38 KB
/
index.js
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
import React, { useState } from 'react';
import { useIntl } from 'react-intl';
import { TextInput } from '@strapi/design-system/TextInput';
import { Stack } from '@strapi/design-system/Stack';
import { Button } from '@strapi/design-system/Button';
import { Textarea } from '@strapi/design-system';
import { auth } from '@strapi/helper-plugin'
export default function Index({
name,
error,
description,
onChange,
value,
intlLabel,
attribute,
}) {
const { formatMessage } = useIntl();
const [prompt, setPrompt] = useState('');
const [err, setErr] = useState('');
const generateText = async () => {
try {
const response = await fetch(`/ai-text-generation/generate-text`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${auth.getToken()}`
},
body: JSON.stringify({
'model': 'text-davinci-001',
'prompt': `${prompt}`,
'temperature': 0.4,
'max_tokens': 64,
'top_p': 1,
'frequency_penalty': 0,
'presence_penalty': 0
})
});
if (!response.ok) {
throw new Error(`Error! status: ${response.status}`);
}
const result = await response.json();
const parsedResult = result.choices[0].text.replace(/(?:\r\n|\r|\n)/g, '');
onChange({ target: { name, value: parsedResult, type: attribute.type } })
} catch (err) {
setErr(err.message);
}
}
const clearGeneratedText = async () => {
onChange({ target: { name, value: '', type: attribute.type } })
}
return (
<Stack spacing={1}>
<TextInput
placeholder="Please write a prompt for content to generate"
label="Prompt"
name="Prompt"
onChange={(e) => setPrompt(e.target.value)}
value={prompt}
/>
<Stack padding={4} spacing={2}>
<Textarea
placeholder="Generated text"
label="Content"
name="content"
onChange={(e) =>
onChange({
target: { name, value: e.target.value, type: attribute.type },
})
}
>
{value}
</Textarea>
<Stack horizontal spacing={4}>
<Button onClick={() => generateText()}>Generate</Button>
<Button onClick={() => clearGeneratedText()}>Clear</Button>
</Stack>
</Stack>
</Stack>
)
}