-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.html
109 lines (99 loc) · 3.61 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat with Custom Assistant</title>
<style>
#chat {
max-width: 600px;
margin: auto;
padding: 10px;
border: 1px solid #ccc;
border-radius: 8px;
display: flex;
flex-direction: column;
height: 80vh;
}
#messages {
flex: 1;
overflow-y: auto;
margin-bottom: 10px;
border-bottom: 1px solid #ccc;
padding: 10px;
}
#userInput {
width: calc(100% - 60px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
width: 50px;
padding: 10px;
border: none;
background-color: #007bff;
color: white;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Chat with Custom Assistant</h1>
<div id="chat">
<div id="messages"></div>
<div>
<input type="text" id="userInput" placeholder="Type your message...">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
let threadId = null;
// Initialize the chat by creating a new thread
async function initializeChat() {
try {
const response = await fetch('https://sample-alpha-pearl.vercel.app/start');
if (!response.ok) throw new Error('Failed to start chat');
const data = await response.json();
threadId = data.thread_id;
console.log('Thread initialized:', threadId);
} catch (error) {
console.error('Error initializing chat:', error);
}
}
// Send a message to the assistant
async function sendMessage() {
const userInput = document.getElementById('userInput').value;
if (!userInput.trim()) return;
document.getElementById('userInput').value = '';
const messageElement = document.createElement('div');
messageElement.textContent = 'You: ' + userInput;
document.getElementById('messages').appendChild(messageElement);
try {
const response = await fetch('https://sample-alpha-pearl.vercel.app/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
thread_id: threadId,
message: userInput,
}),
});
if (!response.ok) throw new Error('Failed to send message');
const data = await response.json();
const assistantMessage = data.response;
const assistantMessageElement = document.createElement('div');
assistantMessageElement.textContent = 'Assistant: ' + assistantMessage;
document.getElementById('messages').appendChild(assistantMessageElement);
document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight; // Scroll to the bottom
} catch (error) {
console.error('Error sending message:', error);
}
}
// Initialize the chat when the page loads
window.onload = initializeChat;
</script>
</body>
</html>