File size: 1,985 Bytes
1710d5f
 
 
 
 
fc49625
1710d5f
52d5007
1710d5f
 
 
 
 
 
fc49625
1710d5f
 
 
 
52d5007
fc49625
 
1710d5f
52d5007
1710d5f
52d5007
 
1710d5f
 
52d5007
1710d5f
 
 
 
 
 
 
 
 
52d5007
1710d5f
 
 
52d5007
1710d5f
52d5007
 
fc49625
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
// script.js
document.addEventListener('DOMContentLoaded', function() {
    const sendMessageButton = document.getElementById('send-message');
    const messageInput = document.getElementById('message-input');
    const conversationHistory = document.getElementById('conversation-history');

    sendMessageButton.addEventListener('click', function() {
        const userMessage = messageInput.value;
        if (userMessage) {
            addMessageToChat('user', userMessage);
            sendMessageToApi(userMessage);
            messageInput.value = ''; // Clear the input field
        }
    });

    function addMessageToChat(role, message) {
        const messageElement = document.createElement('div');
        messageElement.classList.add('message', role);
        messageElement.textContent = message;
        conversationHistory.appendChild(messageElement);
    }

    async function sendMessageToApi(message) {
        try {
            const response = await fetch(`https://api.fireworks.ai/inference/v1/chat/completions`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': 'Bearer YOUR_API_KEY_HERE' // Replace with your real API key
                },
                body: JSON.stringify({
                    messages: [{ role: 'user', content: message }],
                    model: 'accounts/fireworks/models/mixtral-8x7b-instruct',
                    stream: true,
                    n: 1,
                    max_tokens: 1000,
                    temperature: 0.9,
                    stop: []
                })
            });
            const data = await response.json();
            const botMessage = data.choices[0].message.content; // Adjust this according to the API response structure
            addMessageToChat('bot', botMessage);
        } catch (error) {
            console.error('Error sending message to API:', error);
        }
    }
});