Skip to content

Commit 8c40204

Browse files
react-ui: Add support to display Chinese (#713)
* react-ui: Add support to display Chinese llm-tgi microservice from GenAIComps has encoded each text, so Chinese response will be shown as hexadecimal in react UI. Add support to decode and display the response in Chinese. Also return raw response if no pattern found. Signed-off-by: Cathy Zhang <cathy.zhang@intel.com> Signed-off-by: Ruoyu Ying <ruoyu.ying@intel.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Cathy Zhang <cathy.zhang@intel.com> Signed-off-by: Ruoyu Ying <ruoyu.ying@intel.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent afc3341 commit 8c40204

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ChatQnA/docker/ui/react/src/redux/Conversation/ConversationSlice.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,21 @@ export const doConversation = (conversationRequest: ConversationRequest) => {
162162
const match = msg.data.match(/b'([^']*)'/);
163163
if (match && match[1] != "</s>") {
164164
const extractedText = match[1];
165-
result += extractedText;
165+
166+
// Check for the presence of \x hexadecimal
167+
if (extractedText.includes("\\x")) {
168+
// Decode Chinese (or other non-ASCII characters)
169+
const decodedText = decodeEscapedBytes(extractedText);
170+
result += decodedText;
171+
} else {
172+
result += extractedText;
173+
}
174+
} else if (!match) {
175+
// Return data without pattern
176+
result += msg?.data;
177+
}
178+
// Store back result if it is not null
179+
if (result) {
166180
store.dispatch(setOnGoingResult(result));
167181
}
168182
} catch (e) {
@@ -195,3 +209,13 @@ export const doConversation = (conversationRequest: ConversationRequest) => {
195209
console.log(err);
196210
}
197211
};
212+
213+
// decode \x hexadecimal encoding
214+
function decodeEscapedBytes(str: string): string {
215+
// Convert the byte portion separated by \x into a byte array and decode it into a UTF-8 string
216+
const byteArray: number[] = str
217+
.split("\\x")
218+
.slice(1)
219+
.map((byte: string) => parseInt(byte, 16));
220+
return new TextDecoder("utf-8").decode(new Uint8Array(byteArray));
221+
}

0 commit comments

Comments
 (0)