@kangthink/q-engine
Version:
A question-answer generation engine that stimulates thinking
155 lines • 7.04 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptManager = void 0;
const types_1 = require("../types");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
class PromptManager {
constructor(promptsDirectory) {
this.prompts = new Map();
// 브라우저 환경에서는 __dirname이 없으므로 안전하게 처리
if (promptsDirectory) {
this.promptsDirectory = promptsDirectory;
}
else if (typeof __dirname !== 'undefined') {
// Node.js 환경
this.promptsDirectory = path.join(__dirname, 'templates');
}
else {
// 브라우저 환경 - 기본 프롬프트만 사용
this.promptsDirectory = '';
// 브라우저에서는 바로 기본 프롬프트 로드
this.loadDefaultPrompts();
}
}
async loadFromFile(filePath) {
// 브라우저 환경에서는 파일 시스템 접근 불가
if (typeof process === 'undefined' || typeof process.versions === 'undefined' || !process.versions.node) {
console.warn('File loading is not supported in browser environment');
return;
}
try {
const content = fs.readFileSync(filePath, 'utf-8');
const template = JSON.parse(content);
this.addPrompt(template);
}
catch (error) {
throw new Error(`Failed to load prompt from file: ${filePath}`);
}
}
async loadFromDirectory(directory) {
const dir = directory || this.promptsDirectory;
// 브라우저 환경에서는 파일 시스템 접근 불가
if (typeof process === 'undefined' || typeof process.versions === 'undefined' || !process.versions.node || !dir) {
console.warn('File system access not available in browser environment. Using default prompts.');
return;
}
if (!fs.existsSync(dir)) {
return;
}
const files = fs.readdirSync(dir).filter(file => file.endsWith('.json'));
for (const file of files) {
await this.loadFromFile(path.join(dir, file));
}
}
addPrompt(template) {
const key = this.generateKey(template.mode, template.id);
this.prompts.set(key, template);
}
getPrompt(mode, id) {
if (id) {
return this.prompts.get(this.generateKey(mode, id));
}
// If no specific id, return the first prompt for the mode
for (const [key, prompt] of this.prompts.entries()) {
if (prompt.mode === mode) {
return prompt;
}
}
return undefined;
}
getDefaultPrompt(mode) {
const defaultPrompts = this.getDefaultPrompts();
const prompt = defaultPrompts.find(p => p.mode === mode);
if (!prompt) {
throw new Error(`No default prompt found for mode: ${mode}`);
}
return prompt;
}
generateKey(mode, id) {
return `${mode}:${id}`;
}
loadDefaultPrompts() {
const defaultPrompts = this.getDefaultPrompts();
defaultPrompts.forEach(prompt => this.addPrompt(prompt));
}
getDefaultPrompts() {
return [
{
id: 'default',
mode: types_1.TransformMode.FIVE_W_ONE_H,
template: `"{{content}}"에 대해 육하원칙(누가, 무엇을, 언제, 어디서, 왜, 어떻게)을 적용한 {{targetType}}{{count}}개를 생성하세요. 각 줄에 하나씩 질문만 작성하세요.`,
variables: ['content', 'targetType', 'count'],
},
{
id: 'default',
mode: types_1.TransformMode.SOCRATIC,
template: `"{{content}}"에 대해 소크라테스식 {{targetType}}{{count}}개를 생성하세요. 가정을 의심하고 더 깊은 사고를 유도하는 질문만 각 줄에 하나씩 작성하세요.`,
variables: ['content', 'targetType', 'count'],
},
{
id: 'default',
mode: types_1.TransformMode.MATRIX,
template: `"{{content}}"에 대해 Matrix 방법(레벨{{level}}, {{direction}})으로 {{targetType}}{{count}}개를 생성하세요. {{levelDescription}} {{directionDescription}} 질문만 각 줄에 하나씩 작성하세요.`,
variables: ['content', 'targetType', 'count', 'level', 'levelDescription', 'direction', 'directionDescription'],
},
{
id: 'default',
mode: types_1.TransformMode.SCIENTIFIC,
template: `"{{content}}"에 대해 과학적 질문 방법(Scientific Method)으로 {{targetType}}{{count}}개를 생성하세요. 관찰(observation), 가설(hypothesis), 변수(variable), 실험(experiment), 분석(analysis), 결론(conclusion)의 각 단계에 해당하는 질문만, 각 줄에 하나씩 작성하세요.`,
variables: ['content', 'targetType', 'count'],
},
{
id: 'default',
mode: types_1.TransformMode.ANSWER,
template: `"{{content}}"에 대한 {{targetType}}을 {{lengthGuidance}} {{toneGuidance}} {{exampleGuidance}} 작성하세요. 정확하고 유용한 정보를 제공하되, 명확하고 이해하기 쉽게 설명하세요.`,
variables: ['content', 'targetType', 'lengthGuidance', 'toneGuidance', 'exampleGuidance'],
},
];
}
}
exports.PromptManager = PromptManager;
//# sourceMappingURL=PromptManager.js.map