1、Dify简介
Dify 是开源的LLM 应用开发平台,可以可视化拖拽编排 AI 工作流、RAG 知识库、Agent 智能体,支持私有化部署 / 云端 SaaS,底层兼容 GPT、通义千问、DeepSeek、Ollama 等几乎所有大模型Dify。 核心能力:
- Chat 应用:对话机器人,支持流式 SSE 输出、会话记忆、提示词编排
- Workflow 工作流:画布拖拽多节点(LLM、条件分支、工具调用、知识库检索),构建复杂 Agent 业务流程
- RAG 知识库:文档上传、切片、向量化检索,降低大模型幻觉
- LLMOps:调用日志、标注、用量统计、提示词版本管理
- 完整开放 API:对话、工作流、知识库管理 API,方便业务后端接入 Java/Python 等服务
Dify 两种 API 密钥:
- App ApiKey(应用密钥):调用对话 / 工作流,业务对接用(最常用)
- Server ApiKey:平台管理员接口,管理应用、知识库、模型
2、官方文档
Dify文档:Dify API 快速开始 - Dify Docs
dify-spring-boot4-starter文档:dify-spring-boot-starter/README-zh.md at main · guoshiqiufeng/dify-spring-boot-starter · GitHub
3、dify-spring-boot4-starter介绍
dify-spring-boot4-starter 是社区开源 Spring Boot 4 专用 Starter(仓库:guoshiqiufeng/dify-spring-boot-starter),自动装配 Bean,封装 Dify 全部 API,不用手写 HTTP、SSE、序列化、重试逻辑GitHub。
- 适配:Spring Boot 4.x、Spring Framework 7、Java17+
- 内置 Bean:
DifyChat(对话)、DifyWorkflow(工作流)、DifyDataset(知识库)、DifyServer(平台管理) - 能力:同步调用、SSE 流式返回、文件上传、会话管理、知识库文档增删、连接池、日志脱敏
4、接入过程
4.1 前置准备
- 部署 Dify(docker compose 本地私有化或 dify.ai 云端)
- Dify 控制台新建【聊天应用 / 工作流应用】,发布应用,复制 App ApiKey
- 拿到 Dify 访问地址:
http://127.0.0.1(私有化)或云端地址
4.2 Maven 引入依赖(Spring Boot4 项目 pom.xml)
<dependency>
<groupId>io.github.guoshiqiufeng.dify</groupId>
<artifactId>dify-spring-boot4-starter</artifactId>
<version>${dify-spring-boot-starter.version}</version>
</dependency>
4.3 application.yml 配置
dify:
url: http://127.0.0.1 # Dify 服务地址
# HTTP 客户端超时配置(默认 connect/read/write 均为 30 秒,同步对话易超时被取消)
client-config:
connect-timeout: 30 # 连接超时(秒),默认 30
read-timeout: 300 # 读取超时(秒),默认 30;同步 chat-messages 需等待大模型完整回复,必须调大
write-timeout: 60 # 写入超时(秒),默认 30
call-timeout: 0 # 整个调用的总超时(秒),0 表示不限制,默认 0
sse-read-timeout: 0 # SSE 流式读取超时(秒),0 表示不限制,默认 0
server:
email: aaa@test.com # Dify 服务邮箱(调用 Server API 时需要)
password: 123456 # Dify 服务密码(调用 Server API 时需要)
password-encryption: false # 密码加密开关,默认 true
# Dify 1.11.2+ 需要开启(或使用 Base64 密文)
# Dify 1.11.2 以下版本设置为 false
4.4 代码使用示例
4.4.1 service实现
package com.ybw.service;
import io.github.guoshiqiufeng.dify.chat.DifyChat;
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
import io.github.guoshiqiufeng.dify.chat.dto.response.ChatMessageSendResponse;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class DifyChatService {
@Resource
private DifyChat difyChat;
/**
* 发送消息
*
* @param sendRequest 消息发送请求对象,包含消息内容等信息
* @return ChatMessageSendResponse 消息发送响应对象,包含发送结果等信息
*/
public ChatMessageSendResponse send(ChatMessageSendRequest sendRequest) {
return difyChat.send(sendRequest);
}
}
4.4.2 测试
package com.ybw.service;
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
import io.github.guoshiqiufeng.dify.chat.dto.response.ChatMessageSendResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Slf4j
public class DifyChatServiceTest {
@Resource
private DifyChatService difyChatService;
@Test
public void send() {
ChatMessageSendRequest request = new ChatMessageSendRequest();
//首次对话 / 新会话,不设置(或传空),Dify 自动创建
// request.setConversationId();
request.setUserId("zhangsan");
request.setContent("你好");
request.setApiKey("aaaaaaaaaaaaaa");
ChatMessageSendResponse chatMessageSendResponse = difyChatService.send(request);
// 打印响应结果
log.info("回复:{}",chatMessageSendResponse.getAnswer());
}
}
打印日志
2026-09-11T10:03:38.034+08:00 INFO 24252 --- [spring-boot-dify-demo] [ main] com.ybw.service.DifyChatServiceTest : 回复:<think>
Thinking Process:
1. **Analyze the Input:**
* User says: "你好" (Nǐ hǎo) - This means "Hello" in Chinese.
* Intent: Greeting, initiating conversation.
* Context: No specific context provided yet. I am an AI assistant.
2. **Determine the Appropriate Response:**
* Language: Since the user spoke Chinese, I should respond in Chinese.
* Tone: Polite, friendly, helpful.
* Content: Acknowledge the greeting and offer assistance.
3. **Drafting Potential Responses:**
* Option 1 (Simple): 你好!有什么可以帮你的吗? (Hello! Is there anything I can help you with?)
* Option 2 (Friendly + Capabilities): 你好呀!很高兴见到你。今天想聊聊什么呢,或者需要我帮你处理什么事情? (Hey! Nice to meet you. What would you like to chat about today, or need me to handle something for you?)
* Option 3 (Professional): 您好!请问有什么我可以帮您的吗? (Hello there! May I ask what I can do for you?)
4. **Selecting the Best Response:**
* Option 2 strikes a good balance between friendliness and helpfulness without being overly formal or robotic.
* Add emojis to make it more welcoming? Maybe 🌟, 😊. Let's keep it clean but warm.
5. **Final Polish (Internal Monologue):**
* "你好!很高兴遇见你。今天有什么我可以帮助你的吗?" (Hello! Nice to meet you. Is there anything I can help you with today?) - Simple and effective.
6. **Output Generation:** (Translating thought process into the final output)
* Greet back warmly.
* Offer specific areas of help if needed, or keep it open-ended.
Let's go with: "你好!很高兴见到你。今天有什么我可以帮助你的吗?无论是解答问题、提供建议,还是闲聊聊天,随时告诉我哦~😊" (Hello! Nice to meet you. Is there anything I can help you with today? Whether it's answering questions, providing suggestions, or just chatting casually, let me know anytime~😊)
7. **Refining for Conciseness:**
"你好!很高兴见到你。有什么我可以帮你的吗?无论是问题解答、文档处理,还是日常闲聊,我都在这儿听着呢~ 😊" (Hello! Nice to meet you. Is there anything I can help with? Whether it's Q&A, document processing, or daily chatting, I'm here listening~😊)
Actually, let's keep it very open and welcoming for a first interaction.
"你好!很高兴见到你。今天有什么我可以帮你的吗?无论是查资料、写文案,还是闲聊解压,随时告诉我就好啦~ 😊" (Hello! Nice to meet you. Is there anything I can help with today? Whether checking info, writing copy, or chatting/relaxing, just let me know~😊)
Let's pick a standard friendly response.cw
</think>你好!很高兴见到你。👋
今天有什么我可以帮你的吗?无论是解答问题、提供建议,还是只是想聊聊天,随时告诉我哦~ 😊
ApiKey位置
