120 lines
3.4 KiB
JavaScript
120 lines
3.4 KiB
JavaScript
console.log(">>> basic_messenger >>>");
|
|
console.log({ query: req.query, body: req.body });
|
|
console.log(">>> basic_messenger >>>");
|
|
let sessionId = req.body.previous_response_id || `resp_${uuidv4()}`;
|
|
const input = req.body.input;
|
|
const modelName =
|
|
req.body.model || settings_667ef98d3ee8930a5debcbdb.nlu.modelName;
|
|
const metadata = {
|
|
...req.body.metadata,
|
|
userId: req.body.sentBy?.userId,
|
|
};
|
|
const postBack = req.body.postBack;
|
|
const configuration = req.body.event?.configuration;
|
|
|
|
(async () => {
|
|
try {
|
|
const smStr = `session-map-${sessionId}`;
|
|
const sessionMap = await redis.hGetAll(smStr);
|
|
console.log({ smStr: smStr, sessionMap: sessionMap });
|
|
|
|
const recognizedData = await axios
|
|
.post(
|
|
`${settings_667ef98d3ee8930a5debcbdb.nlu.apiBaseURL}Model/run/${req.params.workspaceId}/${req.params.branch}/${modelName}`,
|
|
{
|
|
input: input,
|
|
conversationId: sessionMap.conversationId,
|
|
settings: settings_667ef98d3ee8930a5debcbdb.nlu.settings,
|
|
metadata: metadata,
|
|
postBack: postBack,
|
|
configuration: configuration,
|
|
}
|
|
)
|
|
.then((response) => {
|
|
return response.data;
|
|
})
|
|
.catch((error) => {
|
|
let errorPayload = {
|
|
id: `resp_${uuidv4()}`,
|
|
object: "response",
|
|
status: "failed",
|
|
model: modelName,
|
|
created_at: Math.floor(Date.now() / 1000),
|
|
error: {
|
|
name: error.name,
|
|
message: error.message,
|
|
},
|
|
};
|
|
return errorPayload;
|
|
});
|
|
|
|
console.log({
|
|
rdCi: recognizedData.conversationId,
|
|
sessionMap: sessionMap,
|
|
});
|
|
|
|
if (recognizedData.conversationId) {
|
|
await redis.hSet(smStr, "conversationId", recognizedData.conversationId);
|
|
await redis.expire(smStr, 3600);
|
|
if (sessionMap.conversationId) {
|
|
} else {
|
|
db.analytics.addConversation({
|
|
id: recognizedData.conversationId,
|
|
});
|
|
}
|
|
}
|
|
|
|
// NOTE: Additonal logic could be used to provide answer from alternative sources
|
|
// recognizedData.answers = API_RESPONSE || ['Example'];
|
|
if (recognizedData.tag === "ERROR") {
|
|
} else if (
|
|
recognizedData.classificationResults.length === 0 &&
|
|
recognizedData.answers.length === 0
|
|
) {
|
|
recognizedData.answers = [
|
|
settings_667ef98d3ee8930a5debcbdb.responses.unrecognized,
|
|
];
|
|
} else if (recognizedData.answers.length === 0) {
|
|
recognizedData.answers = [
|
|
settings_667ef98d3ee8930a5debcbdb.responses.unanswered,
|
|
];
|
|
}
|
|
recognizedData._id = (
|
|
await db.analytics.addTransaction(recognizedData)
|
|
).insertedId;
|
|
|
|
// Concatenate the array of strings into a single string
|
|
const text = recognizedData.answers.join(" ");
|
|
|
|
const response = {
|
|
id: sessionId,
|
|
object: "response",
|
|
created_at: Math.floor(Date.now() / 1000),
|
|
status: "completed",
|
|
model: modelName,
|
|
output: [
|
|
{
|
|
type: "message",
|
|
id: `msg_${uuidv4()}`,
|
|
role: "assistant",
|
|
content: [
|
|
{
|
|
type: "output_text",
|
|
text: text,
|
|
annotations: [],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
res.send(response);
|
|
} catch (error) {
|
|
console.log(error.message);
|
|
res.send({
|
|
answers: ["Something went wrong. Please try again."],
|
|
outputs: {},
|
|
});
|
|
}
|
|
})();
|