Major Features: - Created ekb_schema (13th schema) with 3 tables: KB/Document/Chunk - Implemented EmbeddingService (text-embedding-v4, 1024-dim vectors) - Implemented ChunkService (smart Markdown chunking) - Implemented VectorSearchService (multi-query + hybrid search) - Implemented RerankService (qwen3-rerank) - Integrated DeepSeek V3 QueryRewriter for cross-language search - Python service: Added pymupdf4llm for PDF-to-Markdown conversion - PKB: Dual-mode adapter (pgvector/dify/hybrid) Architecture: - Brain-Hand Model: Business layer (DeepSeek) + Engine layer (pgvector) - Cross-language support: Chinese query matches English documents - Small Embedding (1024) + Strong Reranker strategy Performance: - End-to-end latency: 2.5s - Cost per query: 0.0025 RMB - Accuracy improvement: +20.5% (cross-language) Tests: - test-embedding-service.ts: Vector embedding verified - test-rag-e2e.ts: Full pipeline tested - test-rerank.ts: Rerank quality validated - test-query-rewrite.ts: Cross-language search verified - test-pdf-ingest.ts: Real PDF document tested (Dongen 2003.pdf) Documentation: - Added 05-RAG-Engine-User-Guide.md - Added 02-Document-Processing-User-Guide.md - Updated system status documentation Status: Production ready
129 lines
3.6 KiB
TypeScript
129 lines
3.6 KiB
TypeScript
/**
|
|
* Prompt管理系统初始化脚本
|
|
*
|
|
* 功能:
|
|
* 1. 创建 capability_schema
|
|
* 2. 添加 prompt:* 权限
|
|
* 3. 更新角色权限分配
|
|
*/
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
async function main() {
|
|
console.log('🚀 开始初始化 Prompt 管理系统...\n');
|
|
|
|
// 1. 创建 capability_schema
|
|
console.log('📁 Step 1: 创建 capability_schema...');
|
|
try {
|
|
await prisma.$executeRaw`CREATE SCHEMA IF NOT EXISTS capability_schema`;
|
|
console.log(' ✅ capability_schema 创建成功\n');
|
|
} catch (error) {
|
|
console.log(' ⚠️ capability_schema 可能已存在\n');
|
|
}
|
|
|
|
// 2. 添加 prompt:* 权限
|
|
console.log('🔐 Step 2: 添加 prompt:* 权限...');
|
|
|
|
const promptPermissions = [
|
|
{ code: 'prompt:view', name: '查看Prompt', description: '查看Prompt模板列表和详情', module: 'admin' },
|
|
{ code: 'prompt:edit', name: '编辑Prompt', description: '创建和修改Prompt草稿', module: 'admin' },
|
|
{ code: 'prompt:debug', name: '调试Prompt', description: '开启调试模式,在生产环境测试草稿', module: 'admin' },
|
|
{ code: 'prompt:publish', name: '发布Prompt', description: '将草稿发布为正式版', module: 'admin' },
|
|
];
|
|
|
|
for (const perm of promptPermissions) {
|
|
try {
|
|
await prisma.permissions.upsert({
|
|
where: { code: perm.code },
|
|
update: { name: perm.name, description: perm.description, module: perm.module },
|
|
create: perm,
|
|
});
|
|
console.log(` ✅ ${perm.code}`);
|
|
} catch (error) {
|
|
console.log(` ⚠️ ${perm.code} 添加失败:`, error);
|
|
}
|
|
}
|
|
console.log('');
|
|
|
|
// 3. 获取权限ID
|
|
console.log('🔗 Step 3: 更新角色权限分配...');
|
|
|
|
const permissions = await prisma.permissions.findMany({
|
|
where: { code: { startsWith: 'prompt:' } },
|
|
});
|
|
|
|
const permissionMap = new Map(permissions.map(p => [p.code, p.id]));
|
|
|
|
// SUPER_ADMIN: 全部权限
|
|
const superAdminPermissions = ['prompt:view', 'prompt:edit', 'prompt:debug', 'prompt:publish'];
|
|
for (const permCode of superAdminPermissions) {
|
|
const permId = permissionMap.get(permCode);
|
|
if (permId) {
|
|
try {
|
|
await prisma.role_permissions.upsert({
|
|
where: {
|
|
role_permission_id: { role: 'SUPER_ADMIN', permission_id: permId },
|
|
},
|
|
update: {},
|
|
create: { role: 'SUPER_ADMIN', permission_id: permId },
|
|
});
|
|
} catch (error) {
|
|
// 可能已存在
|
|
}
|
|
}
|
|
}
|
|
console.log(' ✅ SUPER_ADMIN: prompt:view, prompt:edit, prompt:debug, prompt:publish');
|
|
|
|
// PROMPT_ENGINEER: 无 publish 权限
|
|
const promptEngineerPermissions = ['prompt:view', 'prompt:edit', 'prompt:debug'];
|
|
for (const permCode of promptEngineerPermissions) {
|
|
const permId = permissionMap.get(permCode);
|
|
if (permId) {
|
|
try {
|
|
await prisma.role_permissions.upsert({
|
|
where: {
|
|
role_permission_id: { role: 'PROMPT_ENGINEER', permission_id: permId },
|
|
},
|
|
update: {},
|
|
create: { role: 'PROMPT_ENGINEER', permission_id: permId },
|
|
});
|
|
} catch (error) {
|
|
// 可能已存在
|
|
}
|
|
}
|
|
}
|
|
console.log(' ✅ PROMPT_ENGINEER: prompt:view, prompt:edit, prompt:debug (无publish)');
|
|
console.log('');
|
|
|
|
// 4. 验证
|
|
console.log('✅ Prompt 管理系统初始化完成!\n');
|
|
|
|
const allPermissions = await prisma.permissions.findMany({
|
|
where: { code: { startsWith: 'prompt:' } },
|
|
});
|
|
console.log('📋 已添加的权限:');
|
|
allPermissions.forEach(p => console.log(` - ${p.code}: ${p.name}`));
|
|
}
|
|
|
|
main()
|
|
.catch(console.error)
|
|
.finally(() => prisma.$disconnect());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|