找回密码
 立即注册
首页 业界区 安全 【原】无脑操作:IDEA + maven + SpringAI + 讯飞星火大 ...

【原】无脑操作:IDEA + maven + SpringAI + 讯飞星火大模型实现简单智能对话

时思美 2025-10-24 10:35:01
1、实现效果
1.png

 
2、设置pom.xml
  1. 1 <?xml version="1.0" encoding="UTF-8"?>
  2. 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. 4     <modelVersion>4.0.0</modelVersion>
  5. 5     <parent>
  6. 6         <groupId>org.springframework.boot</groupId>
  7. 7         spring-boot-starter-parent</artifactId>
  8. 8         <version>3.4.4</version>
  9. 9         <relativePath/>
  10. 10     </parent>
  11. 11     <groupId>cn.temptation</groupId>
  12. 12     studyAI</artifactId>
  13. 13     <version>0.0.1-SNAPSHOT</version>
  14. 14     <name>studyAI</name>
  15. 15     <description>studyAI</description>
  16. 16     <url/>
  17. 17
  18. 18     <properties>
  19. 19         <java.version>17</java.version>
  20. 20         <spring-ai.version>1.0.3</spring-ai.version>
  21. 21     </properties>
  22. 22
  23. 23     <dependencyManagement>
  24. 24         <dependencies>
  25. 25             <dependency>
  26. 26                 <groupId>org.springframework.ai</groupId>
  27. 27                 spring-ai-bom</artifactId>
  28. 28                 <version>1.0.3</version>
  29. 29                 <type>pom</type>
  30. 30                 <scope>import</scope>
  31. 31             </dependency>
  32. 32         </dependencies>
  33. 33     </dependencyManagement>
  34. 34
  35. 35     <dependencies>
  36. 36         
  37. 37         <dependency>
  38. 38             <groupId>org.projectlombok</groupId>
  39. 39             lombok</artifactId>
  40. 40             <version>1.18.22</version>
  41. 41         </dependency>
  42. 42         
  43. 43         <dependency>
  44. 44             <groupId>org.springframework.boot</groupId>
  45. 45             spring-boot-starter-web</artifactId>
  46. 46         </dependency>
  47. 47         
  48. 48         <dependency>
  49. 49             <groupId>org.springframework.boot</groupId>
  50. 50             spring-boot-starter-thymeleaf</artifactId>
  51. 51         </dependency>
  52. 52         
  53. 53         <dependency>
  54. 54             <groupId>io.github.briqt</groupId>
  55. 55             xunfei-spark4j</artifactId>
  56. 56             <version>1.3.0</version>
  57. 57         </dependency>
  58. 58         
  59. 59         <dependency>
  60. 60             <groupId>org.springframework.boot</groupId>
  61. 61             spring-boot-devtools</artifactId>
  62. 62             <optional>true</optional>
  63. 63         </dependency>
  64. 64     </dependencies>
  65. 65
  66. 66     <build>
  67. 67         <plugins>
  68. 68             <plugin>
  69. 69                 <groupId>org.springframework.boot</groupId>
  70. 70                 spring-boot-maven-plugin</artifactId>
  71. 71             </plugin>
  72. 72         </plugins>
  73. 73     </build>
  74. 74
  75. 75 </project>
复制代码
 
3、设置application.yaml
2.png

3.png
  1. 1 spring:
  2. 2   application:
  3. 3     name: iflytek-ai
  4. 4   thymeleaf:
  5. 5     cache: false
  6. 6
  7. 7 xunfei:
  8. 8   client:
  9. 9     appId: 从讯飞开放平台上找到自己的应用的APPID
  10. 10     apiSecret: 从讯飞开放平台上找到自己的应用的APISecret
  11. 11     apiKey: 从讯飞开放平台上找到自己的应用的APIKey
  12. 12
  13. 13 server:
  14. 14   port: 80
复制代码
 
4、整个程序结构如下图所示:
4.png

 
5、编写SparkConfig.java配置类
  1. 1 package cn.temptation.config;
  2. 2
  3. 3 import io.github.briqt.spark4j.SparkClient;
  4. 4 import lombok.Data;
  5. 5 import org.springframework.boot.context.properties.ConfigurationProperties;
  6. 6 import org.springframework.context.annotation.Bean;
  7. 7 import org.springframework.context.annotation.Configuration;
  8. 8
  9. 9 @Configuration
  10. 10 @ConfigurationProperties(prefix = "xunfei.client")
  11. 11 @Data
  12. 12 public class SparkConfig {
  13. 13     private String appid;
  14. 14     private String apiSecret;
  15. 15     private String apiKey;
  16. 16
  17. 17     @Bean
  18. 18     public SparkClient sparkClient() {
  19. 19         SparkClient sparkClient = new SparkClient();
  20. 20         sparkClient.appid = this.appid;
  21. 21         sparkClient.apiSecret = this.apiSecret;
  22. 22         sparkClient.apiKey = this.apiKey;
  23. 23
  24. 24         return sparkClient;
  25. 25     }
  26. 26 }
复制代码
 
