找回密码
 立即注册
查看: 7|回复: 0

6,SpringAI入门(对话机器人)

[复制链接]

191

主题

6

精华

195

金币

技术维护QQ:515138

积分
420
发表于 昨天 10:35 | 显示全部楼层 |阅读模式
1.jpg 2.jpg

依赖包:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.     <modelVersion>4.0.0</modelVersion>
  5.     <parent>
  6.         <groupId>org.springframework.boot</groupId>
  7.         <artifactId>spring-boot-starter-parent</artifactId>
  8.         <version>3.5.10</version>
  9.         <relativePath/> <!-- lookup parent from repository -->
  10.     </parent>
  11.     <groupId>com.jinhei</groupId>
  12.     <artifactId>jinhei-ai</artifactId>
  13.     <version>0.0.1-SNAPSHOT</version>
  14.     <name>jinhei-ai</name>
  15.     <description>jinhei-ai</description>
  16.     <url/>
  17.     <licenses>
  18.         <license/>
  19.     </licenses>
  20.     <developers>
  21.         <developer/>
  22.     </developers>
  23.     <scm>
  24.         <connection/>
  25.         <developerConnection/>
  26.         <tag/>
  27.         <url/>
  28.     </scm>
  29.     <properties>
  30.         <java.version>21</java.version>
  31.         <spring-ai.version>1.1.2</spring-ai.version>
  32.     </properties>
  33.     <dependencies>
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-starter-web</artifactId>
  37.         </dependency>
  38.         <dependency>
  39.             <groupId>org.springframework.ai</groupId>
  40.             <artifactId>spring-ai-starter-model-ollama</artifactId>
  41.         </dependency>
  42.         <dependency>
  43.             <groupId>com.mysql</groupId>
  44.             <artifactId>mysql-connector-j</artifactId>
  45.             <scope>runtime</scope>
  46.         </dependency>
  47.         <dependency>
  48.             <groupId>org.springframework.boot</groupId>
  49.             <artifactId>spring-boot-starter-test</artifactId>
  50.             <scope>test</scope>
  51.         </dependency>
  52.         <!--lombok-->
  53.         <dependency>
  54.             <groupId>org.projectlombok</groupId>
  55.             <artifactId>lombok</artifactId>
  56.             <version>1.18.30</version>
  57.         </dependency>
  58.     </dependencies>
  59.     <dependencyManagement>
  60.         <dependencies>
  61.             <dependency>
  62.                 <groupId>org.springframework.ai</groupId>
  63.                 <artifactId>spring-ai-bom</artifactId>
  64.                 <version>${spring-ai.version}</version>
  65.                 <type>pom</type>
  66.                 <scope>import</scope>
  67.             </dependency>
  68.         </dependencies>
  69.     </dependencyManagement>
  70.     <build>
  71.         <plugins>
  72.             <plugin>
  73.                 <groupId>org.springframework.boot</groupId>
  74.                 <artifactId>spring-boot-maven-plugin</artifactId>
  75.             </plugin>
  76.         </plugins>
  77.     </build>
  78. </project>
复制代码
注意:千万不要用start.spring.io提供的lombok,有bug!!
yml配置:
  1. spring:
  2.   application:
  3.     name: jinhei-ai
  4.   ai:
  5.     ollama:
  6.       base-url: http://localhost:11434
  7.       chat:
  8.         model: deepseek-r1:1.5b
复制代码
C层:
  1. package com.jinhei.ai.controller;
  2. import lombok.RequiredArgsConstructor;
  3. import org.springframework.ai.chat.client.ChatClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import reactor.core.publisher.Flux;
  7. @RequiredArgsConstructor
  8. @RestController
  9. @RequestMapping("/ai")
  10. public class ChatController {
  11.     private final ChatClient chatClient;
  12.     // 访问:http://localhost:8080/ai/chat?prompt=%E4%BD%A0%E6%98%AF%E8%B0%81
  13.     @RequestMapping("/chat")
  14.     public String chat(String prompt) {
  15.         return chatClient.prompt()
  16.                 .user(prompt)
  17.                 .call()
  18.                 .content();
  19.     }
  20.     // 流式 访问:http://localhost:8080/ai/chat1?prompt=%E4%BD%A0%E6%98%AF%E8%B0%81
  21.     @RequestMapping(value = "/chat1",produces = "text/html;charset=utf-8")
  22.     public Flux<String> chat1(String prompt) {
  23.         return chatClient.prompt()
  24.                 .user(prompt)
  25.                 .stream()
  26.                 .content();
  27.     }
  28. }
复制代码
// 注意看返回值,是Flux<String>,也就是流式结果,另外需要设定响应类型和编码,不然前端会乱码@RequestMapping(value = "/chat", produces = "text/html;charset=UTF-8")public Flux<String> chat(@RequestParam(defaultValue = "讲个笑话") String prompt) {    return chatClient            .prompt(prompt)            .stream() // 流式调用            .content();}



Config/配置:
  1. package com.jinhei.ai.config;
  2. import org.springframework.ai.ollama.OllamaChatModel;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.ai.chat.client.ChatClient;
  6. @Configuration
  7. public class CommonConfiguration {
  8.     @Bean
  9.     public ChatClient chatClient(OllamaChatModel model) {
  10.         return ChatClient
  11.                 .builder(model)
  12.                 .defaultSystem("你是由深圳市金黑网络技术公司开发的智能AI助手,名字叫做金黑,请以金黑身份和语气回答用户问题")
  13.                 .build();
  14.     }
  15. }
复制代码
jinhei-ai.zip (27.3 KB, 下载次数: 0, 售价: 50 金币)





上一篇:5,传统应用和大模型应用
下一篇:7,AI大模型日志功能
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-2-16 07:10

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表