Skip to content

Commit

Permalink
update faqGen ui response (#1091)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: lvliang-intel <liang1.lv@intel.com>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent c9088eb commit e2bdd19
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 65 deletions.
2 changes: 1 addition & 1 deletion FaqGen/tests/test_compose_on_gaudi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function main() {

validate_microservices
validate_megaservice
validate_frontend
# validate_frontend

stop_docker
echo y | docker system prune
Expand Down
2 changes: 1 addition & 1 deletion FaqGen/tests/test_compose_on_xeon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function main() {

validate_microservices
validate_megaservice
validate_frontend
# validate_frontend

stop_docker
echo y | docker system prune
Expand Down
2 changes: 1 addition & 1 deletion FaqGen/ui/svelte/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DOC_BASE_URL = 'http://backend_address:8888/v1/faqgen'
FAQ_BASE_URL = 'http://backend_address:8888/v1/faqgen'
14 changes: 7 additions & 7 deletions FaqGen/ui/svelte/src/lib/doc.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
} else {
currentIdx = index;
if (
(currentIdx === 1 && message !== "") ||
(currentIdx === 2 && $kb_id !== "")
(currentIdx === 2 && message !== "") ||
(currentIdx === 1 && $kb_id !== "")
) {
formModal = true;
} else {
Expand All @@ -49,10 +49,10 @@
}
function panelExchange() {
if (currentIdx === 2) {
if (currentIdx === 1) {
kb_id.set("");
dispatch("clearMsg", { status: true });
} else if (currentIdx === 1) {
} else if (currentIdx === 2) {
message = "";
dispatch("clearMsg", { status: true });
}
Expand Down Expand Up @@ -152,7 +152,7 @@
type="submit"
data-testid="sum-click"
class="xl:my-12 inline-flex items-center px-5 py-2.5 text-sm font-medium text-center text-white bg-blue-700 mt-2 focus:ring-4 focus:ring-blue-200 dark:focus:ring-blue-900 hover:bg-blue-800"
on:click={() => generateFaq()}
on:click={() => generateFaq()}
>
Generate FAQs
</button>
Expand All @@ -165,11 +165,11 @@
/>
{#if currentIdx === 1}
<h3 class="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400">
The current content will be cleared.
The currently uploaded file will be cleared.
</h3>
{:else if currentIdx === 2}
<h3 class="mb-5 text-lg font-normal text-gray-500 dark:text-gray-400">
The currently uploaded file will be cleared.
The current content will be cleared.
</h3>
{/if}

Expand Down
31 changes: 21 additions & 10 deletions FaqGen/ui/svelte/src/lib/dropFile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,36 @@
-->

<script lang="ts">
import { Dropzone } from "flowbite-svelte";
import ImgLogo from "./assets/imgLogo.svelte";
import { kb_id } from "./shared/Store.js";
import { fetchKnowledgeBaseId } from "./shared/Network.js";
import { kb_id, uploadFile, uploadFilesName } from "./shared/Store.js";
let uploadInput: HTMLInputElement;
let uploadFileName = '';
let uploadFileName = "";
let displayHint = false;
function handleInput(event: Event) {
const file = (event.target as HTMLInputElement).files![0];
if (!file) return;
// Check if the file size exceeds 2KB (2048 bytes)
if (file.size > 2048) {
displayHint = true;
setTimeout(() => {
displayHint = false;
}, 3000);
return; // Exit the function if the file is too large
}
const reader = new FileReader();
reader.onloadend = async () => {
if (!reader.result) return;
const src = reader.result.toString();
const blob = await fetch(src).then((r) => r.blob());
const fileName = file.name;
uploadFilesName.set(fileName);
kb_id.set(fileName);
uploadFile.set(blob);
uploadFileName = fileName;
const res = await fetchKnowledgeBaseId(blob, fileName);
kb_id.set(res.document_id);
console.log("upload File", $kb_id);
};
reader.readAsDataURL(file);
}
Expand All @@ -65,13 +71,18 @@
d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2"
/>
</svg>
{#if uploadFileName === ''}
{#if uploadFileName === ""}
<p class="mb-2 text-sm text-gray-500 dark:text-gray-400">
<span class="font-semibold">Click to upload</span> or drag and drop
</p>
<p class="text-xs text-gray-500 dark:text-gray-400">
PDF, TXT, .Doc and so on
</p>
{#if displayHint}
<p class="text-xs text-red-500 dark:text-red-400">
Maximum upload size is 2KB.
</p>
{/if}
{:else}
<p>{uploadFileName}</p>
{/if}
Expand Down
89 changes: 57 additions & 32 deletions FaqGen/ui/svelte/src/lib/shared/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,75 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { SSE } from "sse.js";
import { env } from "$env/dynamic/public";

const DOC_BASE_URL = env.DOC_BASE_URL;
const FAQ_BASE_URL = env.FAQ_BASE_URL;

async function fetchPostRes(url, init) {
try {
const response = await fetch(url, init);
if (!response.ok) throw response.status;
return await response.json();
} catch (error) {
console.error("network error: ", error);
return undefined;
export async function fetchTextStream(query: string | Blob, params: string, file: Blob, fileName: string | undefined) {
const url = `${FAQ_BASE_URL}`; // Ensure the URL is constructed correctly
const formData = new FormData();

if (!file) {
file = new Blob([""], { type: "text/plain" });
fileName = "empty.txt";
}
}

export async function fetchKnowledgeBaseId(file: Blob, fileName: string) {
const url = `${DOC_BASE_URL}/doc_upload`;
const formData = new FormData();
formData.append("file", file, fileName);
if (params === "doc_id") {
formData.append("files", file, fileName);
formData.append("messages", query);
} else if (params === "text") {
formData.append("files", file, fileName);
formData.append("messages", query);
}

const init: RequestInit = {
// Initiate the POST request to upload the file
const init = {
method: "POST",
body: formData,
};

return fetchPostRes(url, init);
}
const postResponse = await fetch(url, init);

export async function fetchTextStream(query: string, urlSuffix: string, params: string) {
let payload = {};
let url = "";
if (params === "doc_id") {
payload = { doc_id: query };
url = ``;
} else if (params === "text") {
payload = { messages: query };
url = `${DOC_BASE_URL}`;
if (!postResponse.ok) {
throw new Error(`Error uploading file: ${postResponse.status}`);
}

console.log("url", url);
// Function to create an async iterator for the stream
async function* streamGenerator() {
if (!postResponse.body) {
throw new Error("Response body is null");
}
const reader = postResponse.body.getReader();
const decoder = new TextDecoder("utf-8");
let done, value;

let buffer = ""; // Initialize a buffer

while (({ done, value } = await reader.read())) {
if (done) break;

// Decode chunk and append to buffer
const chunk = decoder.decode(value, { stream: true });
buffer += chunk;

// Use regex to clean and extract data
const cleanedChunks = buffer
.split("\n")
.map((line) => {
// Remove 'data: b' at the start and ' ' at the end
return line.replace(/^data:\s*|^b'|'\s*$/g, "").trim(); // Clean unnecessary characters
})
.filter((line) => line); // Remove empty lines

for (const cleanedChunk of cleanedChunks) {
// Further clean to ensure all unnecessary parts are removed
yield cleanedChunk.replace(/^b'|['"]$/g, ""); // Again clean 'b' and other single or double quotes
}

// If there is an incomplete message in the current buffer, keep it
buffer = buffer.endsWith("\n") ? "" : cleanedChunks.pop() || ""; // Keep the last incomplete part
}
}

return new SSE(url, {
headers: { "Content-Type": "application/json" },
payload: JSON.stringify(payload),
});
return streamGenerator(); // Return the async generator
}
4 changes: 4 additions & 0 deletions FaqGen/ui/svelte/src/lib/shared/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ import { writable } from "svelte/store";
export let kb_id = writable("");

export let loading = writable(false);

export const uploadFile = writable<Blob | null>(null);

export let uploadFilesName = writable("");
33 changes: 20 additions & 13 deletions FaqGen/ui/svelte/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import Doc from "$lib/doc.svelte";
import Faq from "$lib/faq.svelte";
import { fetchTextStream } from "$lib/shared/Network.js";
import { loading } from "$lib/shared/Store.js";
import { loading, uploadFilesName, uploadFile } from "$lib/shared/Store.js";
import { onMount } from "svelte";
import { scrollToBottom } from "$lib/shared/Utils.js";
Expand All @@ -31,28 +31,33 @@
console.log("scrollToDiv", scrollToDiv);
});
let code_output: string = "";
let query: string = "";
let deleteFlag: boolean = false;
const callTextStream = async (
query: string,
urlSuffix: string,
params: string,
params: string
) => {
messages = "";
const eventSource = await fetchTextStream(query, urlSuffix, params);
// Fetch the stream
const eventStream = await fetchTextStream(
query,
params,
$uploadFile,
$uploadFilesName
);
// Process the stream as an async iterator
try {
for await (const chunk of eventStream) {
let Msg = chunk;
console.log('Msg', Msg);
eventSource.addEventListener("message", (e: any) => {
let Msg = e.data;
if (Msg !== "[DONE]") {
let res = JSON.parse(Msg);
let logs = res.ops;
logs.forEach((log: { op: string; path: string; value: any }) => {
if (log.op === "add") {
if (
log.value !== "</s>" &&
log.value !== "<|eot_id|>" &&
log.path.endsWith("/streamed_output/-") &&
log.path.length > "/streamed_output/-".length
) {
Expand All @@ -65,8 +70,10 @@
loading.set(false);
scrollToBottom(scrollToDiv);
}
});
eventSource.stream();
}
} catch (error) {
console.error("Error processing the stream:", error);
}
};
async function handleGenerateFaq(e) {
Expand Down

0 comments on commit e2bdd19

Please sign in to comment.