-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
164 lines (143 loc) · 4.8 KB
/
chat.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chatbot Interface</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f0f2f5;
}
#chat-container {
width: 500px;
margin: 30px auto;
background-color: white;
border: 1px solid #ccc;
border-radius: 8px;
overflow-y: scroll;
max-height: 500px;
padding: 1em;
}
#input-container {
width: 600px;
margin: 30px auto;
display: flex;
justify-content: space-between;
}
#text-message {
width: 300px;
padding: 5px;
}
#output-field {
width: 100%;
min-height: 50px;
padding: 5px;
border: 1px solid darkgray;
border-radius: 5px;
word-wrap: break-word;
}
button {
padding: 5px 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f0f2f5;
cursor: pointer;
font-weight: bold;
margin-left: 5px;
}
.chat {
margin-bottom: 1em;
}
</style>
<script>
chats = [
'role': 'system',
'content': 'Never repeat what the user says unless they explicitly ask. Never talk in third person. You are talking to the user. The user\'s name is " + user,
];
const sendMessage = async () => {
const user = document.getElementById('user-name').value;
const message = document.getElementById('text-message').value;
if (message) {
document.getElementById('output-field').innerHTML += "<br>" + user + ": " + message;
}
document.getElementById('text-message').value = "";
chats.push({
'role': 'user',
'content': message,
});
const url = 'https://brain-buddy.com/wp-json/ai-chatbot/v1/chat';
const headers = {
'Accept': '*/*',
'Content-Type': 'application/json',
'Origin': 'https://brain-buddy.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
};
const data = {
"env": "chatbot",
"session": "N/A",
"prompt": "3-4",
"context": "",
"messages": chats,
"newMessage": message,
"userName": "User",
"aiName": "GPT4",
"model": "gpt-4",
"temperature": 0.8,
"maxTokens": 30000,
"maxResults": 1,
"apiKey": "",
"service": "openai",
"embeddingsIndex": "",
"stop": "",
"clientId": ""
};
const response = await fetch(url, {
headers: headers,
method: "POST",
body: JSON.stringify(data),
});
const responseContent = await response.json();
let responseText = "";
if ("answer" in responseContent) {
responseText = responseContent["answer"];
} else {
responseText = "Error: No 'answer' key found in API response.";
console.log(responseContent);
}
chats.push({
'role': 'assistant',
'content': responseText,
});
document.getElementById('output-field').innerHTML += "<br>GPT4: " + responseText;
};
const clearHistory = () => {
document.getElementById('output-field').innerHTML = "";
chats = [
'role': 'system',
'content': 'Never repeat what the user says unless they explicitly ask. The user\' name is " + user,
];
};
</script>
</head>
<body>
<div id="chat-container">
<div id="output-field"></div>
</div>
<div id="input-container">
<label>
Name:
<input id="user-name" type="text" placeholder="Name">
</label>
<input id="text-message" type="text" placeholder="Enter message">
<button onclick="sendMessage()">Send</button>
<button onclick="clearHistory()">Reset</button>
</div>
</body>
</html>