Administrator
2025-09-05 14bff9c21f86ebced8436fa0d92ed20a1f8384aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package cc.mrbird.febs.ai.service.impl;
 
import cc.mrbird.febs.ai.req.AiProductQuestionAiDto;
import cc.mrbird.febs.ai.service.AiService;
import cc.mrbird.febs.common.exception.FebsException;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
 
@Slf4j
@Service
@RequiredArgsConstructor
public class AiServiceImpl implements AiService {
 
    private static final String apiKey = "sk-babdcf8799144134915cee2683794b2f";
    private static final String appId = "963b854d994a4f578d8207cd477a2464";
 
    private final String bizParam_1 = "question";
    private final String bizParam_2 = "prompt_ai_system";
    private final String bizParam_3 = "json_format";
 
    @Override
    public String llmInvokeNonStreaming(AiProductQuestionAiDto dto) {
        if (ObjectUtil.isNull(dto)){
            throw new FebsException("百炼工作流初始化异常");
        }
 
        HashMap<String, Object> bizParams = new HashMap<>();
        bizParams.put(bizParam_1,dto.getQuestionCnt());
        bizParams.put(bizParam_2,dto.getPromptAiSystem());
        bizParams.put(bizParam_3,dto.getJsonFormat());
        String query = dto.getQuery();
        long startTime = System.currentTimeMillis();
//        ApplicationParam param = ApplicationParam.builder()
//                // 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
//                .apiKey(apiKey)
//                .appId(appId) //替换为实际的应用 ID
//                .flowStreamMode(FlowStreamMode.MESSAGE_FORMAT)
//                .prompt(query)
//                .bizParams(JsonUtils.toJsonObject( bizParams))
//                .build();
//
//        Application application = new Application();
//        Flowable<ApplicationResult> result;
//        try {
//            result = application.streamCall(param);
//        } catch (NoApiKeyException | InputRequiredException e) {
//            throw new FebsException(StrUtil.format("百炼工作流输出失败:{}",e.getMessage()));
//        }
//
//        return Flux.from(result)
//                .map(message -> {
//                    HashMap<String, String> stringStringHashMap = new HashMap<>();
//                    if (!message.getOutput().getFinishReason().equals("stop")){
//                        stringStringHashMap.put(LlmStrategyContextEnum.CONTENT.name(),message.getOutput().getWorkflowMessage().getMessage().getContent());
//                    }
//                    return new FebsResponse().success().data(stringStringHashMap);
//                })
//                .doOnComplete(() -> {
//                    long endTime = System.currentTimeMillis();
//                    System.out.println("百炼工作流输出:" + (endTime - startTime) + "毫秒");
//                })
//                .doOnError(error -> {
//                    throw new FebsException(StrUtil.format("百炼工作流输出失败:{}",error));
//                });
        return null;
    }
}