跳到主要内容

基本使用

本指南帮助您快速上手 OpenHuman。

创建您的第一个 Agent

npx openhuman agent create --name "我的助手"

或使用代码:

import { OpenHuman } from 'openhuman-sdk';

const client = new OpenHuman({
apiKey: process.env.OPENHUMAN_API_KEY,
});

const agent = await client.agents.create({
name: '我的助手',
instructions: '你是一个有用的助手,可以回答各种问题。',
});

console.log('Agent ID:', agent.id);

开始对话

const response = await client.agents.run(agent.id, {
message: '你好,请介绍一下你自己',
});

console.log(response.message);

使用工具

Agent 可以使用多种工具执行任务:

const response = await client.agents.run(agent.id, {
message: '帮我搜索最新的 AI 新闻',
tools: ['web-search'], // 指定要使用的工具
});

管理记忆

// 存储信息
await client.memory.store({
content: '用户喜欢简洁的设计风格',
source: 'conversation',
tags: ['preference', 'design'],
});

// 检索记忆
const memories = await client.memory.recall({
query: '用户的设计偏好',
});

配置 Agent

const agent = await client.agents.create({
name: '技术文档助手',
instructions: `
你是一个专业的技术文档助手。

行为规范:
- 回答简洁明了
- 提供代码示例时使用 markdown 格式
- 遇到不确定的问题时如实说明
`,
tools: ['web-search', 'web-scraper'],
model: 'claude-sonnet-4-20250514',
});

监控运行状态

const run = await client.agents.run(agent.id, {
message: '写一篇关于 AI 的博客文章',
});

console.log('Run ID:', run.id);
console.log('Status:', run.status);

// 获取结果
const result = await client.runs.get(run.id);
console.log(result.message);

下一步