@cognigy/rest-api-client
Version:
Cognigy REST-Client
68 lines • 3.09 kB
JavaScript
/** Custom Modules */
import { generativeAIPrompts } from "./utils/generativeAIPrompts";
import { InternalServerError } from "../errors/internalServerError";
export const isCompletionPrompt = (data) => {
return typeof data === "object" && data !== null && "prompt" in data;
};
export const isChatPrompt = (data) => {
return typeof data === "object" && data !== null && "messages" in data;
};
export const isOpenAIChatPrompt = (data) => {
return Array.isArray(data) &&
data.every((item) => typeof item === "object" &&
item !== null &&
("role" in item) &&
("content" in item) &&
(item.role === "system" || item.role === "user" || item.role === "assistant") &&
(typeof item.content === "string"));
};
/**
* Gets the current prompts for the passed model/useCase
* @param model - The model to get the prompt for
* @param useCase - The use case to get the prompt for
* @param subUseCase - Optional sub-use case to get a specific prompt
* @param promptParser - Optional function to modify the prompt before returning it
* @returns {TALLPrompts}
*/
export const getPrompt = (model, useCase, subUseCase, promptParser) => {
var _a;
const loggerMeta = {
module: "getPrompt.ts",
label: "generativeAI",
function: "getPrompt",
model,
useCase,
subUseCase
};
let modelPrompts = (_a = generativeAIPrompts[`${model}`]) !== null && _a !== void 0 ? _a : generativeAIPrompts["default"];
if (!modelPrompts) {
throw new InternalServerError(`Neither the model "${model}" nor the default fallback have predefined prompts`, undefined, loggerMeta);
}
let prompt = modelPrompts[`${useCase}`];
// generativeAIPrompts[model] has no prompt for use case, so try to fallback to default prompt
if (!prompt) {
modelPrompts = generativeAIPrompts["default"];
if (!modelPrompts) {
throw new InternalServerError(`The default fallback has no predefined prompts`, undefined, loggerMeta);
}
prompt = modelPrompts[`${useCase}`];
}
if (!prompt) {
throw new InternalServerError(`Neither the model "${model}" nor the default fallback define a prompt for useCase "${useCase}"`, undefined, loggerMeta);
}
if (subUseCase && prompt && typeof prompt === "object" && `${subUseCase}` in prompt) {
prompt = prompt[`${subUseCase}`];
}
if (!prompt) {
throw new InternalServerError(`The prompt defined for the model "${model}" or the default fallback, useCase "${useCase}", and subUseCase "${subUseCase}" is invalid`, undefined, loggerMeta);
}
try {
return promptParser
? promptParser(JSON.parse(JSON.stringify(prompt)))
: JSON.parse(JSON.stringify(prompt));
}
catch (error) {
throw new InternalServerError(`Error while parsing prompt for model: ${model} and useCase: ${useCase} and subUseCase: ${subUseCase}`, undefined, Object.assign({ originalError: error }, loggerMeta));
}
};
//# sourceMappingURL=getPrompt.js.map