@@ -2,6 +2,7 @@ package com.rj.controller;
import com.rj.dto.DifyWorkflowRequestDto ;
import com.rj.dto.DifyWorkflowResponseDto ;
import com.rj.dto.CustomerProfileAnalysisRequestDto ;
import com.rj.service.DifyWorkflowService ;
import io.swagger.v3.oas.annotations.Operation ;
import io.swagger.v3.oas.annotations.tags.Tag ;
@@ -15,7 +16,7 @@ import java.util.HashMap;
import java.util.Map ;
/**
* Dify工作流测试 控制器
* Dify工作流控制器
*
* @author 李中华
* @date 2025/1/3
@@ -23,18 +24,18 @@ import java.util.Map;
@Slf4j
@RestController
@RequestMapping ( " /api/dify " )
@Tag ( name = " Dify工作流测试 " , description = " Dify工作流API测试 接口 " )
@Tag ( name = " Dify工作流 " , description = " Dify工作流API接口 " )
public class DifyWorkflowController {
@Autowired
private DifyWorkflowService difyWorkflowService ;
/**
* 测试 企微对话分析工作流
* 企微对话分析工作流
*/
@PostMapping ( " /workflow/consulting-scenario " )
@Operation ( summary = " 企微对话分析工作流测试 " , description = " 调用Dify企微对话分析工作流进行测试 " )
public ResponseEntity < DifyWorkflowResponseDto > testC onsultingScenarioWorkflow(
@Operation ( summary = " 企微对话分析工作流 " , description = " 调用Dify企微对话分析工作流 " )
public ResponseEntity < DifyWorkflowResponseDto > c onsultingScenarioWorkflow(
@Valid @RequestBody DifyWorkflowRequestDto requestDto ) {
try {
@@ -79,57 +80,106 @@ public class DifyWorkflowController {
}
}
/**
* 使用预设测试数据测试工作流
*/
@PostMapping ( " /workflow/consulting-scenario/test " )
@Operation ( summary = " 使用预设数据测试企微对话分析工作流 " , description = " 使用预设的测试数据调用工作流 " )
public ResponseEntity < DifyWorkflowResponseDto > testWithPresetData ( ) {
// 创建预设测试数据
DifyWorkflowRequestDto requestDto = new DifyWorkflowRequestDto ( ) ;
requestDto . setUnionId ( " test_union_12345 " ) ;
requestDto . setConsultantId ( " consultant_67890 " ) ;
requestDto . setCommunicateDate ( " 2025-01-03 10:30:00 " ) ;
requestDto . setAnalysisScene ( " sales_consultation " ) ;
requestDto . setAiAnalysisRequestId ( " ai_req_20250103_001 " ) ;
requestDto . setVersion ( 1 ) ;
// 模拟企微对话内容
String chat = " 客户: 你好, 我想了解一下沃尔沃XC60这款车。 \ n " +
" 顾问: 您好! 很高兴为您介绍沃尔沃XC60。这是一款非常优秀的中型SUV, 请问您主要关注哪些方面呢? \ n " +
" 客户:我比较关心安全性能和油耗表现。 \ n " +
" 顾问: XC60在安全方面表现非常出色, 配备了City Safety城市安全系统, 还有Pilot Assist领航辅助系统。油耗方面, 2.0T发动机百公里综合油耗约8.5L。 \ n " +
" 客户:价格大概是多少? \ n " +
" 顾问: XC60的指导价在37.39-47.49万元之间, 目前有优惠活动, 可以优惠3万元左右。 \ n " +
" 客户: 我预算在40万以内, 有什么推荐的配置吗? \ n " +
" 顾问: 根据您的预算, 我推荐智逸豪华版, 指导价39.69万, 优惠后36.69万,完全符合您的预算。 \ n " +
" 客户:好的,我考虑一下,什么时候可以试驾? \ n " +
" 顾问: 明天下午2点可以安排试驾, 您方便吗? \ n " +
" 客户:可以的,我明天下午过去。 \ n " +
" 顾问: 好的, 我为您预约明天下午2点的试驾, 地址是... " ;
requestDto . setChat ( chat ) ;
return testConsultingScenarioWorkflow ( requestDto ) ;
/**
* DCC对话分析工作流
*/
@PostMapping ( " /workflow/dcc-scenario " )
@Operation ( summary = " DCC对话分析工作流 " , description = " 调用Dify DCC对话分析工作流 " )
public ResponseEntity < DifyWorkflowResponseDto > dccScenarioWorkflow (
@Valid @RequestBody DifyWorkflowRequestDto requestDto ) {
try {
log . info ( " 开始调用DCC对话分析工作流, 参数: " + requestDto . toString ( ) ) ;
// 构建工作流输入参数
Map < String , Object > inputs = new HashMap < > ( ) ;
inputs . put ( " unionId " , requestDto . getUnionId ( ) ) ;
inputs . put ( " consultantId " , requestDto . getConsultantId ( ) ) ;
inputs . put ( " communicateDate " , requestDto . getCommunicateDate ( ) ) ;
inputs . put ( " analysisScene " , requestDto . getAnalysisScene ( ) ) ;
inputs . put ( " aiAnalysisRequestId " , requestDto . getAiAnalysisRequestId ( ) ) ;
inputs . put ( " version " , requestDto . getVersion ( ) ) ;
inputs . put ( " chat " , requestDto . getChat ( ) ) ;
// 创建请求对象
DifyWorkflowService . DifyWorkflowRequest request =
new DifyWorkflowService . DifyWorkflowRequest ( inputs , requestDto . getConsultantId ( ) ) ;
// 调用DCC工作流( Service层会自动保存数据到数据库)
DifyWorkflowService . DifyWorkflowResponse response =
difyWorkflowService . callDCCScenarioWorkflow ( request ) ;
// 构建返回结果
DifyWorkflowResponseDto result = DifyWorkflowResponseDto . success (
response . getWorkflowRunId ( ) ,
response . getTaskId ( ) ,
response . getData ( ) ,
response . getMetadata ( )
) ;
return ResponseEntity . ok ( result ) ;
} catch ( Exception e ) {
log . error ( " 调用DCC对话分析工作流失败 " , e ) ;
DifyWorkflowResponseDto errorResult = DifyWorkflowResponseDto . failure (
" DCC工作流调用失败: " + e . getMessage ( ) ,
e . getClass ( ) . getSimpleName ( )
) ;
return ResponseEntity . status ( 500 ) . body ( errorResult ) ;
}
}
/**
* 获取工作流配置信息
* 客户画像分析工作流
*/
@Ge tMapping ( " /workflow/config " )
@Operation ( summary = " 获取工作流配置信息 " , description = " 获取当前Dify工作流的配置信息 " )
public ResponseEntity < Map < String , Object > > getWorkflowConfig ( ) {
Map < String , Object > config = new HashMap < > ( ) ;
config . put ( " message " , " 工作流配置信息 " ) ;
config . put ( " workflowName " , " 企微对话分析工作流 " ) ;
config . put ( " description " , " 对企微对话内容进行深度剖析,输出客户需求、顾问方案等信息 " ) ;
config . put ( " inputParameters " , new String [ ] {
" unionId " , " consultantId " , " communicateDate " ,
" analysisScene " , " aiAnalysisRequestId " , " version " , " chat "
} ) ;
config . put ( " outputFormat " , " JSON格式, 包含analysisResult和analysisDetail " ) ;
@Pos tMapping ( " /workflow/customer-profile-analysis " )
@Operation ( summary = " 客户画像分析工作流 " , description = " 调用Dify客户画像分析工作流 " )
public ResponseEntity < DifyWorkflowResponseDto > customerProfileAnalysisWorkflow (
@Valid @RequestBody CustomerProfileAnalysisRequestDto requestDto ) {
return ResponseEntity . ok ( config ) ;
try {
log . info ( " 开始调用客户画像分析工作流,参数: " + requestDto . toString ( ) ) ;
// 构建工作流输入参数
Map < String , Object > inputs = new HashMap < > ( ) ;
inputs . put ( " chat " , requestDto . getChat ( ) ) ;
inputs . put ( " communicateDate " , requestDto . getCommunicateDate ( ) ) ;
inputs . put ( " analysisScene " , requestDto . getAnalysisScene ( ) ) ;
inputs . put ( " aiAnalysisRequestld " , requestDto . getAiAnalysisRequestId ( ) ) ; // 注意: Dify工作流期望的参数名是aiAnalysisRequestld
inputs . put ( " businessId " , requestDto . getBusinessId ( ) ) ;
inputs . put ( " customerFlowId " , requestDto . getCustomerFlowId ( ) ) ;
inputs . put ( " businessType " , requestDto . getBusinessType ( ) ) ;
// 创建请求对象
DifyWorkflowService . DifyWorkflowRequest request =
new DifyWorkflowService . DifyWorkflowRequest ( inputs , requestDto . getBusinessId ( ) ) ;
// 调用客户画像分析工作流( Service层会自动保存数据到数据库)
DifyWorkflowService . DifyWorkflowResponse response =
difyWorkflowService . callCustomerProfileAnalysisWorkflow ( request ) ;
// 构建返回结果
DifyWorkflowResponseDto result = DifyWorkflowResponseDto . success (
response . getWorkflowRunId ( ) ,
response . getTaskId ( ) ,
response . getData ( ) ,
response . getMetadata ( )
) ;
return ResponseEntity . ok ( result ) ;
} catch ( Exception e ) {
log . error ( " 调用客户画像分析工作流失败 " , e ) ;
DifyWorkflowResponseDto errorResult = DifyWorkflowResponseDto . failure (
" 客户画像分析工作流调用失败: " + e . getMessage ( ) ,
e . getClass ( ) . getSimpleName ( )
) ;
return ResponseEntity . status ( 500 ) . body ( errorResult ) ;
}
}
}