Commit ebd5cbfca291b645f8843314644c30cdc1b0a9c0

Authored by cherishsince
1 parent f4b3bb5a

基层 open ai 添加 chat 聊天

yudao-module-ai/yudao-module-ai-api/pom.xml
... ... @@ -17,4 +17,22 @@
17 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18 18 </properties>
19 19  
  20 + <dependencies>
  21 + <!-- yudao-common -->
  22 + <dependency>
  23 + <groupId>cn.iocoder.boot</groupId>
  24 + <artifactId>yudao-common</artifactId>
  25 + </dependency>
  26 + <!-- lombok -->
  27 + <dependency>
  28 + <groupId>org.projectlombok</groupId>
  29 + <artifactId>lombok</artifactId>
  30 + </dependency>
  31 + <!-- 参数校验 -->
  32 + <dependency>
  33 + <groupId>org.springframework.boot</groupId>
  34 + <artifactId>spring-boot-starter-validation</artifactId>
  35 + <optional>true</optional>
  36 + </dependency>
  37 + </dependencies>
20 38 </project>
21 39 \ No newline at end of file
... ...
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/AiModelEnum.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.enums;
  2 +
  3 +import lombok.Getter;
  4 +
  5 +/**
  6 + * author: fansili
  7 + * time: 2024/3/4 12:36
  8 + */
  9 +@Getter
  10 +public enum AiModelEnum {
  11 +
  12 + OPEN_AI_GPT_3_5("gpt-3.5-turbo", "GPT3.5"),
  13 + OPEN_AI_GPT_4("gpt-4-turbo", "GPT4")
  14 +
  15 + ;
  16 +
  17 + AiModelEnum(String value, String message) {
  18 + this.value = value;
  19 + this.message = message;
  20 + }
  21 +
  22 + private String value;
  23 +
  24 + private String message;
  25 +}
... ...
yudao-module-ai/yudao-module-ai-biz/pom.xml
... ... @@ -19,10 +19,38 @@
19 19  
20 20  
21 21 <dependencies>
  22 + <dependency>
  23 + <groupId>cn.iocoder.boot</groupId>
  24 + <artifactId>yudao-module-ai-api</artifactId>
  25 + <version>${revision}</version>
  26 + </dependency>
  27 + <dependency>
  28 + <groupId>cn.iocoder.boot</groupId>
  29 + <artifactId>yudao-common</artifactId>
  30 + </dependency>
  31 +
22 32 <!-- 增加 openai -->
23 33 <dependency>
24 34 <groupId>org.springframework.ai</groupId>
25 35 <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
26 36 </dependency>
  37 +
  38 + <dependency>
  39 + <groupId>cn.iocoder.boot</groupId>
  40 + <artifactId>yudao-spring-boot-starter-security</artifactId>
  41 + </dependency>
  42 +
27 43 </dependencies>
  44 + <build>
  45 + <plugins>
  46 + <plugin>
  47 + <groupId>org.apache.maven.plugins</groupId>
  48 + <artifactId>maven-compiler-plugin</artifactId>
  49 + <configuration>
  50 + <source>17</source>
  51 + <target>17</target>
  52 + </configuration>
  53 + </plugin>
  54 + </plugins>
  55 + </build>
28 56 </project>
29 57 \ No newline at end of file
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/ChatController.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.controller.admin;
  2 +
  3 +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
  4 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  5 +import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
  6 +import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO;
  7 +import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
  8 +import io.swagger.v3.oas.annotations.Operation;
  9 +import io.swagger.v3.oas.annotations.tags.Tag;
  10 +import org.springframework.ai.chat.ChatClient;
  11 +import org.springframework.ai.openai.OpenAiChatClient;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.context.ApplicationContext;
  14 +import org.springframework.validation.annotation.Validated;
  15 +import org.springframework.web.bind.annotation.PostMapping;
  16 +import org.springframework.web.bind.annotation.RequestBody;
  17 +import org.springframework.web.bind.annotation.RequestMapping;
  18 +import org.springframework.web.bind.annotation.RestController;
  19 +
  20 +/**
  21 + * AI模块
  22 + *
  23 + * author: fansili
  24 + * time: 2024/3/3 20:28
  25 + */
  26 +@Tag(name = "AI模块")
  27 +@RestController
  28 +@RequestMapping("/ai-api")
  29 +public class ChatController {
  30 +
  31 + @Autowired
  32 + private ApplicationContext applicationContext;
  33 +
  34 + @PostMapping("/chat")
  35 + @Operation(summary = "对话聊天", description = "简单的ai聊天")
  36 + public CommonResult chat(@RequestBody @Validated AiChatReqVO reqVO) {
  37 + ChatClient chatClient = getChatClient(reqVO.getAiModel());
  38 + String res;
  39 + try {
  40 + res = chatClient.call(reqVO.getInputText());
  41 + } catch (Exception e) {
  42 + res = e.getMessage();
  43 + }
  44 + return CommonResult.success(res);
  45 + }
  46 +
  47 + /**
  48 + * 根据 ai模型 获取对于的 模型实现类
  49 + *
  50 + * @param aiModelEnum
  51 + * @return
  52 + */
  53 + private ChatClient getChatClient(AiModelEnum aiModelEnum) {
  54 + if (AiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) {
  55 + return applicationContext.getBean(OpenAiChatClient.class);
  56 + }
  57 + // AI模型暂不支持
  58 + throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODULE_NOT_SUPPORTED);
  59 + }
  60 +}
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/vo/AiChatReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.controller.admin.vo;
  2 +
  3 +import cn.iocoder.yudao.module.ai.enums.AiModelEnum;
  4 +import io.swagger.v3.oas.annotations.media.Schema;
  5 +import jakarta.validation.constraints.NotNull;
  6 +import lombok.Data;
  7 +
  8 +/**
  9 + * ai 聊天 req
  10 + *
  11 + * author: fansili
  12 + * time: 2024/3/4 12:33
  13 + */
  14 +@Schema(description = "用户 App - 上传文件 Request VO")
  15 +@Data
  16 +public class AiChatReqVO {
  17 +
  18 + @Schema(description = "输入内容", requiredMode = Schema.RequiredMode.REQUIRED)
  19 + @NotNull(message = "输入内容不能为空")
  20 + private String inputText;
  21 +
  22 + @Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED)
  23 + @NotNull(message = "AI模型不能为空")
  24 + private AiModelEnum aiModel;
  25 +
  26 +}
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/package-info.java 0 → 100644
  1 +/**
  2 + * author: fansili
  3 + * time: 2024/3/4 13:08
  4 + */
  5 +package cn.iocoder.yudao.module.ai.controller;
0 6 \ No newline at end of file
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.properties 0 → 100644
  1 +# open ai
  2 +
  3 +# openAI https://openai.com/
  4 +spring.ai.openai.api-key=${OPEN_AI_KEY}
  5 +spring.ai.openai.chat.options.model=gpt-3.5-turbo
  6 +spring.ai.openai.chat.options.temperature=0.7
0 7 \ No newline at end of file
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/resources/application.yaml deleted