Skip to content

Commit

Permalink
Modify the corresponding format according to the backend new structur…
Browse files Browse the repository at this point in the history
…e. (opea-project#326)

* Modify the corresponding format according to the backend new structure.

Signed-off-by: Yue, Wenjiao <wenjiao.yue@intel.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update +page.svelte

* Update +page.svelte

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* update validate string

Signed-off-by: Yue, Wenjiao <wenjiao.yue@intel.com>

* Update test_searchqna_on_gaudi.sh

---------

Signed-off-by: Yue, Wenjiao <wenjiao.yue@intel.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sihan Chen <39623753+Spycsh@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 27, 2024
1 parent 6f5a993 commit 3ffe19e
Show file tree
Hide file tree
Showing 46 changed files with 57 additions and 39 deletions.
1 change: 0 additions & 1 deletion SearchQnA/deprecated/ui/svelte/.env

This file was deleted.

File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions SearchQnA/docker/ui/svelte/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BACKEND_BASE_URL = 'http://x.x.x.x:yyyy/v1/searchqna'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ export async function fetchTextStream(query: string, knowledge_base_id: string,
let url = "";

payload = {
query: query,
knowledge_base_id: knowledge_base_id,
messages: query,
stream: "True",
};
url = `${BACKEND_BASE_URL}/web_search_chat_stream`;
url = `${BACKEND_BASE_URL}`;

return new SSE(url, {
headers: { "Content-Type": "application/json" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

<script lang="ts">
export let data;
import { ifStoreMsg, isCheckedStore, knowledge1 } from "$lib/shared/stores/common/Store";
import {
ifStoreMsg,
isCheckedStore,
knowledge1,
} from "$lib/shared/stores/common/Store";
import { onMount } from "svelte";
import Header from "$lib/shared/components/header/header.svelte";
import {
Expand Down Expand Up @@ -56,73 +60,85 @@
});
function handleTop() {
console.log("top");
scrollToTop(scrollToDiv);
}
function storeMessages() {
console.log('localStorage', chatMessages);
localStorage.setItem(
LOCAL_STORAGE_KEY.STORAGE_CHAT_KEY,
JSON.stringify(chatMessages)
);
}
function decodeEscapedBytes(str: string): string {
const byteArray = str
.split("\\x")
.slice(1)
.map((byte) => parseInt(byte, 16));
return new TextDecoder("utf-8").decode(new Uint8Array(byteArray));
}
function decodeUnicode(str: string): string {
return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => {
return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16));
});
}
const callTextStream = async (query: string) => {
const eventSource = await fetchTextStream(query, knowledge_1, $isCheckedStore);
const eventSource = await fetchTextStream(
query,
knowledge_1,
$isCheckedStore
);
eventSource.addEventListener("message", (e: any) => {
let currentMsg = e.data;
currentMsg = currentMsg.replaceAll("@#$", " ")
console.log("currentMsg", currentMsg);
if (currentMsg == "[DONE]") {
console.log("done getCurrentTimeStamp", getCurrentTimeStamp);
let startTime = chatMessages[chatMessages.length - 1].time;
let Msg = e.data;
if (Msg.startsWith("b")) {
let trimmedData = Msg.slice(2, -1);
loading = false;
let totalTime = parseFloat(((getCurrentTimeStamp() - startTime) / 1000).toFixed(2));
console.log("done totalTime", totalTime);
console.log(
"chatMessages[chatMessages.length - 1]",
chatMessages[chatMessages.length - 1]
);
if (chatMessages.length - 1 !== -1) {
chatMessages[chatMessages.length - 1].time = totalTime;
if (/\\x[\dA-Fa-f]{2}/.test(trimmedData)) {
trimmedData = decodeEscapedBytes(trimmedData);
} else if (/\\u[\dA-Fa-f]{4}/.test(trimmedData)) {
trimmedData = decodeUnicode(trimmedData);
}
console.log("done chatMessages", chatMessages);
storeMessages();
} else {
if (trimmedData !== "</s>") {
trimmedData = trimmedData.replace(/\\n/g, "\n");
}
if (chatMessages[chatMessages.length - 1].role == MessageRole.User) {
console.log("?", getCurrentTimeStamp());
chatMessages = [
...chatMessages,
{
role: MessageRole.Assistant,
type: MessageType.Text,
content: currentMsg,
content: trimmedData,
time: getCurrentTimeStamp(),
},
];
console.log("? chatMessages", chatMessages);
} else {
let content = chatMessages[chatMessages.length - 1].content as string;
chatMessages[chatMessages.length - 1].content =
content + currentMsg;
chatMessages[chatMessages.length - 1].content = content + trimmedData;
}
scrollToBottom(scrollToDiv);
} else if (Msg === "[DONE]") {
let startTime = chatMessages[chatMessages.length - 1].time;
loading = false;
let totalTime = parseFloat(
((getCurrentTimeStamp() - startTime) / 1000).toFixed(2)
);
if (chatMessages.length - 1 !== -1) {
chatMessages[chatMessages.length - 1].time = totalTime;
}
storeMessages();
}
});
eventSource.stream();
};
const handleTextSubmit = async () => {
console.log("handleTextSubmit");
loading = true;
const newMessage = {
role: MessageRole.User,
Expand Down Expand Up @@ -154,6 +170,8 @@
}
return true;
}
</script>

<Header />
Expand All @@ -165,7 +183,7 @@
class="fixed relative flex w-full flex-col items-center justify-between bg-white p-2 pb-0"
>
<div class="relative my-4 flex w-full flex-row justify-center">
<div class="focus:border-none relative w-full">
<div class="relative w-full focus:border-none">
<input
class="text-md block w-full border-0 border-b-2 border-gray-300 px-1 py-4
text-gray-900 focus:border-gray-300 focus:ring-0 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions SearchQnA/tests/test_searchqna_on_gaudi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function start_services() {
# Start Docker Containers
docker compose -f docker_compose.yaml up -d
n=0
until [[ "$n" -ge 200 ]]; do
until [[ "$n" -ge 300 ]]; do
docker logs tgi-gaudi-server > tgi_service_start.log
if grep -q Connected tgi_service_start.log; then
break
Expand All @@ -84,7 +84,7 @@ function validate_megaservice() {
result=$(http_proxy="" curl http://${ip_address}:3008/v1/searchqna -XPOST -d '{"messages": "What is the latest news? Give me also the source link", "stream": "False"}' -H 'Content-Type: application/json')
echo $result

if [[ $result == *"www"* ]]; then
if [[ $result == *"news"* ]]; then
echo "Result correct."
else
docker logs web-retriever-chroma-server
Expand Down
2 changes: 1 addition & 1 deletion SearchQnA/tests/test_searchqna_on_xeon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function validate_megaservice() {
result=$(http_proxy="" curl http://${ip_address}:3008/v1/searchqna -XPOST -d '{"messages": "What is the latest news? Give me also the source link", "stream": "False"}' -H 'Content-Type: application/json')
echo $result

if [[ $result == *"www"* ]]; then
if [[ $result == *"news"* ]]; then
echo "Result correct."
else
docker logs web-retriever-chroma-server
Expand Down

0 comments on commit 3ffe19e

Please sign in to comment.