基层 open ai 添加 chat 聊天
This commit is contained in:
parent
f4b3bb5aaa
commit
ebd5cbfca2
|
@ -17,4 +17,22 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- yudao-common -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
<!-- lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<!-- 参数校验 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
package cn.iocoder.yudao.module.ai.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* author: fansili
|
||||
* time: 2024/3/4 12:36
|
||||
*/
|
||||
@Getter
|
||||
public enum AiModelEnum {
|
||||
|
||||
OPEN_AI_GPT_3_5("gpt-3.5-turbo", "GPT3.5"),
|
||||
OPEN_AI_GPT_4("gpt-4-turbo", "GPT4")
|
||||
|
||||
;
|
||||
|
||||
AiModelEnum(String value, String message) {
|
||||
this.value = value;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
private String value;
|
||||
|
||||
private String message;
|
||||
}
|
|
@ -19,10 +19,38 @@
|
|||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-module-ai-api</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 增加 openai -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
<artifactId>yudao-spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,60 @@
|
|||
package cn.iocoder.yudao.module.ai.controller.admin;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO;
|
||||
import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.ai.chat.ChatClient;
|
||||
import org.springframework.ai.openai.OpenAiChatClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* AI模块
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/3/3 20:28
|
||||
*/
|
||||
@Tag(name = "AI模块")
|
||||
@RestController
|
||||
@RequestMapping("/ai-api")
|
||||
public class ChatController {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@PostMapping("/chat")
|
||||
@Operation(summary = "对话聊天", description = "简单的ai聊天")
|
||||
public CommonResult chat(@RequestBody @Validated AiChatReqVO reqVO) {
|
||||
ChatClient chatClient = getChatClient(reqVO.getAiModel());
|
||||
String res;
|
||||
try {
|
||||
res = chatClient.call(reqVO.getInputText());
|
||||
} catch (Exception e) {
|
||||
res = e.getMessage();
|
||||
}
|
||||
return CommonResult.success(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 ai模型 获取对于的 模型实现类
|
||||
*
|
||||
* @param aiModelEnum
|
||||
* @return
|
||||
*/
|
||||
private ChatClient getChatClient(AiModelEnum aiModelEnum) {
|
||||
if (AiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) {
|
||||
return applicationContext.getBean(OpenAiChatClient.class);
|
||||
}
|
||||
// AI模型暂不支持
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODULE_NOT_SUPPORTED);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cn.iocoder.yudao.module.ai.controller.admin.vo;
|
||||
|
||||
import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ai 聊天 req
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/3/4 12:33
|
||||
*/
|
||||
@Schema(description = "用户 App - 上传文件 Request VO")
|
||||
@Data
|
||||
public class AiChatReqVO {
|
||||
|
||||
@Schema(description = "输入内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "输入内容不能为空")
|
||||
private String inputText;
|
||||
|
||||
@Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "AI模型不能为空")
|
||||
private AiModelEnum aiModel;
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
/**
|
||||
* author: fansili
|
||||
* time: 2024/3/4 13:08
|
||||
*/
|
||||
package cn.iocoder.yudao.module.ai.controller;
|
|
@ -0,0 +1,6 @@
|
|||
# open ai
|
||||
|
||||
# openAI https://openai.com/
|
||||
spring.ai.openai.api-key=${OPEN_AI_KEY}
|
||||
spring.ai.openai.chat.options.model=gpt-3.5-turbo
|
||||
spring.ai.openai.chat.options.temperature=0.7
|
Loading…
Reference in New Issue