|
@@ -32,24 +32,25 @@ public class AliyunChatClient extends ChatApiClient {
|
|
|
@Override
|
|
|
public ChatEndpoint buildEndpoint(ChatRequest request, LlmAppType appType) {
|
|
|
ChatEndpoint endpoint = new ChatEndpoint();
|
|
|
- // endpoint.setUrl(getConfig().getUrl());//todo
|
|
|
- endpoint.setUrl("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
|
|
|
+ endpoint.setUrl(getConfig().getUrl());
|
|
|
endpoint.setMethod("POST");
|
|
|
|
|
|
Map<String, String> headers = new HashMap<>();
|
|
|
headers.put(AUTH_HEADER_NAME, AUTH_HEADER_VALUE + getConfig().getSecret());
|
|
|
- headers.put("X-DashScope-SSE", "enable");
|
|
|
+ if (request.isStream()) {
|
|
|
+ headers.put("X-DashScope-SSE", "enable");
|
|
|
+ }
|
|
|
endpoint.setHeaders(headers);
|
|
|
|
|
|
- request.setModel(getConfig().getModel());
|
|
|
+ this.buildRequest(request, appType);
|
|
|
|
|
|
- byte[] json;
|
|
|
+ byte[] jsonBytes;
|
|
|
try {
|
|
|
- json = new ObjectMapper().writeValueAsBytes(request);
|
|
|
+ jsonBytes = new ObjectMapper().writeValueAsBytes(request);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- endpoint.setRequestBody(json);
|
|
|
+ endpoint.setRequestBody(jsonBytes);
|
|
|
|
|
|
return endpoint;
|
|
|
}
|
|
@@ -57,26 +58,24 @@ public class AliyunChatClient extends ChatApiClient {
|
|
|
@Override
|
|
|
protected Headers buildHeader(Headers.Builder headerBuilder, LlmAppType appType) {
|
|
|
headerBuilder.add(AUTH_HEADER_NAME, AUTH_HEADER_VALUE + getConfig().getSecret());
|
|
|
- // if (appType == LlmAppType.AUTO_SCORE) {
|
|
|
- // headerBuilder.add("X-DashScope-DataInspection", "{\"input\":\"disable\", \"output\":\"disable\"}");
|
|
|
- // }
|
|
|
return headerBuilder.build();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected Object buildRequest(ChatRequest request, LlmAppType appType) {
|
|
|
- AliyunChatRequest chatRequest = new AliyunChatRequest(request, getConfig().getModel());
|
|
|
+ request.setModel(getConfig().getModel());
|
|
|
if (appType == LlmAppType.AUTO_SCORE) {
|
|
|
- chatRequest.getParameters().put("top_p", 0.1);
|
|
|
+ request.setTopP(0.1f);
|
|
|
} else if (appType == LlmAppType.AUTO_GENERATE_QUESTION) {
|
|
|
- chatRequest.getParameters().put("top_p", 0.9);
|
|
|
+ request.setTopP(0.9f);
|
|
|
}
|
|
|
- return chatRequest;
|
|
|
+ request.setResultFormat("message");
|
|
|
+ return request;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected ChatResult buildResult(byte[] data, ObjectMapper mapper) throws IOException {
|
|
|
- return mapper.readValue(data, AliyunChatResult.class).buildResult();
|
|
|
+ return mapper.readValue(data, ChatResult.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -85,7 +84,7 @@ public class AliyunChatClient extends ChatApiClient {
|
|
|
if (data != null) {
|
|
|
try {
|
|
|
error = mapper.readValue(data, AliyunError.class);
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (Exception ignore) {
|
|
|
}
|
|
|
}
|
|
|
switch (statusCode) {
|
|
@@ -100,19 +99,27 @@ public class AliyunChatClient extends ChatApiClient {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
ChatApiConfig config = new ChatApiConfig();
|
|
|
- config.setSupplier("aliyun");
|
|
|
- config.setUrl("https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation");
|
|
|
- config.setSecret("");
|
|
|
- config.setModel("qwen-turbo");
|
|
|
+ config.setSupplier("Aliyun");
|
|
|
+ config.setUrl("https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
|
|
|
+ config.setSecret("sk-xxx");
|
|
|
+ // config.setModel("qwen3-235b-a22b");
|
|
|
+ // config.setModel("qwen-plus");
|
|
|
+ // config.setModel("qwen-max");
|
|
|
+ // config.setModel("qwen-turbo");
|
|
|
+ // config.setModel("qwen-long");
|
|
|
+ config.setModel("deepseek-v3");
|
|
|
config.setQpm(60);
|
|
|
AliyunChatClient client = new AliyunChatClient(config);
|
|
|
ChatRequest request = new ChatRequest();
|
|
|
request.addMessage(ChatRole.user,
|
|
|
- "作为高等数学科目的命题老师,请按照下列要求出1道单选试题\n" + "试题题干前用单独一行'【题干】'作为内容\n" + "试题答案前用单独一行'【答案】'作为内容\n"
|
|
|
- + "试题答案解析前用单独一行'【解析】'作为内容\n" + "试题包含4个选项,选项内容前用单独一行'【选项】'作为内容,且每个选项前用大写英文字母开头\n"
|
|
|
- + "请按照上述要求出1道高等数学的单选试题");
|
|
|
- System.out.println(
|
|
|
- new ObjectMapper().writeValueAsString(client.call(request, LlmAppType.AUTO_GENERATE_QUESTION)));
|
|
|
+ "作为小学数学科目的命题老师,请按照下列要求出1道单选试题,\n"
|
|
|
+ + "试题题干前用单独一行【题干】作为内容,\n"
|
|
|
+ + "试题答案前用单独一行【答案】作为内容,\n"
|
|
|
+ + "试题答案解析前用单独一行【解析】作为内容,\n"
|
|
|
+ + "试题包含4个选项,选项内容前用单独一行【选项】作为内容,且每个选项前用大写英文字母开头,\n"
|
|
|
+ + "请按照上述要求出1道小学数学的单选试题。");
|
|
|
+ ChatResult chatResult = client.call(request, LlmAppType.AUTO_GENERATE_QUESTION);
|
|
|
+ System.out.println(new ObjectMapper().writeValueAsString(chatResult));
|
|
|
}
|
|
|
|
|
|
}
|