|
|
@ -35,6 +35,10 @@ public class QandATaskServiceImpl implements QandATaskService { |
|
|
|
private SpringBootKafka springBootKafka; |
|
|
|
@Value("${customize-kafka.producer.topic}") |
|
|
|
private String topic; |
|
|
|
@Value("${localModel.apiKey}") |
|
|
|
private String localQaKey; |
|
|
|
@Value("${localModel.url}") |
|
|
|
private String localModelUrl; |
|
|
|
@Override |
|
|
|
public void qandA(Map<String, Object> task) { |
|
|
|
try { |
|
|
@ -144,6 +148,110 @@ public class QandATaskServiceImpl implements QandATaskService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void localQandA(Map<String, Object> task) { |
|
|
|
try { |
|
|
|
log.info("本地任务:{}",JSONObject.toJSONString(task)); |
|
|
|
//结果接收 |
|
|
|
Map<String, Object> result = new HashMap<String, Object>(16); |
|
|
|
//结果内容 |
|
|
|
StringBuffer chatContent = new StringBuffer(); |
|
|
|
//输入配置 |
|
|
|
Map<String, Object> input = (Map<String, Object>) task.get(Constants.INPUT); |
|
|
|
//输出配置 |
|
|
|
Map<String, Object> output = (Map<String, Object>) task.get(Constants.OUTPUT); |
|
|
|
//数据源 |
|
|
|
Map<String, Object> data = (Map<String, Object>) task.get(Constants.DATA); |
|
|
|
int scenesId = (int) task.get(Constants.SCENES_ID); |
|
|
|
int version = (int) task.get(Constants.VERSION); |
|
|
|
String pauseKey = scenesId + "_" + version; |
|
|
|
if (!PauseTool.CACHE.containsKey(pauseKey)) { |
|
|
|
log.info("流程:{}的版本:{}已失效,任务跳过", scenesId, version); |
|
|
|
return; |
|
|
|
} |
|
|
|
//fieldType:自定义输出字段: 0 关闭,1-开启,如果开启则拼接form到output里(如果关闭,则取默认的output拼接) |
|
|
|
int fieldType = (int) input.get(Constants.FIELDTYPE); |
|
|
|
Float temperature = Float.valueOf(input.get(Constants.TEMPERATURE).toString()); |
|
|
|
Float topP = Float.valueOf(input.get(Constants.TOP_P).toString()); |
|
|
|
List<Map<String, Object>> prompt = (List<Map<String, Object>>) input.get(Constants.PROMPT); |
|
|
|
String answerStr = localQandaRequest(temperature, topP, prompt, data); |
|
|
|
log.info("answerStr:" + answerStr); |
|
|
|
Map<String, Object> answer = JSONObject.parseObject(answerStr); |
|
|
|
try { |
|
|
|
//请求成功,正常解析 |
|
|
|
Map<String, Object> message = (Map<String, Object>) answer.get(Constants.MESSAGE); |
|
|
|
chatContent.append(message.get(Constants.CONTENT)); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("问答接口响应体异常:{}", answerStr, e); |
|
|
|
// TODO: handle exception |
|
|
|
//结果集 |
|
|
|
Map<String, Object> results = new HashMap<String, Object>(16); |
|
|
|
//遍历入库返回结果,拼接响应内容 |
|
|
|
results.put("isLast", 1); |
|
|
|
results.put("content", answerStr); |
|
|
|
result.put(Constants.RESULTS, JSONObject.toJSONString(results)); |
|
|
|
result.put(Constants.MESSAGE, "问答异常"); |
|
|
|
result.put(Constants.STATUS, 2); |
|
|
|
task.put(Constants.RESULT, result); |
|
|
|
//发送kafka |
|
|
|
springBootKafka.send(topic, JSONObject.toJSONString(task)); |
|
|
|
log.info("数据流转至下游-------"); |
|
|
|
return; |
|
|
|
} |
|
|
|
Map<String, Object> results = new HashMap<String, Object>(16); |
|
|
|
results.put(Constants.ID, UUID.randomUUID().toString()); |
|
|
|
results.put(Constants.CONTENT, chatContent.toString()); |
|
|
|
if (fieldType != 0) { |
|
|
|
results.remove(Constants.CONTENT); |
|
|
|
try { |
|
|
|
//请求成功,正常解析 |
|
|
|
Map<String, Object> message = (Map<String, Object>) answer.get(Constants.MESSAGE); |
|
|
|
String reponseContent = (String) message.get(Constants.CONTENT); |
|
|
|
Map<String, Object> stringObjectMap = GPTResultParseUtil.parseGPTResult(output, reponseContent); |
|
|
|
results.putAll(stringObjectMap); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("问答接口响应体异常:{}", answerStr, e); |
|
|
|
// TODO: handle exception |
|
|
|
//结果集 |
|
|
|
//遍历入库返回结果,拼接响应内容 |
|
|
|
results.put("isLast", 1); |
|
|
|
results.put("content", answerStr); |
|
|
|
result.put(Constants.RESULTS, JSONObject.toJSONString(results)); |
|
|
|
result.put(Constants.MESSAGE, "问答异常"); |
|
|
|
result.put(Constants.STATUS, 2); |
|
|
|
task.put(Constants.RESULT, result); |
|
|
|
//发送kafka |
|
|
|
springBootKafka.send(topic, JSONObject.toJSONString(task)); |
|
|
|
log.info("数据流转至下游-------"); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
results.put("isLast", 1); |
|
|
|
result.put(Constants.RESULTS, JSONObject.toJSONString(results)); |
|
|
|
result.put(Constants.MESSAGE, "成功"); |
|
|
|
result.put(Constants.STATUS, 1); |
|
|
|
task.put(Constants.RESULT, result); |
|
|
|
//发送kafka |
|
|
|
springBootKafka.send(topic, JSONObject.toJSONString(task)); |
|
|
|
log.info("数据流转至下游-------"); |
|
|
|
} catch (Throwable e) { |
|
|
|
log.error("问答处理异常,", e); |
|
|
|
//结果集 |
|
|
|
Map<String, Object> results = new HashMap<String, Object>(16); |
|
|
|
Map<String, Object> result = new HashMap<String, Object>(16); |
|
|
|
//遍历入库返回结果,拼接响应内容 |
|
|
|
results.put("isLast", 1); |
|
|
|
results.put("content", e.getMessage()); |
|
|
|
result.put(Constants.RESULTS, JSONObject.toJSONString(results)); |
|
|
|
result.put(Constants.MESSAGE, "异常"); |
|
|
|
result.put(Constants.STATUS, 2); |
|
|
|
task.put(Constants.RESULT, result); |
|
|
|
//发送kafka |
|
|
|
springBootKafka.send(topic, JSONObject.toJSONString(task)); |
|
|
|
log.info("数据流转至下游-------"); |
|
|
|
} |
|
|
|
} |
|
|
|
/** |
|
|
|
* 问题请求 |
|
|
|
* |
|
|
@ -178,6 +286,37 @@ public class QandATaskServiceImpl implements QandATaskService { |
|
|
|
String html = DownLoadUtil.doPost(Constants.DEEPSEEK_CHAT_URL, JSONObject.toJSONString(params), headers); |
|
|
|
return html; |
|
|
|
} |
|
|
|
/** |
|
|
|
* 本地模型请求 |
|
|
|
* @param temperature |
|
|
|
* @param topP |
|
|
|
* @param prompt |
|
|
|
* @param data |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private String localQandaRequest(Float temperature, Float topP, List<Map<String, Object>> prompt, Map<String, Object> data) { |
|
|
|
//新建聊天话术 |
|
|
|
StringBuffer chatContent = new StringBuffer(); |
|
|
|
for (Map<String, Object> map : prompt) { |
|
|
|
if (Integer.valueOf(map.get(Constants.TYPE).toString()) == Constants.CHAT_TYPE_ONE) { |
|
|
|
chatContent.append(map.get(Constants.VALUE)); |
|
|
|
} else { |
|
|
|
String jsonPath = (String) map.get(Constants.VALUE); |
|
|
|
chatContent.append(DataUtil.getValue(jsonPath, data).toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
Map<String, Object> headers = new HashMap<String, Object>(16); |
|
|
|
Map<String, Object> params = new HashMap<String, Object>(16); |
|
|
|
List<Map<String, Object>> prompts = buildParam(chatContent.toString(), data); |
|
|
|
params.put(Constants.MESSAGES, prompts); |
|
|
|
params.put(Constants.TEMPERATURE, temperature); |
|
|
|
params.put(Constants.TOP_P, topP); |
|
|
|
params.put(Constants.MAX_TOKENS, 512); |
|
|
|
headers.put("Content-Type", "application/json"); |
|
|
|
headers.put("X-API-Key", localQaKey); |
|
|
|
String html = DownLoadUtil.doPost(localModelUrl, JSONObject.toJSONString(params), headers); |
|
|
|
return html; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 参数构建 |
|
|
|