diff --git a/pom.xml b/pom.xml
index f208525..023b8cd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -81,8 +81,27 @@
dashscope-sdk-java
2.20.0
+
+
+ com.alibaba.cloud.ai
+ spring-ai-alibaba-starter
+ 1.0.0-M6.1
+
+
+
+
+ spring-milestones
+ Spring Milestones
+ https://repo.spring.io/milestone
+
+ false
+
+
+
+
+
diff --git a/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java b/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java
index e758c00..a6fa4ea 100644
--- a/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java
+++ b/src/main/java/com/huangge1199/aiagent/Service/InvokeService.java
@@ -1,5 +1,6 @@
package com.huangge1199.aiagent.Service;
+import cn.hutool.json.JSONObject;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
@@ -11,7 +12,9 @@ import com.alibaba.dashscope.exception.NoApiKeyException;
* @since 2025/5/14 13:27:08
*/
public interface InvokeService {
- GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException;
+ JSONObject callWithMessage(String question) throws NoApiKeyException, InputRequiredException;
- String getMsgByHttp(String question);
+ JSONObject getMsgByHttp(String question);
+
+ String getMsgBySpringAi(String question);
}
diff --git a/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java b/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java
index f2b910b..d759586 100644
--- a/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java
+++ b/src/main/java/com/huangge1199/aiagent/Service/impl/InvokeServiceImpl.java
@@ -5,14 +5,18 @@ import java.util.Arrays;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
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.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.huangge1199.aiagent.Service.InvokeService;
+import jakarta.annotation.Resource;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.model.ChatModel;
+import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -28,8 +32,11 @@ public class InvokeServiceImpl implements InvokeService {
@Value("${bailian.API-KEY}")
private String baiLianKey;
+ @Resource
+ private ChatModel dashscopeChatModel;
+
@Override
- public GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException {
+ public JSONObject callWithMessage(String question) throws NoApiKeyException, InputRequiredException {
Generation gen = new Generation();
Message systemMsg = Message.builder()
.role(Role.SYSTEM.getValue())
@@ -37,7 +44,7 @@ public class InvokeServiceImpl implements InvokeService {
.build();
Message userMsg = Message.builder()
.role(Role.USER.getValue())
- .content("你是谁?")
+ .content(question)
.build();
GenerationParam param = GenerationParam.builder()
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
@@ -47,11 +54,11 @@ public class InvokeServiceImpl implements InvokeService {
.messages(Arrays.asList(systemMsg, userMsg))
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
.build();
- return gen.call(param);
+ return JSONUtil.parseObj(gen.call(param));
}
@Override
- public String getMsgByHttp(String question) {
+ public JSONObject getMsgByHttp(String question) {
// API URL
String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
@@ -89,6 +96,14 @@ public class InvokeServiceImpl implements InvokeService {
.execute();
// 获取响应内容
- return response.body();
+ return JSONUtil.parseObj(response.body());
+ }
+
+ @Override
+ public String getMsgBySpringAi(String question) {
+ AssistantMessage output = dashscopeChatModel.call(new Prompt(question))
+ .getResult()
+ .getOutput();
+ return output.getText();
}
}
diff --git a/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java b/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java
index 9794a30..7ebbd96 100644
--- a/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java
+++ b/src/main/java/com/huangge1199/aiagent/controller/invoke/InvokeController.java
@@ -1,5 +1,6 @@
package com.huangge1199.aiagent.controller.invoke;
+import cn.hutool.json.JSONObject;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
@@ -31,10 +32,10 @@ public class InvokeController {
@PostMapping("/sdk")
@Operation(summary = "sdk接入")
- public R sdkAiInvoke() {
+ public R sdkAiInvoke(@RequestBody String question) {
try {
- GenerationResult result = invokeService.callWithMessage();
- return R.ok(JsonUtils.toJson(result));
+ JSONObject result = invokeService.callWithMessage(question);
+ return R.ok(result);
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
return R.fail(e.getMessage());
}
@@ -42,9 +43,20 @@ public class InvokeController {
@PostMapping("/http")
@Operation(summary = "http接入")
- public R httpAiInvoke(@RequestBody String question) {
+ public R httpAiInvoke(@RequestBody String question) {
try {
- String result = invokeService.getMsgByHttp(question);
+ JSONObject result = invokeService.getMsgByHttp(question);
+ return R.ok(result);
+ } catch (ApiException e) {
+ return R.fail(e.getMessage());
+ }
+ }
+
+ @PostMapping("/springAi")
+ @Operation(summary = "spring ai 接入")
+ public R springAiInvoke(@RequestBody String question) {
+ try {
+ String result = invokeService.getMsgBySpringAi(question);
return R.ok(result);
} catch (ApiException e) {
return R.fail(e.getMessage());