-
Notifications
You must be signed in to change notification settings - Fork 14
/
chatgpt-chat-api.html
67 lines (63 loc) · 1.96 KB
/
chatgpt-chat-api.html
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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/1.3.4/axios.min.js"></script>
<title>Document</title>
</head>
<body>
<main>
<h2>ChatGPT の Chat completions API</h2>
<p>
<a href="https://platform.openai.com/docs/guides/chat"
>API ドキュメント</a
><br />
OpenAI で API キーを発行してソースコードに埋め込んでください
</p>
<input type="text" name="talk" />
<button type="button" class="send">送信</button>
<div class="output"></div>
</main>
<script>
// TODO:
const api_key = "<発行した API キーを入力してください";
const sendButton = document.querySelector(".send");
sendButton.addEventListener("click", async () => {
const text = document.querySelector("[name=talk]");
const responseText = await requestChatAPI(text.value);
const output = document.querySelector(".output");
output.textContent = responseText;
});
async function requestChatAPI(text) {
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${api_key}`,
};
const messages = [
{
role: "user",
content: text,
},
];
const payload = {
model: "gpt-3.5-turbo",
max_tokens: 128,
messages: messages,
};
const response = await axios.post(
"https://api.openai.com/v1/chat/completions",
payload,
{
headers: headers,
}
);
return response.data.choices[0].message.content;
}
</script>
</body>
</html>