6、使用xunfei-spark4j时,想使用免费的Spark Lite时,有一个坑,因为SparkApiVersion枚举的结构如下:
  1. 1 public enum SparkApiVersion {
  2. 2     V1_5("v1.1", "https://spark-api.xf-yun.com/v1.1/chat", "general"),
  3. 3     V2_0("v2.1", "https://spark-api.xf-yun.com/v2.1/chat", "generalv2"),
  4. 4     V3_0("v3.1", "https://spark-api.xf-yun.com/v3.1/chat", "generalv3"),
  5. 5     V3_5("v3.5", "https://spark-api.xf-yun.com/v3.5/chat", "generalv3.5"),
  6. 6     V4_0("v4.0", "https://spark-api.xf-yun.com/v4.0/chat", "4.0Ultra");
  7. 7
  8. 8     private final String version;
  9. 9     private final String url;
  10. 10     private final String domain;
  11. 11
  12. 12     private SparkApiVersion(String version, String url, String domain) {
  13. 13         this.version = version;
  14. 14         this.url = url;
  15. 15         this.domain = domain;
  16. 16     }
  17. 17
  18. 18     public String getVersion() {
  19. 19         return this.version;
  20. 20     }
  21. 21
  22. 22     public String getUrl() {
  23. 23         return this.url;
  24. 24     }
  25. 25
  26. 26     public String getDomain() {
  27. 27         return this.domain;
  28. 28     }
  29. 29 }
复制代码
这里找不到免费的Spark Lite,所以需要自己修改。编写EnumReflectionUtil.java反射工具类。
  1. 1 public class EnumReflectionUtil {
  2. 2     public static void setEnumField(Enum<?> enumConstant, String fieldName, Object newValue) throws Exception {
  3. 3         Field field = enumConstant.getClass().getDeclaredField(fieldName);
  4. 4         field.setAccessible(true);
  5. 5         field.set(enumConstant, newValue);
  6. 6     }
  7. 7 }
复制代码
 
7、编写聊天处理类ChatWithController.java
  1. 1 @Controller
  2. 2 public class ChatWithController {
  3. 3     // 初始化客户端
  4. 4     @Resource
  5. 5     private SparkClient sparkClient;
  6. 6
  7. 7     // 通过反射类,在使用大模型时,指定使用免费的Spark Lite大模型
  8. 8     @PostConstruct
  9. 9     public void init() throws Exception {
  10. 10         // 修改 V1_5 的版本信息
  11. 11         EnumReflectionUtil.setEnumField(SparkApiVersion.V1_5, "version", "v1.1");
  12. 12         EnumReflectionUtil.setEnumField(SparkApiVersion.V1_5, "url", "https://spark-api.xf-yun.com/v1.1/chat");
  13. 13         EnumReflectionUtil.setEnumField(SparkApiVersion.V1_5, "domain", "lite");
  14. 14     }
  15. 15
  16. 16     // AI预设System角色的条件
  17. 17     public static final String PRECONDITION = "你是 iflytek";
  18. 18
  19. 19     // 跳转前端页面
  20. 20     @RequestMapping("/")
  21. 21     public String index() {
  22. 22         return "index";
  23. 23     }
  24. 24
  25. 25     // 和讯飞星火大模型Spark Lite对话
  26. 26     @RequestMapping(value = "/chat", produces = "application/json")
  27. 27     public ResponseEntity<?> sendHttpToSpark(@RequestBody Map<String, String> map) {
  28. 28         // 消息列表
  29. 29         List<SparkMessage> messages = new ArrayList<>();
  30. 30         // 设置System角色,则使用下句
  31. 31 //        messages.add(SparkMessage.systemContent(PRECONDITION));
  32. 32         // 获取前端输入的对话内容,设置User角色
  33. 33         messages.add(SparkMessage.userContent(map.get("message")));
  34. 34
  35. 35         // 构造请求
  36. 36         SparkRequest sparkRequest = SparkRequest.builder()
  37. 37                 .messages(messages)
  38. 38                 .apiVersion(SparkApiVersion.V1_5)
  39. 39                 .build();
  40. 40
  41. 41         SparkSyncChatResponse chatResponse = sparkClient.chatSync(sparkRequest);
  42. 42
  43. 43         String responseContent = chatResponse.getContent();
  44. 44
  45. 45         Map<String, Object> response = new HashMap<>();
  46. 46         response.put("response", responseContent);
  47. 47
  48. 48         return ResponseEntity.ok(response);
  49. 49     }
  50. 50 }
复制代码
 
8、编写前端交互页面index.html
  1.   1 <!DOCTYPE html>
  2.   2 <html lang="zh-CN">
  3.   3 <head>
  4.   4     <meta charset="UTF-8">
  5.   5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.   6     <title>AI 智能助手</title>
  7.   7     <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
  8.   8     
  9. 145 </head>
  10. 146 <body>
  11. 147
  12. 148     <h1 style="margin-bottom: 20px; color: #1e293b;">AI 智能助手 <i class="fas fa-robot"></i></h1>
  13. 149
  14. 150     
  15. 151         
  16. 152            
  17. 153                 <i class="fas fa-robot"></i>
  18. 154                 您好!我是智能助手,可以回答各种问题...
  19. 155            
  20. 156         
  21. 157
  22. 158         
  23. 159             <input type="text" id="user-input" placeholder="输入消息...">
  24. 160             <button onclick="sendMessage()" id="send-btn">
  25. 161                 <i class="fas fa-paper-plane"></i> 发送
  26. 162             </button>
  27. 163         
  28. 164     
  29. 165
  30. 166
  31. 167
  32. 233 </body>
  33. 234 </html>
复制代码
 
本文编写过程中参考了网络上一些大佬的经验,在此一并感谢。
 抛砖引玉,期待更多人研究讯飞星火大模型...

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

2025-10-26 00:45:55

举报

感谢发布原创作品,程序园因你更精彩
您需要登录后才可以回帖 登录 | 立即注册