-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (70 loc) · 2.54 KB
/
index.js
File metadata and controls
77 lines (70 loc) · 2.54 KB
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
var suggestions = [
'What are the best tools for organizing my tasks?',
'How can I maintain work-life balance effectively?',
];
var stopStreaming = false;
// Initialize AI AssistView
var aiAssist = new ej.interactivechat.AIAssistView({
bannerTemplate:
'<div class="banner-content"><div class="e-icons e-assistview-icon"></div><h3>AI Assistance</h3><div>To get started, provide input or choose a suggestion.</div></div>',
promptSuggestions: suggestions,
toolbarSettings: {
items: [{ iconCss: 'e-icons e-refresh', align: 'Right' }],
itemClicked: toolbarItemClicked,
},
promptRequest:onPromptrequest,
stopRespondingClick: handleStopResponse
});
// Handle user prompt: call local LLM via Ollama
async function onPromptrequest(args) {
var lastResponse = "";
var defaultResponse = "⚠️ Something went wrong while connecting to the AI service. Please check your Ollama application running background.";
try {
// Send request to Ollama API
var response = await fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'deepseek-r1',
prompt: `### Instruction:\nRespond in up to 5 lines.\n\n### Input:\n${args.prompt}`,
stream: false,
}),
});
var reply = await response.json();
var responseUpdateRate = 10;
// Stream AI response in chunks
async function streamResponse(response) {
var i = 0;
var responseLength = response.length;
while (i < responseLength && !stopStreaming) {
lastResponse += response[i];
i++;
if (i % responseUpdateRate === 0 || i === responseLength) {
var htmlResponse =marked.parse(lastResponse);
aiAssist.addPromptResponse(htmlResponse, i === responseLength);
aiAssist.scrollToBottom();
}
await new Promise(resolve => setTimeout(resolve, 15)); // Delay before the next chunk
}
aiAssist.promptSuggestions = suggestions;
}
if(response) {
stopStreaming = false;
streamResponse(reply.response);
}
} catch (error) {
aiAssist.addPromptResponse(defaultResponse, true);
aiAssist.promptSuggestions = suggestions;
}
}
function handleStopResponse() {
stopStreaming = true;
}
function toolbarItemClicked(args) {
if (args.item?.iconCss === 'e-icons e-refresh') {
aiAssist.prompts = [];
aiAssist.promptSuggestions = suggestions;
}
}
// Append the AI AssistView to the container
aiAssist.appendTo('#llmAssist');