内容
# 旅行代理教程
本指南提供在 Dify 中设置和运行旅行代理的逐步说明,使用 "llm_en_v_1_3_1" 模型。
## 先决条件
开始前,请确保您具备以下条件:
1. Dify 已通过 Docker 安装并运行(不在 localhost 上运行)
2. 可以访问 LLM 端点
3. 您的端点可以访问模型 "llm_en_v_1_3_1"
## 步骤 1:向 Dify 添加 LLM 模型
1. 点击界面右上角的用户图标
2. 从下拉菜单中选择 "设置"
3. 导航到 "模型提供商" 标签页
4. 如果尚未安装,请安装 "OpenAI-API-compatible" 模型
5. 在 "OpenAI-API-compatible" 模型中,点击 "添加模型" 并按如下方式配置:
- **模型类型**:LLM
- **模型名称**:llm_en_v_1_3_1
- **API 密钥**:留空
- **API 端点 URL**:`http://<您的服务器 IP 地址>:5001/openai/v1`
- **模型上下文大小**:4096
## 步骤 2:设置 MCP 代理
1. 在 Dify 界面的顶部,导航到 "工具" 标签页
2. 点击 "自定义" 标签页
3. 点击 "创建自定义工具"
4. 将 "名称" 字段设置为 "调用 MCP 工具"
5. 确保 MCP 服务器正在运行。您可以通过在终端中输入以下内容来启动它:
```bash
python server.py
```
6. 将 "模式" 字段设置为以下配置:
```json
{
"openapi": "3.0.0",
"info": {
"title": "调用 MCP 工具",
"version": "1.0.0",
"description": "通过 MCP 服务器调用各种 MCP 工具"
},
"servers": [
{
"url": "http://host.docker.internal:8000"
}
],
"paths": {
"/mcp/execute": {
"post": {
"summary": "调用 MCP 工具",
"description": "调用 MCP 工具",
"operationId": "callTool",
"parameters": [
{
"name": "Accept",
"in": "header",
"required": true,
"description": "客户端必须接受 application/json 和 text/event-stream",
"schema": {
"type": "string",
"example": "application/json, text/event-stream"
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"jsonrpc": {
"type": "string"
},
"id": {
"type": "integer"
},
"method": {
"type": "string",
"enum": [
"tools/list", "tools/call"
],
"description": "要执行的 方法"
},
"params": {
"type": "object",
"description": "方法的参数"
}
},
"required": [
"method"
]
}
}
}
},
"responses": {
"200": {
"description": "成功",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"result": {
"type": "string",
"description": "操作的结果"
}
}
}
},
"text/event-stream": {
"schema": {
"type": "string",
"description": "服务器发送事件流"
}
}
}
},
"400": {
"description": "错误请求"
},
"406": {
"description": "不可接受 - 客户端必须接受 application/json 和 text/event-stream",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"example": "客户端必须接受 application/json 和 text/event-stream 内容类型"
}
}
}
}
}
},
"500": {
"description": "内部服务器错误",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"description": "错误消息"
}
}
}
}
}
}
}
}
}
}
}
```
7. 完成后点击 "保存"
## 步骤 3:通过 Dify 集成 LLM 和 MCP 代理
### 创建临时聊天流
1. 在主界面上,找到 "创建应用" 框
2. 点击 "从空白创建"
3. 选择 "聊天流"
4. 将聊天流命名为 "temp"
### 添加 "调用工具" 块
1. 右键单击 "答案" 块
2. 选择 "更改块"
3. 在 "工具" 标签页下,选择 "调用 MCP 工具"
4. 将 "调用工具" 块连接到 LLM 块
### 导出临时聊天流
1. 点击顶部左侧的聊天流名称
2. 点击 "导出 DSL"
3. 将 "temp.yml" 移动到项目文件夹
### 导入旅行代理 yml
1. 在项目文件夹中运行:
```bash
python generate_yml.py
```
2. 在主界面上,找到 "创建应用" 框
3. 点击 "导入 DSL 文件"
4. 选择项目文件夹中的 "旅行代理.yml" 文件
## 配置说明
- 确保将 `<您的服务器 IP 地址>` 替换为实际的服务器 IP 地址
- 确保在最后一步中正确配置所有必需的凭据
- 在尝试使用代理之前,MCP 服务器必须正在运行
## 故障排除
- 如果遇到连接问题,请验证 Docker 是否不在 localhost 上运行
- 确保 MCP 服务器可以在指定的 URL 访问
- 检查所有必需的端口是否打开且可访问