Skip to content

Commit

Permalink
Parse RAI inner error message for display when using data (#810)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarah-widder authored Apr 25, 2024
1 parent 6e8a079 commit 632ba02
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 49 deletions.
2 changes: 1 addition & 1 deletion backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def format_pf_non_streaming_response(
"role": "tool",
"content": chatCompletion[citations_field_name]
})
response_obj = {
response_obj = {
"id": chatCompletion["id"],
"model": "",
"created": "",
Expand Down
30 changes: 29 additions & 1 deletion frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ const Chat = () => {
else if (typeof result.error === "string") {
errorMessage = result.error;
}

errorMessage = parseErrorMessage(errorMessage);

let errorChatMsg: ChatMessage = {
id: uuid(),
role: ERROR,
Expand Down Expand Up @@ -308,11 +311,12 @@ const Chat = () => {
setMessages(request.messages)
}
let result = {} as ChatResponse;
var errorResponseMessage = "Please try again. If the problem persists, please contact the site administrator.";
try {
const response = conversationId ? await historyGenerate(request, abortController.signal, conversationId) : await historyGenerate(request, abortController.signal);
if (!response?.ok) {
const responseJson = await response.json();
var errorResponseMessage = responseJson.error === undefined ? "Please try again. If the problem persists, please contact the site administrator." : responseJson.error;
errorResponseMessage = responseJson.error === undefined ? errorResponseMessage : parseErrorMessage(responseJson.error);
let errorChatMsg: ChatMessage = {
id: uuid(),
role: ERROR,
Expand Down Expand Up @@ -435,6 +439,9 @@ const Chat = () => {
else if (typeof result.error === "string") {
errorMessage = result.error;
}

errorMessage = parseErrorMessage(errorMessage);

let errorChatMsg: ChatMessage = {
id: uuid(),
role: ERROR,
Expand Down Expand Up @@ -517,6 +524,27 @@ const Chat = () => {
setClearingChat(false)
};

const parseErrorMessage = (errorMessage: string) => {
let errorCodeMessage = errorMessage.substring(0, errorMessage.indexOf("-") + 1);
const innerErrorCue = "{\\'error\\': {\\'message\\': ";
if (errorMessage.includes(innerErrorCue))
{
try {
let innerErrorString = errorMessage.substring(errorMessage.indexOf(innerErrorCue));
if (innerErrorString.endsWith("'}}")) {
innerErrorString = innerErrorString.substring(0, innerErrorString.length - 3);
}
innerErrorString = innerErrorString.replaceAll("\\'", "'")
let newErrorMessage = errorCodeMessage + " " + innerErrorString;
errorMessage = newErrorMessage;

} catch (e) {
console.error("Error parsing inner error message: ", e);
}
}
return errorMessage;
};

const newChat = () => {
setProcessMessages(messageStatus.Processing)
setMessages([])
Expand Down
Loading

0 comments on commit 632ba02

Please sign in to comment.