|
@@ -0,0 +1,52 @@
|
|
|
+package io.github.qifan777.knowledge;
|
|
|
+
|
|
|
+import com.alibaba.dashscope.aigc.generation.Generation;
|
|
|
+import com.alibaba.dashscope.aigc.generation.GenerationParam;
|
|
|
+import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
|
|
+import com.alibaba.dashscope.common.Message;
|
|
|
+import com.alibaba.dashscope.common.Role;
|
|
|
+import com.alibaba.dashscope.exception.ApiException;
|
|
|
+import com.alibaba.dashscope.exception.InputRequiredException;
|
|
|
+import com.alibaba.dashscope.exception.NoApiKeyException;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ *
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author taohongrun
|
|
|
+ * @since 2025/3/13
|
|
|
+ */
|
|
|
+public class test {
|
|
|
+ public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
|
|
|
+ Generation gen = new Generation();
|
|
|
+ Message userMsg = Message.builder()
|
|
|
+ .role(Role.USER.getValue())
|
|
|
+ .content("你是谁?")
|
|
|
+ .build();
|
|
|
+ GenerationParam param = GenerationParam.builder()
|
|
|
+
|
|
|
+ .apiKey("sk-375aa80dcb56402db68857b31846b1bb")
|
|
|
+ .model("deepseek-v3")
|
|
|
+ .messages(Arrays.asList(userMsg))
|
|
|
+ // 不可以设置为"text"
|
|
|
+ .resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
|
|
+ .build();
|
|
|
+ return gen.call(param);
|
|
|
+ }
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ GenerationResult result = callWithMessage();
|
|
|
+ System.out.println("思考过程:");
|
|
|
+ System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
|
|
|
+ System.out.println("回复内容:");
|
|
|
+ System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
|
|
|
+ } catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
|
|
+ // 使用日志框架记录异常信息
|
|
|
+ System.err.println("An error occurred while calling the generation service: " + e.getMessage());
|
|
|
+ }
|
|
|
+ System.exit(0);
|
|
|
+ }
|
|
|
+}
|