@cognigy/rest-api-client
Version:
Cognigy REST-Client
81 lines (80 loc) • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSearchPrompt = void 0;
const transcripts_1 = require("../../../../../interfaces/transcripts/transcripts");
const createSystemMessage_1 = require("./createSystemMessage");
/**
* Generates a search prompt based on the user's input and the context of the conversation.
*
* @param {IGenerateSearchPromptOptions} promptOptions - Options for generating the search prompt.
* @returns The generated search prompt.
*/
const generateSearchPrompt = async (promptOptions) => {
var _a, _b, _c, _d;
const { api, llmProviderReferenceId, debugLogTokenCount, userMemory, memoryContextInjection, } = promptOptions;
let parsedPrompt = { generated_prompt: "" };
try {
const transcript = await api.getTranscript({
limit: 7,
rolesWhiteList: [transcripts_1.TranscriptRole.USER, transcripts_1.TranscriptRole.ASSISTANT, transcripts_1.TranscriptRole.TOOL],
excludeDataOnlyMessagesFilter: [transcripts_1.TranscriptRole.ASSISTANT]
});
const profileInstruction = (0, createSystemMessage_1.createContactProfileInstruction)(userMemory);
let memoryText = "";
if (profileInstruction) {
memoryText = profileInstruction;
}
if (memoryContextInjection) {
memoryText += `You also know the following:\n${typeof memoryContextInjection === "object" ? JSON.stringify(memoryContextInjection) : memoryContextInjection}\n`;
}
if (memoryText) {
memoryText += "\n";
}
const systemMessage = {
role: "system",
content: `${memoryText}Your task is to generate a search prompt based on the user's input. Consider the context of the conversation. Use exactly the following JSON response format:
{
"generated_prompt": "Generated question including the context of the conversation"
}`,
};
const llmPromptOptions = {
prompt: "",
chat: [systemMessage],
transcript,
detailedResults: true,
timeoutInMs: 8000,
temperature: 0,
maxTokens: 4000,
responseFormat: "json_object",
};
if (llmProviderReferenceId && llmProviderReferenceId !== "default") {
llmPromptOptions["llmProviderReferenceId"] = llmProviderReferenceId;
}
const llmResult = await ((_a = api.runGenerativeAIPrompt) === null || _a === void 0 ? void 0 : _a.call(api, llmPromptOptions, "aiAgent"));
// Send optional debug message with token usage
const tokenUsage = llmResult === null || llmResult === void 0 ? void 0 : llmResult.tokenUsage;
if (debugLogTokenCount && tokenUsage) {
(_b = api.logDebugMessage) === null || _b === void 0 ? void 0 : _b.call(api, tokenUsage, "UI__DEBUG_MODE__AI_AGENT_JOB__KNOWLEDGE_SEARCH__QUERY_GENERATION__TOKEN_USAGE__HEADER");
}
const prompt = llmResult === null || llmResult === void 0 ? void 0 : llmResult.result;
if (typeof prompt === "object" && prompt !== null) {
if (prompt === null || prompt === void 0 ? void 0 : prompt.generated_prompt) {
parsedPrompt.generated_prompt = prompt.generated_prompt;
}
}
else if (typeof prompt === "string") {
parsedPrompt = JSON.parse(prompt);
}
}
catch (error) {
const errorDetails = {
name: (error === null || error === void 0 ? void 0 : error.name) || "Error",
code: (error === null || error === void 0 ? void 0 : error.code) || (error === null || error === void 0 ? void 0 : error.httpStatusCode),
message: (error === null || error === void 0 ? void 0 : error.message) || ((_c = error.originalErrorDetails) === null || _c === void 0 ? void 0 : _c.message),
};
(_d = api.logDebugError) === null || _d === void 0 ? void 0 : _d.call(api, errorDetails.message + (errorDetails.code ? ` (error code: ${errorDetails.code})` : ""), `UI__DEBUG_MODE__AI_AGENT_JOB__KNOWLEDGE_SEARCH__QUERY_GENERATION__ERROR__HEADER ${errorDetails.name}`);
}
return parsedPrompt;
};
exports.generateSearchPrompt = generateSearchPrompt;
//# sourceMappingURL=generateSearchPrompt.js.map