跳到主要内容

高级功能

本文档介绍 OpenHuman 的高级功能。

多模态交互

图像处理

const response = await client.agents.run(agent.id, {
message: '描述这张图片',
attachments: [
{
type: 'image',
url: 'https://example.com/image.jpg',
},
],
});

文件处理

const response = await client.agents.run(agent.id, {
message: '总结这个 PDF',
attachments: [
{
type: 'file',
name: 'report.pdf',
url: 'https://example.com/report.pdf',
},
],
});

Agent 链式调用

// 创建链式调用
const chain = await client.chains.create({
name: '研究写作链',
steps: [
{
type: 'agent',
agentId: researchAgent.id,
prompt: '研究 {{input}} 相关主题',
},
{
type: 'agent',
agentId: writerAgent.id,
prompt: '基于以下研究结果写一篇文章:{{previous}}',
},
{
type: 'agent',
agentId: editorAgent.id,
prompt: '编辑文章,使其更加流畅:{{previous}}',
},
],
});

// 执行链
const result = await client.chains.run(chain.id, {
input: 'AI Agent 的发展趋势',
});

并行处理

const results = await Promise.all([
client.agents.run(agent1.id, { message: '任务 1' }),
client.agents.run(agent2.id, { message: '任务 2' }),
client.agents.run(agent3.id, { message: '任务 3' }),
]);

自定义工具

HTTP 请求工具

const httpTool = await client.tools.register({
name: 'http-request',
description: '发送 HTTP 请求',
parameters: {
type: 'object',
properties: {
url: { type: 'string' },
method: { type: 'string', enum: ['GET', 'POST'] },
headers: { type: 'object' },
body: { type: 'object' },
},
},
execute: async ({ url, method, headers, body }) => {
const response = await fetch(url, { method, headers, body });
return await response.json();
},
});

数据库工具

const dbTool = await client.tools.register({
name: 'query-db',
description: '查询数据库',
parameters: {
type: 'object',
properties: {
sql: { type: 'string' },
},
},
execute: async ({ sql }) => {
const result = await db.query(sql);
return result;
},
});

批量处理

const batch = await client.batches.create({
name: '批量处理任务',
items: [
{ message: '任务 1', metadata: { id: 1 } },
{ message: '任务 2', metadata: { id: 2 } },
{ message: '任务 3', metadata: { id: 3 } },
],
agentId: agent.id,
});

// 监控进度
const status = await client.batches.getStatus(batch.id);
console.log(`进度: ${status.completed}/${status.total}`);

Webhook 回调

await client.agents.update(agent.id, {
webhook: {
url: 'https://your-server.com/webhook',
events: ['onComplete', 'onError'],
},
});

下一步