@cognigy/rest-api-client
Version:
Cognigy REST-Client
71 lines (69 loc) • 3.5 kB
JavaScript
import { __awaiter } from "tslib";
const getInitialPrompt = ({ recentConversation, value, }) => {
const prompt = `Below is the transcript of a conversation:
${recentConversation}
USER: ${value}
Does the last USER input refer to the conversation before?
Answer with "true" or "false". Answer:`;
return prompt;
};
const getFollowUpPrompt = ({ lastRoundTrip, value, }) => {
const prompt = `You are tasked to rewrite a question based on a context, so that the question is clearer.
Example:
Context:
USER: Where is Germany?
BOT: Germany is in Europe.
Question: Is that a continent?
New: Is Europe a continent?
Task:
Context:
${lastRoundTrip}
Question: ${value}
New: `;
return prompt;
};
export function detectFollowUp({ value, cognigy, followUpDetectionSteps, }) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const { api, input } = cognigy;
let prompt;
let lastRoundTrip;
// this is a fallback in case the node was created before this function was added and followUpDetectionSteps is undefined
followUpDetectionSteps = followUpDetectionSteps || 2;
let promptResponse;
// check whether we're in an flow execution that's not the first
// as it doesn't make sense to check for follow ups in the first execution
if (input.execution > 1) {
// always remember the last thing the user said (needed later)
lastRoundTrip = (_a = cognigy.lastConversationEntries) === null || _a === void 0 ? void 0 : _a.slice(1, 3).reverse().map((entry) => "- " + (entry.source === "user" ? "USER: " : "BOT: ") + entry.text).join("\n");
// if follow up detection is set to 2 or more, we use the conversation transcript
// as reference. Start at the second entry, because the first one is the current
const recentConversation = (_b = cognigy.lastConversationEntries) === null || _b === void 0 ? void 0 : _b.slice(1, followUpDetectionSteps + 1).reverse().map((entry) => "- " + (entry.source === "user" ? "USER: " : "BOT: ") + entry.text).join("\n");
prompt = getInitialPrompt({
recentConversation,
value,
});
try {
promptResponse = yield ((_c = api.runGenerativeAIPrompt) === null || _c === void 0 ? void 0 : _c.call(api, { prompt }, "answerExtraction"));
// check if LLM thinks the input was a follow up question and rewrite it
if (promptResponse === null || promptResponse === void 0 ? void 0 : promptResponse.toLowerCase().includes("true")) {
prompt = getFollowUpPrompt({
lastRoundTrip,
value,
});
promptResponse = yield ((_d = api.runGenerativeAIPrompt) === null || _d === void 0 ? void 0 : _d.call(api, { prompt }, "answerExtraction"));
}
else {
// if there is no question to rewrite, no response is provided to not override original input
promptResponse = undefined;
}
}
catch (err) {
(_e = api.log) === null || _e === void 0 ? void 0 : _e.call(api, "error", "Search Extract Output. Error when trying to rewrite follow up input: " +
err.message);
}
}
return promptResponse;
});
}
//# sourceMappingURL=followUpDetection.helper.js.map