feat: add agent
This commit is contained in:
1
src/routes/agent/index.ts
Normal file
1
src/routes/agent/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
132
src/routes/agent/list.ts
Normal file
132
src/routes/agent/list.ts
Normal 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);
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user