-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry-llm.js
62 lines (57 loc) · 2.33 KB
/
try-llm.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
document.addEventListener("DOMContentLoaded", function() {
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");
const sendButton = document.getElementById("send-button");
const apiKeyInput = document.getElementById("api-key-input");
const setApiKeyButton = document.getElementById("set-api-key-button");
const chatContainer = document.getElementById("chat-container");
let apiKey = '';
setApiKeyButton.addEventListener("click", function() {
apiKey = apiKeyInput.value.trim();
if (apiKey) {
apiKeyInput.disabled = true;
setApiKeyButton.disabled = true;
chatContainer.style.display = "block";
}
});
sendButton.addEventListener("click", function() {
const userMessage = userInput.value;
if (userMessage.trim() === "") return;
appendMessage("ユーザー", userMessage);
userInput.value = "";
// LLMへのリクエストを送信
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "gpt-4o-mini", // 使用するモデル名を確認してください
messages: [{ role: "user", content: userMessage }],
max_tokens: 500
})
})
.then(response => {
if (!response.ok) {
return response.json().then(errorInfo => Promise.reject(errorInfo));
}
return response.json();
})
.then(data => {
const llmMessage = data.choices[0].message.content.trim();
appendMessage("LLM", llmMessage);
})
.catch(error => {
console.error("Error:", error);
appendMessage("LLM", `エラーが発生しました: ${error.error.message}`);
});
});
function appendMessage(sender, message) {
const messageElement = document.createElement("div");
messageElement.className = "message";
messageElement.innerHTML = `<strong>${sender}:</strong> ${message}`;
chatBox.appendChild(messageElement);
chatBox.scrollTop = chatBox.scrollHeight;
}
});