Skip to content

Commit 8b07647

Browse files
authored
Merge pull request #2 from CodeRifle/feat/llm-chat-integration
Integrate Language Model for chat functionality
2 parents fe20f36 + b669fe2 commit 8b07647

2 files changed

Lines changed: 92 additions & 28 deletions

File tree

index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ <h2>Get In Touch</h2>
116116
</div>
117117
</footer>
118118
</div>
119+
<!-- Assuming Google Gemini SDK. Replace with the correct SDK if different -->
120+
<script type="module">
121+
// This is a placeholder. The actual LanguageModel library (e.g., from a CDN)
122+
// would make `LanguageModel` available globally or as a module.
123+
// For demonstration, we'll mock a simple version if it's not already defined.
124+
if (typeof window.LanguageModel === 'undefined') {
125+
console.log("Mocking LanguageModel for development purposes.");
126+
window.LanguageModel = {
127+
create: async () => {
128+
console.log("Mock LanguageModel.create() called");
129+
return {
130+
prompt: async (message) => {
131+
console.log(`Mock session.prompt() called with: "${message}"`);
132+
await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate network delay
133+
if (message.toLowerCase().includes("error test")) {
134+
throw new Error("Simulated LLM error.");
135+
}
136+
return `Mock response to: "${message}"`;
137+
}
138+
};
139+
}
140+
};
141+
}
142+
</script>
119143
<script src="script.js"></script>
120144
</body>
121145
</html>

script.js

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
1-
const responses = {
2-
'who is rishabh gupta?': `Rishabh Gupta is an accomplished AI Engineer with expertise in developing and implementing cutting-edge artificial intelligence solutions. He has a strong background in machine learning, deep learning, and natural language processing, with a proven track record of delivering innovative AI-powered applications.`,
3-
4-
'what are his skills?': `Rishabh's technical skills include:
5-
• Deep Learning & Machine Learning (PyTorch, TensorFlow)
6-
• Natural Language Processing
7-
• Computer Vision
8-
• Large Language Models (LLMs)
9-
• Python, JavaScript, and various AI/ML frameworks
10-
• Cloud platforms (AWS, GCP)`,
11-
12-
'show me his projects': `Here are some of Rishabh's notable projects:
13-
1. Developed an advanced NLP system for automated customer support
14-
2. Created a computer vision solution for real-time object detection
15-
3. Implemented a recommendation engine using deep learning
16-
4. Built scalable ML pipelines for production environments`,
17-
18-
'default': `I'm not sure about that specific query, but I'd be happy to tell you about Rishabh's experience, skills, or projects. Feel free to ask about those topics!`
19-
};
1+
// const responses = {
2+
// 'who is rishabh gupta?': `Rishabh Gupta is an accomplished AI Engineer with expertise in developing and implementing cutting-edge artificial intelligence solutions. He has a strong background in machine learning, deep learning, and natural language processing, with a proven track record of delivering innovative AI-powered applications.`,
3+
//
4+
// 'what are his skills?': `Rishabh's technical skills include:
5+
// • Deep Learning & Machine Learning (PyTorch, TensorFlow)
6+
// • Natural Language Processing
7+
// • Computer Vision
8+
// • Large Language Models (LLMs)
9+
// • Python, JavaScript, and various AI/ML frameworks
10+
// • Cloud platforms (AWS, GCP)`,
11+
//
12+
// 'show me his projects': `Here are some of Rishabh's notable projects:
13+
// 1. Developed an advanced NLP system for automated customer support
14+
// 2. Created a computer vision solution for real-time object detection
15+
// 3. Implemented a recommendation engine using deep learning
16+
// 4. Built scalable ML pipelines for production environments`,
17+
//
18+
// 'default': `I'm not sure about that specific query, but I'd be happy to tell you about Rishabh's experience, skills, or projects. Feel free to ask about those topics!`
19+
// };
2020

2121
const chatMessages = document.querySelector('.chat-messages');
2222
const userInput = document.getElementById('userInput');
2323
const typingIndicator = document.querySelector('.typing-indicator');
2424

25+
// Variable to hold the LanguageModel session
26+
let modelSession = null;
27+
28+
// Initialize the LanguageModel session
29+
async function initializeSession() {
30+
try {
31+
// Assuming LanguageModel is available globally e.g. via a <script> tag
32+
if (window.LanguageModel && typeof window.LanguageModel.create === 'function') {
33+
modelSession = await window.LanguageModel.create();
34+
console.log("LanguageModel session initialized.");
35+
} else {
36+
console.error("LanguageModel library not found or 'create' method is missing.");
37+
addMessage("Error: Chat functionality is currently unavailable. LanguageModel library not found.", false);
38+
typingIndicator.style.display = 'none';
39+
}
40+
} catch (error) {
41+
console.error("Error initializing LanguageModel session:", error);
42+
addMessage("Error: Could not initialize the chat assistant. Please try again later.", false);
43+
typingIndicator.style.display = 'none';
44+
}
45+
}
46+
47+
// Call initializeSession when the script loads
48+
initializeSession();
49+
50+
2551
function addMessage(message, isUser = false) {
2652
const messageDiv = document.createElement('div');
2753
messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
@@ -37,25 +63,39 @@ function addMessage(message, isUser = false) {
3763

3864
function simulateTyping() {
3965
typingIndicator.style.display = 'block';
40-
return new Promise(resolve => setTimeout(resolve, Math.random() * 1000 + 1000));
66+
// We don't need a fixed timeout anymore as we wait for the actual LLM response
4167
}
4268

43-
async function getResponse(message) {
44-
const normalizedMessage = message.toLowerCase().trim();
45-
await simulateTyping();
46-
typingIndicator.style.display = 'none';
47-
return responses[normalizedMessage] || responses['default'];
48-
}
69+
// async function getResponse(message) {
70+
// const normalizedMessage = message.toLowerCase().trim();
71+
// await simulateTyping();
72+
// typingIndicator.style.display = 'none';
73+
// return responses[normalizedMessage] || responses['default'];
74+
// }
4975

5076
async function sendMessage() {
5177
const message = userInput.value.trim();
5278
if (!message) return;
5379

5480
addMessage(message, true);
5581
userInput.value = '';
82+
simulateTyping(); // Show typing indicator
5683

57-
const response = await getResponse(message);
58-
addMessage(response);
84+
if (!modelSession) {
85+
addMessage("Chat assistant is not available. Session not initialized.", false);
86+
typingIndicator.style.display = 'none';
87+
return;
88+
}
89+
90+
try {
91+
const result = await modelSession.prompt(message);
92+
addMessage(result);
93+
} catch (error) {
94+
console.error("Error getting response from LanguageModel:", error);
95+
addMessage("Sorry, I encountered an error trying to respond. Please try again.", false);
96+
} finally {
97+
typingIndicator.style.display = 'none'; // Hide typing indicator
98+
}
5999
}
60100

61101
function usePrompt(prompt) {

0 commit comments

Comments
 (0)