feat: add neo4j and prompt and prompt-graph

This commit is contained in:
2024-09-26 01:18:43 +08:00
parent 25c055b490
commit 229ad7caed
12 changed files with 441 additions and 74 deletions

View File

@@ -90,7 +90,7 @@ RouterCodeModel.init(
},
{
sequelize,
tableName: 'cf_router_code',
tableName: 'cf_router_code', // container flow router code
},
);
RouterCodeModel.sync({ alter: true, logging: false }).catch((e) => {

View File

@@ -0,0 +1,83 @@
import { neode } from '@/app.ts';
import { getSession } from '@/modules/neo4j.ts';
import Neode from 'neode';
export const PromptNeo = neode.model('Prompt', {
id: {
type: 'uuid',
primary: true,
},
title: {
type: 'string',
},
description: 'string',
// profile: { type: 'object', optional: true }, // 用于存储 JSON 对象
prompt: 'string',
// inputVariables: { type: 'array', item },
// tags: { type: 'array', items: 'string', optional: true } // 定义字符串数组
inputVariables: { type: 'string', default: JSON.stringify([]) },
localVariables: { type: 'string', default: JSON.stringify([]) },
// 定义可单向或双向的关系
relatedPrompts: {
type: 'relationship',
relationship: 'RELATED_TO',
target: 'Prompt', // 指向自身
direction: 'out', // 默认是单向的
properties: {
created_at: 'datetime',
bidirectional: 'boolean', // 用来标记该关系是否为双向
},
eager: true, // 自动加载相关的 Prompts
},
});
export async function createRelationship(promptA: Neode.Node<unknown>, promptB: Neode.Node<unknown>, isBidirectional = false) {
// 创建单向关系
await promptA.relateTo(promptB, 'RELATED_TO', { created_at: new Date(), bidirectional: isBidirectional });
// 如果是双向关系,创建反向关系
if (isBidirectional) {
await promptB.relateTo(promptA, 'RELATED_TO', { created_at: new Date(), bidirectional: true });
}
}
export async function createRelationship2(promptId1, promptId2, isBidirectional = false) {
const query = `
MATCH (p1:Prompt {id: $id1}), (p2:Prompt {id: $id2})
CREATE (p1)-[r:RELATED_TO {created_at: $createdAt, bidirectional: $bidirectional}]->(p2)
RETURN r
`;
const result = await getSession().run(query, {
id1: promptId1,
id2: promptId2,
createdAt: new Date().toISOString(),
bidirectional: isBidirectional,
});
return result.records[0].get('r');
}
export async function createPrompt(promptData) {
const session = getSession();
const query = `
CREATE (p:Prompt {
id: $id,
title: $title,
description: $description,
prompt: $prompt,
inputVariables: $inputVariables,
localVariables: $localVariables
})
RETURN p
`;
const result = await session.run(query, {
id: promptData.id,
title: promptData.title,
description: promptData.description,
prompt: promptData.prompt,
inputVariables: JSON.stringify(promptData.inputVariables || []),
localVariables: JSON.stringify(promptData.localVariables || []),
});
return result.records[0].get('p');
}

View File

@@ -1,18 +1,65 @@
import { neode } from '@/app.ts';
import { sequelize } from '../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
import { Variable } from '@kevisual/ai-graph';
export const PromptNeo = neode.model('Prompt', {
id: {
type: 'uuid',
primary: true,
/**
* 预设数据,定义了请求的内容和验证器
*/
export type PresetData = {
// 参数
validator: {
[key: string]: any; // 请求的内容的验证器
};
data: {
prompt?: string; // 提前预设值
inputs: Variable & { operate?: string }[]; // 请求内容的变量和内容
};
};
export class Prompt extends Model {
declare id: string;
declare title: string;
declare description: string;
declare presetData: PresetData;
}
Prompt.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.TEXT,
allowNull: true,
},
presetData: {
type: DataTypes.JSON,
},
key: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
// inputVariables: {
// type: DataTypes.JSON,
// defaultValue: [],
// },
// localVariables: {
// type: DataTypes.JSON,
// defaultValue: [],
// },
},
title: {
type: 'string',
{
sequelize, // 传入 Sequelize 实例
modelName: 'prompt', // 模型名称
},
description: 'string',
// profile: { type: 'object', optional: true }, // 用于存储 JSON 对象
prompt: 'string',
// inputVariables: { type: 'array', item },
// tags: { type: 'array', items: 'string', optional: true } // 定义字符串数组
inputVariables: { type: 'string', default: JSON.stringify([]) },
localVariables: { type: 'string', default: JSON.stringify([]) },
);
Prompt.sync({ alter: true, force: false, logging: false }).catch((e) => {
console.error('Prompt sync error', e);
});