dump error message to frontend instead of generic error (#114)

This commit is contained in:
Timothy Carambat 2023-06-26 17:54:55 -07:00 committed by GitHub
parent 60a00843df
commit 82d132427c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 9 deletions

View File

@ -54,11 +54,6 @@ export default function ChatContainer({ workspace, knownHistory = [] }) {
window.localStorage.getItem(`workspace_chat_mode_${workspace.slug}`) ??
"chat"
);
if (!chatResult) {
alert("Could not send chat.");
setLoadingResponse(false);
return;
}
handleChat(
chatResult,
setLoadingResponse,

View File

@ -10,7 +10,6 @@ export default function handleChat(
if (type === "abort") {
setLoadingResponse(false);
alert(error);
setChatHistory([
...remHistory,
{

View File

@ -1,3 +1,4 @@
const { v4: uuidv4 } = require("uuid");
const { reqBody } = require("../utils/http");
const { Workspace } = require("../models/workspace");
const { chatWithWorkspace } = require("../utils/chats");
@ -18,8 +19,14 @@ function chatEndpoints(app) {
const result = await chatWithWorkspace(workspace, message, mode);
response.status(200).json({ ...result });
} catch (e) {
console.log(e.message, e);
response.sendStatus(500).end();
response.status(500).json({
id: uuidv4(),
type: "abort",
textResponse: null,
sources: [],
close: true,
error: e.message,
});
}
});
}

View File

@ -3,7 +3,6 @@ class OpenAi {
constructor() {
const config = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
// organization: "org-123xyz", // Optional
});
const openai = new OpenAIApi(config);
this.openai = openai;
@ -23,6 +22,11 @@ class OpenAi {
if (res.results.length === 0)
throw new Error("OpenAI moderation: No results length!");
return res.results[0];
})
.catch((error) => {
throw new Error(
`OpenAI::CreateModeration failed with: ${error.message}`
);
});
if (!flagged) return { safe: true, reasons: [] };
@ -65,6 +69,12 @@ class OpenAi {
if (res.choices.length === 0)
throw new Error("OpenAI chat: No results length!");
return res.choices[0].message.content;
})
.catch((error) => {
console.log(error);
throw new Error(
`OpenAI::createChatCompletion failed with: ${error.message}`
);
});
return textResponse;