新值:AI大模型接入-Spring AI接入
修改:AI大模型接入-http和sdk测试接口返回类型改为JSONObject,便于查看
This commit is contained in:
parent
3a3c4fc51c
commit
5b2d1988da
19
pom.xml
19
pom.xml
@ -81,8 +81,27 @@
|
|||||||
<artifactId>dashscope-sdk-java</artifactId>
|
<artifactId>dashscope-sdk-java</artifactId>
|
||||||
<version>2.20.0</version>
|
<version>2.20.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- Spring AI Alibaba 接入 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud.ai</groupId>
|
||||||
|
<artifactId>spring-ai-alibaba-starter</artifactId>
|
||||||
|
<version>1.0.0-M6.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spring-milestones</id>
|
||||||
|
<name>Spring Milestones</name>
|
||||||
|
<url>https://repo.spring.io/milestone</url>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.huangge1199.aiagent.Service;
|
package com.huangge1199.aiagent.Service;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
||||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||||
@ -11,7 +12,9 @@ import com.alibaba.dashscope.exception.NoApiKeyException;
|
|||||||
* @since 2025/5/14 13:27:08
|
* @since 2025/5/14 13:27:08
|
||||||
*/
|
*/
|
||||||
public interface InvokeService {
|
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);
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,18 @@ import java.util.Arrays;
|
|||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpResponse;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.alibaba.dashscope.aigc.generation.Generation;
|
import com.alibaba.dashscope.aigc.generation.Generation;
|
||||||
import com.alibaba.dashscope.aigc.generation.GenerationParam;
|
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.Message;
|
||||||
import com.alibaba.dashscope.common.Role;
|
import com.alibaba.dashscope.common.Role;
|
||||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||||
import com.huangge1199.aiagent.Service.InvokeService;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@ -28,8 +32,11 @@ public class InvokeServiceImpl implements InvokeService {
|
|||||||
@Value("${bailian.API-KEY}")
|
@Value("${bailian.API-KEY}")
|
||||||
private String baiLianKey;
|
private String baiLianKey;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatModel dashscopeChatModel;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GenerationResult callWithMessage() throws NoApiKeyException, InputRequiredException {
|
public JSONObject callWithMessage(String question) throws NoApiKeyException, InputRequiredException {
|
||||||
Generation gen = new Generation();
|
Generation gen = new Generation();
|
||||||
Message systemMsg = Message.builder()
|
Message systemMsg = Message.builder()
|
||||||
.role(Role.SYSTEM.getValue())
|
.role(Role.SYSTEM.getValue())
|
||||||
@ -37,7 +44,7 @@ public class InvokeServiceImpl implements InvokeService {
|
|||||||
.build();
|
.build();
|
||||||
Message userMsg = Message.builder()
|
Message userMsg = Message.builder()
|
||||||
.role(Role.USER.getValue())
|
.role(Role.USER.getValue())
|
||||||
.content("你是谁?")
|
.content(question)
|
||||||
.build();
|
.build();
|
||||||
GenerationParam param = GenerationParam.builder()
|
GenerationParam param = GenerationParam.builder()
|
||||||
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
|
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
|
||||||
@ -47,11 +54,11 @@ public class InvokeServiceImpl implements InvokeService {
|
|||||||
.messages(Arrays.asList(systemMsg, userMsg))
|
.messages(Arrays.asList(systemMsg, userMsg))
|
||||||
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
||||||
.build();
|
.build();
|
||||||
return gen.call(param);
|
return JSONUtil.parseObj(gen.call(param));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMsgByHttp(String question) {
|
public JSONObject getMsgByHttp(String question) {
|
||||||
// API URL
|
// API URL
|
||||||
String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
|
String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";
|
||||||
|
|
||||||
@ -89,6 +96,14 @@ public class InvokeServiceImpl implements InvokeService {
|
|||||||
.execute();
|
.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.huangge1199.aiagent.controller.invoke;
|
package com.huangge1199.aiagent.controller.invoke;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
||||||
import com.alibaba.dashscope.exception.ApiException;
|
import com.alibaba.dashscope.exception.ApiException;
|
||||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||||
@ -31,10 +32,10 @@ public class InvokeController {
|
|||||||
|
|
||||||
@PostMapping("/sdk")
|
@PostMapping("/sdk")
|
||||||
@Operation(summary = "sdk接入")
|
@Operation(summary = "sdk接入")
|
||||||
public R<String> sdkAiInvoke() {
|
public R<JSONObject> sdkAiInvoke(@RequestBody String question) {
|
||||||
try {
|
try {
|
||||||
GenerationResult result = invokeService.callWithMessage();
|
JSONObject result = invokeService.callWithMessage(question);
|
||||||
return R.ok(JsonUtils.toJson(result));
|
return R.ok(result);
|
||||||
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
||||||
return R.fail(e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
@ -42,9 +43,20 @@ public class InvokeController {
|
|||||||
|
|
||||||
@PostMapping("/http")
|
@PostMapping("/http")
|
||||||
@Operation(summary = "http接入")
|
@Operation(summary = "http接入")
|
||||||
public R<String> httpAiInvoke(@RequestBody String question) {
|
public R<JSONObject> httpAiInvoke(@RequestBody String question) {
|
||||||
try {
|
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<String> springAiInvoke(@RequestBody String question) {
|
||||||
|
try {
|
||||||
|
String result = invokeService.getMsgBySpringAi(question);
|
||||||
return R.ok(result);
|
return R.ok(result);
|
||||||
} catch (ApiException e) {
|
} catch (ApiException e) {
|
||||||
return R.fail(e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
|
Loading…
Reference in New Issue
Block a user