0.1.0 • Published 7 months ago
@seepine/ai v0.1.0
@seepine/ai
帮助你快速构建拥有 MCP 能力的 Agent
一、基本用法
依赖安装
npm i @seepine/ai创建 Agent
import { ChatOpenAI, Agent } from '@seepine/ai'
const agent = new Agent({
baseURL: process.env['OPENAI_BASE_URL'],
apiKey: process.env['OPENAI_API_KEY']!,
model: process.env['OPENAI_MODEL']!,
prompts: [
{
// 使用提示词构建 agent
role: 'system',
content: '你是一个中英文翻译专家,将用户输入的中文翻译成英文,或将用户输入的英文翻译成中文',
},
],
})
agent.chatSync('荣誉').then((res) => {
// 输出结果 Honor
console.log(res)
})流式输出
for await (const chunk of agent.chat(input)) {
if (chunk.type === 'chat') {
process.stdout.write(chunk.content)
} else {
console.log(chunk)
}
}二、使用 MCP
创建 MCP
import { McpClient } from '@seepine/ai'
// 这是获取网页内容和bing搜索的mcp
const fetchMCP = new McpClient({
name: 'mcp-server-fetch',
type: 'stdio',
command: 'npx',
args: ['-y', '@seepine/mcp-fetch'],
})创建 Agent
import { ChatOpenAI, Agent, McpClient } from '@seepine/ai'
const agent = new Agent({
baseURL: process.env['OPENAI_BASE_URL'],
apiKey: process.env['OPENAI_API_KEY']!,
model: process.env['OPENAI_MODEL']!,
// 传给 Agent
mcps: [fetchMCP],
})使用
for await (const chunk of agent.chat(input)) {
if (chunk.type === 'chat') {
process.stdout.write(chunk.content)
} else if (chunk.type === 'tool_call_begin') {
// 开始调用mcp
} else if (chunk.type === 'tool_call_end') {
// 调用mcp结束
}
}