feat: add agent

This commit is contained in:
2024-09-27 23:03:30 +08:00
parent cfdf2ba00d
commit 718322ae47
16 changed files with 446 additions and 43 deletions

View File

@@ -7,5 +7,5 @@ import { app as aiApp } from '@kevisual/ai-lang/src/index.ts';
export { aiApp };
export { app };
app.listen(config.port, () => {
console.log(`server is running at http://localhost:${config.port}`);
console.log(`server2 is running at http://localhost:${config.port}`);
});

85
src/models/agent.ts Normal file
View File

@@ -0,0 +1,85 @@
import { sequelize } from '@/modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export class AiAgent extends Model {
id: string;
type: string;
model: string;
baseUrl: string;
apiKey: string;
temperature: number;
cache: string;
cacheName: string;
status: string;
data: any;
key: string;
}
// 获取AIAgent的属性
export type AiProperties = {
id: string;
type: string;
model: string;
baseUrl: string;
apiKey?: string;
temperature?: number;
cache?: string;
cacheName?: string;
data?: any;
};
AiAgent.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
type: {
type: DataTypes.STRING,
allowNull: false,
},
status: {
type: DataTypes.STRING,
defaultValue: 'open',
},
model: {
type: DataTypes.STRING,
allowNull: false,
},
baseUrl: {
type: DataTypes.STRING,
allowNull: false,
},
apiKey: {
type: DataTypes.STRING,
allowNull: false,
},
key: {
type: DataTypes.STRING,
},
temperature: {
type: DataTypes.FLOAT,
allowNull: true,
},
cache: {
type: DataTypes.STRING,
allowNull: true,
},
cacheName: {
type: DataTypes.STRING,
allowNull: true,
},
data: {
type: DataTypes.JSON,
allowNull: true,
defaultValue: {},
},
},
{
sequelize,
tableName: 'ai_agent',
},
);
AiAgent.sync({ alter: true, logging: false }).catch((e) => {
console.error('AiAgent sync error', e);
});

View File

@@ -0,0 +1 @@
import './list.ts';

132
src/routes/agent/list.ts Normal file
View File

@@ -0,0 +1,132 @@
import { app } from '@/app.ts';
import { AiAgent, AiProperties } from '@/models/agent.ts';
import { CustomError } from '@abearxiong/router';
import { agentManger } from '@kevisual/ai-lang';
import { v4 } from 'uuid';
app
.route('agent', 'list')
.define(async (ctx) => {
const agents = await AiAgent.findAll({
order: [['updatedAt', 'DESC']],
// 返回的内容不包含apiKey的字段
attributes: { exclude: ['apiKey'] },
});
ctx.body = agents;
})
.addTo(app);
app
.route('agent', 'get')
.define(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
ctx.body = await AiAgent.findByPk(id, {
attributes: { exclude: ['apiKey'] },
});
return ctx;
})
.addTo(app);
app
.route('agent', 'update')
.define(async (ctx) => {
const { id, ...rest } = ctx.query.data;
let agent = await AiAgent.findByPk(id);
if (!agent) {
agent = await AiAgent.create(rest);
ctx.body = agent;
return ctx;
}
await agent.update(rest);
ctx.body = agent;
return ctx;
})
.addTo(app);
app
.route('agent', 'delete')
.define(async (ctx) => {
const id = ctx.query.id;
if (!id) {
throw new CustomError('id is required');
}
const agent = await AiAgent.findByPk(id);
if (!agent) {
throw new CustomError('agent not found');
}
await agent.destroy();
ctx.body = agent;
return ctx;
})
.addTo(app);
app
.route('agent', 'test')
.define(async (ctx) => {
const { message } = ctx.query;
const data: AiProperties = {
type: 'ollama',
id: 'test',
model: 'qwen2.5:14b',
baseUrl: 'http://mz.zxj.im:11434',
cache: 'memory',
};
const agent = agentManger.createAgent(data as any);
const res = await agent.sendHumanMessage(message);
// agent.close();
agentManger.removeAgent(agent.id);
ctx.body = res;
return ctx;
})
.addTo(app);
export const agentModelList = ['qwen2.5:14b', 'qwen2.5-coder:7b', 'llama3.1:8b', 'bakllava:latest'] as const;
export const openAiModels = ['gpt-4o'];
const demoData: AiProperties[] = [
{
id: v4(),
type: 'openai',
model: 'gpt-4o',
baseUrl: 'https://oneapi.on-ai.ai/v1',
apiKey: 'sk-GJE6I8OJWDr2ErFBD4C4706a65Ad4cD9B596Cf7c76943e45',
},
...agentModelList.map((item) => {
return {
id: v4(),
type: 'ollama',
model: item,
baseUrl: 'http://mz.zxj.im:11434',
apiKey: 'sk-GJE6I8OJWDr2ErFBD4C4706a65Ad4cD9B596Cf7c76943e45',
};
}),
];
// AiAgent.bulkCreate(demoData, { ignoreDuplicates: true }).then(() => {
// console.log('create demo data success');
// });
const initManager = async () => {
// const list = await AiAgent.findAll();
const list = await AiAgent.findAll({
where: {
status: 'open',
},
});
const data = list.map((item) => {
return {
id: item.id,
type: item.type as any,
model: item.model as any,
baseUrl: item.baseUrl,
apiKey: item.apiKey,
temperature: item.temperature,
cache: item.cache as any,
cacheName: item.cacheName,
};
});
agentManger.createAgentList(data);
};
setTimeout(() => {
initManager();
}, 1000);

View File

@@ -4,4 +4,6 @@ import './page/index.ts';
import './resource/index.ts';
import './prompt-graph/index.ts';
import './prompt-graph/index.ts';
import './agent/index.ts';