@ deepagents/ 代理服务器
用TypeScript构建多代理AI系统的框架. 创建使用工具的代理,通过交接进行协调,并合作解决复杂任务.
特征
- 代理组成 - 建立具有特定作用和能力的模块化代理人
- 工具整合 - 与Vercel AI SDK工具兼容
- 处理 - 特工可以自动委托给专门人员
- 结构化输出 - 与Zod计划进行类型安全反应
- 流线 - 实时流出反应
- 背景共享 - 特工之间的安全状态
安装
npm install @deepagents/agent
需要节点js LTS 和 a zod 相对依赖.
快速启动
简单代理
import { openai } from '@ai-sdk/openai';
import { agent, execute, user } from '@deepagents/agent';
const assistant = agent({
name: 'assistant',
model: openai('gpt-4o'),
prompt: 'You are a helpful assistant.',
});
const stream = execute(assistant, 'Hello!', {});
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}
有工具的代理
import { openai } from '@ai-sdk/openai';
import { tool } from 'ai';
import { z } from 'zod';
import { agent, execute } from '@deepagents/agent';
const weatherTool = tool({
description: 'Get weather for a location',
parameters: z.object({
location: z.string(),
}),
execute: async ({ location }) => {
return { temperature: 72, condition: 'sunny', location };
},
});
const weatherAgent = agent({
name: 'weather_agent',
model: openai('gpt-4o'),
prompt: 'You help users check the weather.',
tools: { weather: weatherTool },
});
const stream = execute(weatherAgent, 'What is the weather in Tokyo?', {});
console.log(await stream.text);
拥有交接权的多代理人
import { openai } from '@ai-sdk/openai';
import { agent, instructions, swarm } from '@deepagents/agent';
const researcher = agent({
name: 'researcher',
model: openai('gpt-4o'),
prompt: 'You research topics and provide detailed information.',
handoffDescription: 'Handles research and fact-finding tasks',
tools: {/* research tools */},
});
const writer = agent({
name: 'writer',
model: openai('gpt-4o'),
prompt: 'You write clear, engaging content based on research.',
handoffDescription: 'Handles writing and content creation',
});
const coordinator = agent({
name: 'coordinator',
model: openai('gpt-4o'),
prompt: instructions({
purpose: ['Coordinate research and writing tasks'],
routine: [
'Analyze the request',
'Use transfer_to_researcher for fact-finding',
'Use transfer_to_writer for content creation',
],
}),
handoffs: [researcher, writer],
});
// Agents automatically transfer control via transfer_to_<agent_name> tools
const stream = swarm(coordinator, 'Write a blog post about AI agents', {});
结构化输出
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
import { agent, generate } from '@deepagents/agent';
const analyzer = agent({
name: 'analyzer',
model: openai('gpt-4o'),
prompt: 'Analyze the sentiment of the given text.',
output: z.object({
sentiment: z.enum(['positive', 'negative', 'neutral']),
confidence: z.number(),
keywords: z.array(z.string()),
}),
});
const result = await generate(analyzer, 'I love this product!', {});
console.log(result.output);
// { sentiment: 'positive', confidence: 0.95, keywords: ['love', 'product'] }
背景变量
代理和工具之间的共享状态 :
import { type ToolExecutionOptions, tool } from 'ai';
import { z } from 'zod';
import { agent, execute } from '@deepagents/agent';
// A `type`, not an `interface`: the AI SDK requires a tool context to
// extend Record<string, unknown>, which an interface does not.
type AppContext = {
userId: string;
preferences: Record<string, string>;
};
const preferenceTool = tool({
description: 'Save user preference',
inputSchema: z.object({
key: z.string(),
value: z.string(),
}),
execute: async (
{ key, value },
{ context }: ToolExecutionOptions<AppContext>,
) => {
context.preferences[key] = value;
return `Saved ${key}=${value}`;
},
});
const assistant = agent<unknown, AppContext>({
name: 'assistant',
model: openai('gpt-4o'),
prompt: (ctx) => `Help user ${ctx?.userId} manage their preferences.`,
tools: { savePreference: preferenceTool },
});
const context: AppContext = { userId: 'user123', preferences: {} };
const stream = execute(assistant, 'Set my theme to dark', context);
await stream.text;
console.log(context.preferences); // { theme: 'dark' }
API 参考
agent(config)
创建新代理 :
agent<Output, ContextIn, ContextOut>({
name: string; // Agent identifier (required)
model: LanguageModel; // AI SDK model (required)
prompt: string | string[] | ((ctx?) => string); // Instructions
tools?: ToolSet; // Available tools
handoffs?: Agent[]; // Agents to delegate to
handoffDescription?: string; // When to use this agent
output?: z.Schema<Output>; // Structured output schema
temperature?: number; // LLM temperature
toolChoice?: ToolChoice; // 'auto' | 'none' | 'required'
})
instructions({ purpose, routine })
结构化提示的助手 :
instructions({
purpose: string | string[], // Agent's role
routine: string[], // Step-by-step workflow
})
// For swarm coordination
instructions.swarm({ purpose, routine })
执行函数
// Streaming execution
execute(agent, messages, context, config?)
stream(agent, messages, context, config?) // alias
// Non-streaming execution
generate(agent, messages, context, config?)
// High-level UI streaming with handoff support
swarm(agent, messages, context, abortSignal?)
公用事业
// Create user message
user(message: string): UIMessage
// Extract structured output
toOutput<T>(result): Promise<T>
AI 模式提供者
与任何模型提供者一起工作 维塞尔 AI SDK数字 :
import { anthropic } from '@ai-sdk/anthropic';
import { google } from '@ai-sdk/google';
import { groq } from '@ai-sdk/groq';
import { openai } from '@ai-sdk/openai';
const agent1 = agent({ model: openai('gpt-4o') /* ... */ });
const agent2 = agent({
model: anthropic('claude-sonnet-4-20250514') /* ... */,
});
const agent3 = agent({ model: google('gemini-1.5-pro') /* ... */ });
文档
备有文件全文 (原始内容存档于2013-10-12) (日语). januarylabs.github.io/deepagents。 。 。 .