172 lines
3.6 KiB
Markdown
172 lines
3.6 KiB
Markdown
---
|
||
title: AI 智能体接入
|
||
description: 集成 AI Agent,实现知识库问答、工作流触发与多模型切换。
|
||
category: ai
|
||
order: 1
|
||
---
|
||
|
||
# AI 智能体接入
|
||
|
||
> 集成 AI Agent,实现知识库问答、工作流触发与多模型切换。
|
||
|
||
## 🧠 什么是 AI 智能体?
|
||
|
||
AI 智能体(AI Agent)是 Websopy 平台的核心能力,它能:
|
||
|
||
- 🤖 基于自然语言理解用户意图
|
||
- 📚 连接你的知识库进行精准问答
|
||
- 🔗 触发业务工作流执行自动化任务
|
||
- 🔄 在多个 AI 模型间智能切换
|
||
|
||
## 📋 前提条件
|
||
|
||
- 已完成 [快速上手](/developer/docs/getting-started/quickstart)
|
||
- 已开通 AI 功能(控制台 → 开发者中心 → AI 功能)
|
||
|
||
## 🚀 接入步骤
|
||
|
||
### 第一步:安装 AI SDK
|
||
|
||
```bash
|
||
npm install @websopy/ai-sdk
|
||
```
|
||
|
||
### 第二步:初始化 Agent
|
||
|
||
```typescript
|
||
import { AIAgent } from '@websopy/ai-sdk'
|
||
|
||
const agent = new AIAgent({
|
||
apiKey: process.env.WEBSOPY_API_KEY,
|
||
defaultModel: 'gpt-4',
|
||
models: ['gpt-4', 'claude-3', 'gemini-pro']
|
||
})
|
||
```
|
||
|
||
### 第三步:创建会话
|
||
|
||
```typescript
|
||
const session = await agent.createSession({
|
||
userId: 'user-123',
|
||
knowledgeBaseId: 'kb-abc456',
|
||
metadata: {
|
||
source: 'website',
|
||
language: 'zh-CN'
|
||
}
|
||
})
|
||
|
||
console.log('会话 ID:', session.id)
|
||
```
|
||
|
||
### 第四步:发送消息
|
||
|
||
```typescript
|
||
const response = await agent.sendMessage(session.id, {
|
||
content: '我想了解你们的产品价格',
|
||
context: {
|
||
userPlan: 'free',
|
||
priority: 'normal'
|
||
}
|
||
})
|
||
|
||
console.log('AI 回复:', response.content)
|
||
console.log('使用模型:', response.model)
|
||
```
|
||
|
||
### 第五步:流式响应(SSE)
|
||
|
||
```typescript
|
||
const stream = await agent.sendMessageStream(session.id, {
|
||
content: '写一篇关于 AI 的博客文章'
|
||
})
|
||
|
||
for await (const chunk of stream) {
|
||
process.stdout.write(chunk.content)
|
||
}
|
||
```
|
||
|
||
## 📚 知识库集成
|
||
|
||
```typescript
|
||
const kb = await agent.createKnowledgeBase({
|
||
name: '产品文档',
|
||
description: '公司产品相关文档',
|
||
embeddingModel: 'text-embedding-3-small'
|
||
})
|
||
|
||
await agent.uploadDocument(kb.id, {
|
||
file: './docs/product-guide.pdf',
|
||
metadata: {
|
||
category: 'documentation',
|
||
version: '2.0'
|
||
}
|
||
})
|
||
```
|
||
|
||
## 🔄 多模型切换
|
||
|
||
### 自动路由
|
||
|
||
根据任务类型自动选择最优模型:
|
||
|
||
```typescript
|
||
const agent = new AIAgent({
|
||
apiKey: process.env.WEBSOPY_API_KEY,
|
||
autoRoute: {
|
||
enabled: true,
|
||
rules: [
|
||
{ task: 'coding', models: ['gpt-4', 'claude-3'] },
|
||
{ task: 'creative', models: ['gpt-4', 'gemini-pro'] },
|
||
{ task: 'analysis', models: ['claude-3', 'gpt-4'] }
|
||
]
|
||
}
|
||
})
|
||
```
|
||
|
||
## 🧪 完整示例
|
||
|
||
```typescript
|
||
import { AIAgent } from '@websopy/ai-sdk'
|
||
|
||
async function main() {
|
||
const agent = new AIAgent({
|
||
apiKey: process.env.WEBSOPY_API_KEY,
|
||
defaultModel: 'gpt-4'
|
||
})
|
||
|
||
const session = await agent.createSession({
|
||
userId: 'user-123',
|
||
knowledgeBaseId: 'kb-product-docs'
|
||
})
|
||
|
||
const messages = [
|
||
'你好,我想了解一下企业版的功能',
|
||
'支持私有化部署吗?',
|
||
'好的,我想试用一下'
|
||
]
|
||
|
||
for (const msg of messages) {
|
||
const response = await agent.sendMessage(session.id, { content: msg })
|
||
console.log(`\n👤 用户: ${msg}`)
|
||
console.log(`🤖 AI: ${response.content}`)
|
||
}
|
||
}
|
||
|
||
main()
|
||
```
|
||
|
||
## ❓ 常见问题
|
||
|
||
### Q: 支持哪些 Embedding 模型?
|
||
|
||
目前支持:
|
||
- `text-embedding-3-small`(推荐,速度快)
|
||
- `text-embedding-3-large`(高精度)
|
||
- `text-embedding-ada-002`(兼容性)
|
||
|
||
### Q: 知识库检索不到相关内容?
|
||
|
||
1. 检查文档是否已成功上传和向量化
|
||
2. 调整 `scoreThreshold` 降低阈值
|
||
3. 尝试增加 `topK` 获取更多候选
|