Spaces:
Running
Running
File size: 2,389 Bytes
b04738d c9f43f8 b04738d 7d78ade fc8e3f3 b04738d 7d78ade b04738d 7d78ade fc8e3f3 c9f43f8 fc8e3f3 c9f43f8 7d78ade fc8e3f3 7d78ade c9f43f8 2824794 b04738d 7d78ade 2824794 fc8e3f3 b04738d 7d78ade b04738d fc8e3f3 7d78ade |
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 |
// Using DeepSeek Chat API (open source alternative)
const API_URL = 'https://api.deepseek.com/v1/chat/completions';
// Main chat function with improved error handling
async function callChatAPI(message, imageData = null) {
try {
if (imageData) {
return "I see you've uploaded an image. Currently I can process text messages. Please describe the image to me.";
}
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [{
role: 'user',
content: message
}],
temperature: 0.7,
max_tokens: 200
})
});
if (!response.ok) {
throw new Error('API request failed');
}
const data = await response.json();
return data.choices[0]?.message?.content || "I didn't get a response. Please try again.";
} catch (error) {
console.error('API Error:', error);
return "Sorry, I'm having trouble connecting to the AI service. Please try again later.";
}
}
// Utility function to convert file to base64
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
// Initialize chat with welcome message
document.addEventListener('DOMContentLoaded', () => {
const chatContainer = document.getElementById('chat-container');
if (chatContainer && chatContainer.children.length === 1) {
const welcomeMsg = `Hello! I'm your AI assistant. How can I help you today? I can:
- Answer questions
- Explain concepts
- Help with coding
- Provide suggestions
Try asking me anything!`;
const messageDiv = document.createElement('div');
messageDiv.className = 'flex justify-start mb-4';
messageDiv.innerHTML = `
<div class="bg-gray-200 dark:bg-gray-700 text-gray-800 dark:text-white rounded-lg rounded-bl-none p-3 max-w-xs">
${marked.parse(welcomeMsg)}
</div>
`;
chatContainer.appendChild(messageDiv);
}
});
|