dify util
parent
1499030dc4
commit
44d7df942f
@ -0,0 +1,28 @@
|
||||
package com.supervision.common.constant;
|
||||
|
||||
public class DifyConstants {
|
||||
public static final String QA_TYPE_DIFY = "0";
|
||||
public static final String QA_TYPE_NX_LLM = "1";
|
||||
|
||||
public static final String INTENT_TYPE_INDEX_RESULT = "0";
|
||||
public static final String INTENT_TYPE_CASE_RESULT = "1";
|
||||
public static final String INTENT_TYPE_CASE_OVERVIEW = "2";
|
||||
public static final String INTENT_TYPE_CASE_EVIDENCE_GUIDE = "3";
|
||||
|
||||
public static final String METHOD_DATASET = "/datasets";
|
||||
public static final String METHOD_DOCUMENT = "/document";
|
||||
public static final String METHOD_DOCUMENTS = "/documents";
|
||||
public static final String METHOD_CREATE_BY_FILE = "/create-by-file";
|
||||
public static final String METHOD_CREATE_BY_TEXT = "/create_by_text";
|
||||
public static final String METHOD_CHAT_MESSAGES = "/chat-messages";
|
||||
|
||||
public static final String DATASET_INDEXING_TECHNIQUE_HIGH_QUALITY = "high_quality";
|
||||
public static final String CHAT_RESPONSE_MODE_BLOCKING = "blocking";
|
||||
public static final String CHAT_RESPONSE_MODE_STREAMING = "streaming";
|
||||
|
||||
public static final String INTENT_TYPE_TEXT_CASE_RESULT = "案件分析结果";
|
||||
public static final String INTENT_TYPE_TEXT_CASE_OVERVIEW = "案件概况";
|
||||
public static final String INTENT_TYPE_TEXT_CASE_EVIDENCE_GUIDE = "案件证据指引";
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DIFYChatReqInputVO {
|
||||
private String type;
|
||||
private String intent_type;
|
||||
private String case_id;
|
||||
private String user_id;
|
||||
private String conversation_id;
|
||||
private String dataset_id;
|
||||
private String participator;
|
||||
private int dialogue_count;
|
||||
private String previous_query;
|
||||
private String previous_answer;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import static com.supervision.common.constant.DifyConstants.DATASET_INDEXING_TECHNIQUE_HIGH_QUALITY;
|
||||
|
||||
@Data
|
||||
public class DatasetReqVO {
|
||||
private String name;
|
||||
private String description;
|
||||
private String indexing_technique = DATASET_INDEXING_TECHNIQUE_HIGH_QUALITY;
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DatasetResVO {
|
||||
private String id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String provider;
|
||||
private String permission;
|
||||
private String data_source_type;
|
||||
private String indexing_technique;
|
||||
private String app_count;
|
||||
private String document_count;
|
||||
private String word_count;
|
||||
private String created_by;
|
||||
private String created_at;
|
||||
private String updated_by;
|
||||
private String updated_at;
|
||||
private String embedding_model;
|
||||
private String embedding_model_provider;
|
||||
private String embedding_available;
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import static com.supervision.common.constant.DifyConstants.CHAT_RESPONSE_MODE_BLOCKING;
|
||||
|
||||
@Data
|
||||
public class DifyChatReqVO {
|
||||
private String conversationId;
|
||||
private String user;
|
||||
private String query;
|
||||
private String response_mode = CHAT_RESPONSE_MODE_BLOCKING;
|
||||
private DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DifyChatResVO {
|
||||
private String event;
|
||||
private String task_id;
|
||||
private String id;
|
||||
private String message_id;
|
||||
private String conversation_id;
|
||||
private String mode;
|
||||
private String answer;
|
||||
private Metadata metadata;
|
||||
private String created_at;
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class KnowledgeBaseSegmentVO {
|
||||
private String id;
|
||||
private String name;
|
||||
private String snippet;
|
||||
private String score;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Metadata {
|
||||
private Usage usage;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.supervision.model.dify;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Usage {
|
||||
private int promptTokens;
|
||||
private String promptUnitPrice;
|
||||
private String promptPriceUnit;
|
||||
private String promptPrice;
|
||||
private int completionTokens;
|
||||
private String completionUnitPrice;
|
||||
private String completionPriceUnit;
|
||||
private String completionPrice;
|
||||
private int totalTokens;
|
||||
private String totalPrice;
|
||||
private String currency;
|
||||
private double latency;
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package com.supervision.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.supervision.model.dify.DifyChatReqVO;
|
||||
import com.supervision.model.dify.DifyChatResVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.hc.client5.http.ClientProtocolException;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpHeaders;
|
||||
import org.apache.hc.core5.http.HttpStatus;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.apache.hc.core5.http.io.entity.StringEntity;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static com.supervision.common.constant.DifyConstants.METHOD_CHAT_MESSAGES;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DifyApiUtil {
|
||||
|
||||
@Value("${dify.url}")
|
||||
private String difyUrl;
|
||||
@Value("${dify.dataset-auth}")
|
||||
private String difyDatasetAuth;
|
||||
@Value("${dify.app-auth}")
|
||||
private String difyAppAuth;
|
||||
|
||||
public DifyChatResVO chat(DifyChatReqVO difyChatReqVO) {
|
||||
DifyChatResVO difyChatResVO = null;
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
HttpPost httpPost = new HttpPost(difyUrl + METHOD_CHAT_MESSAGES);
|
||||
httpPost.setHeader(HttpHeaders.AUTHORIZATION, difyAppAuth);
|
||||
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||
log.info("发起对话:{}", difyChatReqVO);
|
||||
StringEntity entity = new StringEntity(com.alibaba.fastjson.JSONObject.toJSON(difyChatReqVO).toString(), StandardCharsets.UTF_8);
|
||||
httpPost.setEntity(entity);
|
||||
difyChatResVO = httpClient.execute(httpPost, response -> {
|
||||
final int status = response.getCode();
|
||||
if (status >= HttpStatus.SC_SUCCESS && status < HttpStatus.SC_REDIRECTION) {
|
||||
final HttpEntity responseEntity = response.getEntity();
|
||||
String responseStr = EntityUtils.toString(responseEntity);
|
||||
log.info("发起对话成功!");
|
||||
return JSON.parseObject(responseStr, DifyChatResVO.class);
|
||||
} else {
|
||||
log.info("responseEntity {}", EntityUtils.toString(response.getEntity()));
|
||||
throw new ClientProtocolException("Unexpected response status: " + status);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("发起对话失败", e);
|
||||
}
|
||||
return difyChatResVO;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue