新增:SDK接入ai的demo
This commit is contained in:
parent
d4805a7c98
commit
5f993c453a
1
.gitignore
vendored
1
.gitignore
vendored
@ -31,3 +31,4 @@ build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
/src/main/resources/application-dev.yml
|
||||
|
12
pom.xml
12
pom.xml
@ -39,7 +39,7 @@
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>1.18.36</version>
|
||||
<version>1.18.38</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
@ -52,7 +52,7 @@
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.37</version>
|
||||
</dependency>
|
||||
<!-- 接口文档:knife4j -->
|
||||
<!-- 接口文档:knife4j:https://doc.xiaominfo.com/docs/quick-start -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
|
||||
@ -75,7 +75,12 @@
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-jsqlparser</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里云百炼:https://help.aliyun.com/zh/model-studio/install-sdk/#351a991edb6nm -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>dashscope-sdk-java</artifactId>
|
||||
<version>2.20.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
@ -100,6 +105,7 @@
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.38</version>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
|
@ -1,5 +1,7 @@
|
||||
package com.huangge1199.aiagent.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@ -12,9 +14,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/health")
|
||||
@Tag(name = "程序健康检测")
|
||||
public class HealthController {
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "简单返回")
|
||||
public String healthCheck() {
|
||||
return "OK";
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.huangge1199.aiagent.controller.invoke;
|
||||
|
||||
import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
||||
import com.alibaba.dashscope.exception.ApiException;
|
||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||
import com.alibaba.dashscope.utils.JsonUtils;
|
||||
import com.huangge1199.aiagent.common.R;
|
||||
import com.huangge1199.aiagent.demo.invoke.SdkAiInvoke;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* InvokeController
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/5/14 12:56:17
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/mbgk/data")
|
||||
@Tag(name = "AI大模型接入")
|
||||
public class InvokeController {
|
||||
|
||||
@Resource
|
||||
private SdkAiInvoke sdkAiInvoke;
|
||||
|
||||
@PostMapping("/sdk")
|
||||
@Operation(summary = "sdk接入")
|
||||
public R<String> sdkAiInvoke() {
|
||||
try {
|
||||
GenerationResult result = sdkAiInvoke.callWithMessage();
|
||||
return R.ok(JsonUtils.toJson(result));
|
||||
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
|
||||
return R.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.huangge1199.aiagent.demo.invoke;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.lang.System;
|
||||
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 com.alibaba.dashscope.utils.JsonUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* SdkAiInvoke
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/5/14 12:27:47
|
||||
*/
|
||||
@Component
|
||||
public class SdkAiInvoke {
|
||||
|
||||
@Value("${bailian.API-KEY}")
|
||||
private String baiLianKey;
|
||||
|
||||
public GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
|
||||
Generation gen = new Generation();
|
||||
Message systemMsg = Message.builder()
|
||||
.role(Role.SYSTEM.getValue())
|
||||
.content("You are a helpful assistant.")
|
||||
.build();
|
||||
Message userMsg = Message.builder()
|
||||
.role(Role.USER.getValue())
|
||||
.content("你是谁?")
|
||||
.build();
|
||||
GenerationParam param = GenerationParam.builder()
|
||||
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
|
||||
.apiKey(baiLianKey)
|
||||
// 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
||||
.model("qwen-plus")
|
||||
.messages(Arrays.asList(systemMsg, userMsg))
|
||||
.resultFormat(GenerationParam.ResultFormat.MESSAGE)
|
||||
.build();
|
||||
return gen.call(param);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@ spring:
|
||||
application:
|
||||
name: long-ai-agent
|
||||
profiles:
|
||||
active: local
|
||||
active: dev
|
||||
server:
|
||||
port: 8123
|
||||
servlet:
|
||||
|
Loading…
Reference in New Issue
Block a user