Skip to content

Commit f0269e0

Browse files
982586: added the streaming response
1 parent 17a6b80 commit f0269e0

File tree

2 files changed

+43
-22
lines changed
  • ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/llm-model

2 files changed

+43
-22
lines changed

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/llm-model/razor

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<i>To get started, provide input or choose a suggestion.</i>
2020
</div>
2121
</script>
22-
22+
<script src="https://cdn.jsdelivr.net/npm/marked@latest/marked.min.js"></script>
2323
<script>
2424

2525
var assistObj = null;
@@ -50,10 +50,23 @@
5050
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
5151
}
5252

53-
responseText = await response.json();
54-
assistObj.addPromptResponse(marked.parse(responseText));
53+
const data = await response.json();
54+
responseText = data; // Adjust to data.response if the JSON is { response: "text" }
55+
let current = '';
56+
let i = 0;
57+
const typingSpeed = 15; // ms per character; adjust as needed
58+
const interval = setInterval(() => {
59+
if (i < responseText.length) {
60+
current += responseText.charAt(i);
61+
assistObj.addPromptResponse(marked.parse(current), false);
62+
i++;
63+
} else {
64+
assistObj.addPromptResponse(marked.parse(current), true);
65+
clearInterval(interval);
66+
}
67+
}, typingSpeed);
5568
} catch (error) {
56-
assistObj.addPromptResponse('⚠️ Something went wrong while connecting to the AI service. Please try again later.');
69+
assistObj.addPromptResponse('⚠️ Something went wrong while connecting to the AI service. Please try again later.', true);
5770
}
5871
}, 2000); // Match the 2000ms delay from the reference sample
5972
}

ej2-asp-core-mvc/code-snippet/ai-assistview/ai-integrations/llm-model/tagHelper

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
@model AssistViewDemo.Controllers.IndexViewModel
22
@{
3-
ViewData["Title"] = "AI Assistance with Ollama";
3+
ViewData["Title"] = "AI Assistance";
44
}
55

6-
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/28.1.41/material.css" />
7-
<script src="https://cdn.syncfusion.com/ej2/28.1.41/dist/ej2.min.js"></script>
8-
96
<div class="aiassist-container" style="height: 350px; width: 650px;">
10-
<ejs-aiassistview id="aiAssistView" bannerTemplate="#bannerContent" promptSuggestions="@Model.PromptSuggestionData" promptRequest="onPromptRequest" created="onCreated">
7+
<ejs-aiassistview id="aiAssistView" bannerTemplate="#bannerContent" promptSuggestions="@Model.PromptSuggestionData" promptRequest="onPromptRequest" created="onCreated" stopRespondingClick="handleStopResponse">
118
<e-aiassistview-toolbarsettings items="@Model.Items" itemClicked="toolbarItemClicked"></e-aiassistview-toolbarsettings>
129
</ejs-aiassistview>
1310
</div>
14-
1511
<script id="bannerContent" type="text/x-jsrender">
1612
<div class="banner-content">
1713
<div class="e-icons e-assistview-icon"></div>
@@ -28,14 +24,16 @@
2824
function onCreated() {
2925
assistObj = this;
3026
}
31-
27+
var stopStreaming = false;
28+
function handleStopResponse() {
29+
stopStreaming = true;
30+
}
3231
function toolbarItemClicked(args) {
33-
if (args.item.iconCss === 'e-icons e-refresh') {
34-
this.prompts = [];
35-
this.promptSuggestions = suggestions;
36-
}
32+
if (args.item.iconCss === 'e-icons e-refresh') {
33+
this.prompts = [];
34+
this.promptSuggestions = suggestions;
3735
}
38-
36+
}
3937
function onPromptRequest(args) {
4038
setTimeout(async () => {
4139
let responseText = '';
@@ -45,20 +43,30 @@
4543
headers: { 'Content-Type': 'application/json' },
4644
body: JSON.stringify({ prompt: args.prompt })
4745
});
48-
4946
if (!response.ok) {
5047
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
5148
}
52-
53-
responseText = await response.json();
54-
assistObj.addPromptResponse(marked.parse(responseText));
49+
const data = await response.json();
50+
responseText = data; // Adjust to data.response if the JSON is { response: "text" }
51+
let current = '';
52+
let i = 0;
53+
const typingSpeed = 15; // ms per character; adjust as needed
54+
const interval = setInterval(() => {
55+
if (i < responseText.length && !stopStreaming) {
56+
current += responseText.charAt(i);
57+
assistObj.addPromptResponse(marked.parse(current), false);
58+
i++;
59+
} else {
60+
assistObj.addPromptResponse(marked.parse(current), true);
61+
clearInterval(interval);
62+
}
63+
}, typingSpeed);
5564
} catch (error) {
56-
assistObj.addPromptResponse('⚠️ Something went wrong while connecting to the AI service. Please try again later.');
65+
assistObj.addPromptResponse('⚠️ Something went wrong while connecting to the AI service. Please try again later.', true);
5766
}
5867
}, 2000); // Match the 2000ms delay from the reference sample
5968
}
6069
</script>
61-
6270
<style>
6371
.aiassist-container .e-view-container {
6472
margin: auto;

0 commit comments

Comments
 (0)