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;
|
}
|
}
|