-
Notifications
You must be signed in to change notification settings - Fork 5
/
script.js
107 lines (92 loc) · 3.62 KB
/
script.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const apiKeyInput = document.getElementById("api-key-input");
// Get API key from cookie
const getApiKey = () => {
// Retrieve the API key from localStorage
const apiKey = localStorage.getItem('api_key');
return apiKey ? apiKey : null;
};
// Set API key cookie
const setApiKey = (apiKey) => {
// Store the API key in localStorage
localStorage.setItem('api_key', apiKey);
};
// Load saved API key
apiKeyInput.value = getApiKey();
// Save API key on input change
apiKeyInput.addEventListener("input", () => {
setApiKey(apiKeyInput.value);
});
addEventListeners();
let messages = [];
function sendMessage() {
const input = document.querySelector(".chat-input input");
const message = input.value;
if (!message) return;
const stopInput = document.getElementById("stop-input").value;
const stop = stopInput && stopInput.split(",");
// Construct the request body
const requestBody = {
model: document.getElementById("model-input").value,
messages: messages,
temperature: parseFloat(document.getElementById("temperature-input").value),
top_p: parseFloat(document.getElementById("top-p-input").value),
n: parseInt(document.getElementById("n-input").value),
stream: document.getElementById("stream-input").checked,
stop: stop,
max_tokens: parseInt(document.getElementById("max-tokens-input").value),
presence_penalty: parseFloat(document.getElementById("presence-penalty-input").value),
frequency_penalty: parseFloat(document.getElementById("frequency-penalty-input").value),
logit_bias: JSON.parse(document.getElementById("logit-bias-input").value || "{}"),
};
addMessage({ role: "user", content: message });
input.value = "";
fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${document.getElementById("api-key-input").value}`,
"Content-Type": "application/json"
},
body: JSON.stringify(requestBody)
})
.then(response => response.json())
.then(data => {
var message = data.choices[0].message;
addMessage(message);
})
.catch(error => {
console.error("Error:", error);
addMessage("Sorry, there was an error processing your message.", "received");
});
}
function addMessage(message) {
messages.push(message);
console.log(messages);
// var response = JSON.parse(xhr.responseText);
var chatLog = document.getElementById('chat-box');
var chatMessage = document.createElement('div');
chatMessage.classList.add('message');
chatMessage.classList.add(message.role);
chatMessage.innerText = message.content;
chatLog.appendChild(chatMessage);
// Get the chat box element
var chatBox = document.getElementById('chat-box');
// Scroll to the bottom of the chat box
chatBox.scrollTop = chatBox.scrollHeight;
return;
// const container = document.querySelector(".chat-messages");
// const message = arguments[0];
// const type = message.startsWith("received") ? "received" : "sent";
// const messageElement = document.createElement("div");
// messageElement.classList.add("message", type);
// messageElement.innerHTML = `
}
function addEventListeners() {
const sendBtn = document.querySelector(".chat-input button");
sendBtn.addEventListener("click", sendMessage);
const inputBox = document.querySelector(".chat-input input");
inputBox.addEventListener("keyup", event => {
if (event.key === "Enter") {
sendMessage();
}
});
}