找回密码
 立即注册
首页 业界区 业界 基于SpringAI构建大模型应用

基于SpringAI构建大模型应用

臧莞然 5 天前
1. 背景

在这里,我主要分享的是在应用层面大模型相关的技术,假如你已有一个现成的大模型接口,无论是符合OpenAI规范的,还是各家公司一些自己的接口,例如Gemini,Deepseek,通义千问,问心一言等,让用这些大模型来构建一些应用,可以选取下面的方案:

  • 使用低代码大模型应用搭建平台,例如Coze,Dify, FastGPT等,这些平台带了流程编排,知识库,也很方便的和各种大模型对接,向量模型、向量库,有些还有了监控界面或者插件市场等,能满足我们的大部分需求
  • 使用编程的方式来构建应用,这种可以使用公司现有的技术栈,提供更为灵活的使用,接入现有的系统等,或者从更高层面来说,定制自己的大模型应用规范,定制大模型应用构建平台,接入平台等;也可以把上面所说的低代码平台看作为自建大模型应用体系的一部分,即可以通过代码的方式灵活去构建应用,也通过平台更高效的去构建应用
我们后面讲的主要是使用第二种方式,我们选取一些现有的框架来实现,Python主要Langchain相关技术,Java也有一个对应的框架Langchain4j,也有SpringAI
这些框架帮我们做了很多事情:

  • 封装一个通用的模型调用接口,屏蔽了底层不同公司大模型接口的差异
  • 管理了会话和上下文,会话就是将用户之前问的问题和现在问的问题关联起来
  • 结构化输出,将大模型的文本输出转为程序可以使用的结构化对象,例如JSON对象
  • 工具/函数调用
  • 可观测性
  • 模型效果评估
一个框架LiteLLM专门把不同大模型接口适配为OpenAI格式的,这也是一种屏蔽差异的方式
2. 架构

1.jpeg
.png)
整体架构如上,下面主要介绍部分实现

  • 模型框架层,SpringAI,Java大模型应用开发框架
  • 模型推理能力,Ollama,利用本地CPU或GPU,和现有训练模型,实现模型推理能力,便于开发,生产环境需要替换
  • 监控追踪,LangFuse,追踪大模型应用请求,例如输入输出,耗时、Token消耗等
3. 实现

3.1 安装

Ollama和LangFuse参照文档安装和启动
SpringAI通过pom导入,除此之外,还导入了springboot ollama、opentelemetry等相关包,后者是为了接入LangFuse
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.jd.jt</groupId>
  5.     ai-base</artifactId>
  6.     <version>1.0-SNAPSHOT</version>
  7.     <name>Archetype - ai-base</name>
  8.     <url>http://maven.apache.org</url>
  9.     <parent>
  10.         <groupId>org.springframework.boot</groupId>
  11.         spring-boot-starter-parent</artifactId>
  12.         <version>3.4.3</version>
  13.         <relativePath/>
  14.     </parent>
  15.     <properties>
  16.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17.         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  18.         <maven.compiler.source>21</maven.compiler.source>
  19.         <maven.compiler.target>21</maven.compiler.target>
  20.     </properties>
  21.     <dependencyManagement>
  22.         <dependencies>
  23.             <dependency>
  24.                 <groupId>io.opentelemetry.instrumentation</groupId>
  25.                 opentelemetry-instrumentation-bom</artifactId>
  26.                 <version>2.17.0</version>
  27.                 <type>pom</type>
  28.                 <scope>import</scope>
  29.             </dependency>
  30.             <dependency>
  31.                 <groupId>org.springframework.ai</groupId>
  32.                 spring-ai-bom</artifactId>
  33.                 <version>1.0.1</version>
  34.                 <type>pom</type>
  35.                 <scope>import</scope>
  36.             </dependency>
  37.         </dependencies>
  38.     </dependencyManagement>
  39.     <dependencies>
  40.         <dependency>
  41.             <groupId>org.springframework.boot</groupId>
  42.             spring-boot-starter</artifactId>
  43.         </dependency>
  44.         <dependency>
  45.             <groupId>org.springframework.ai</groupId>
  46.             spring-ai-starter-model-ollama</artifactId>
  47.         </dependency>
  48.         
  49.         <dependency>
  50.             <groupId>org.springframework.boot</groupId>
  51.             spring-boot-starter-web</artifactId>
  52.         </dependency>
  53.         <dependency>
  54.             <groupId>io.opentelemetry.instrumentation</groupId>
  55.             opentelemetry-spring-boot-starter</artifactId>
  56.         </dependency>
  57.         
  58.         <dependency>
  59.             <groupId>org.springframework.boot</groupId>
  60.             spring-boot-starter-actuator</artifactId>
  61.         </dependency>
  62.         
  63.         <dependency>
  64.             <groupId>io.micrometer</groupId>
  65.             micrometer-tracing-bridge-otel</artifactId>
  66.         </dependency>
  67.         
  68.         <dependency>
  69.             <groupId>io.opentelemetry</groupId>
  70.             opentelemetry-exporter-otlp</artifactId>
  71.         </dependency>
  72.         <dependency>
  73.             <groupId>org.projectlombok</groupId>
  74.             lombok</artifactId>
  75.             <version>1.18.38</version>
  76.         </dependency>
  77.     </dependencies>
  78. </project>
复制代码
3.2 起步示例
  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.ai.chat.client.ChatClient;
  3. import org.springframework.http.MediaType;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import reactor.core.publisher.Flux;
  8. @RestController
  9. @Slf4j
  10. class MyController {
  11.     private final ChatClient chatClient;
  12.     public MyController(ChatClient.Builder chatClientBuilder) {
  13.         this.chatClient = chatClientBuilder.build();
  14.     }
  15.     @GetMapping(value = "/ai-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  16.     Flux<String> generationStream(@RequestParam("userInput") String userInput) {
  17.         return this.chatClient.prompt()
  18.                 .user(userInput)
  19.                 .stream()
  20.                 .content();
  21.     }
  22.     @GetMapping("/ai")
  23.     String generation(String userInput) {
  24.         return this.chatClient.prompt()
  25.                 .user(userInput)
  26.                 .call()
  27.                 .content();
  28.     }
复制代码
application.properties
  1. spring.ai.ollama.chat.enabled=true
  2. spring.ai.model.chat=ollama
  3. spring.ai.ollama.chat.options.model=qwen3:8b
  4. spring.ai.chat.observations.include-prompt=true
  5. spring.ai.chat.observations.include-completion=true
  6. management.tracing.sampling.probability=1.0
复制代码
执行命令
[code]curl --location 'http://localhost:8080/ai?userInput=%E4%BD%A0%E5%A5%BD'嗯,用户发来“你好”,我需要以自然的方式回应。首先,应该用中文回复,保持友好和亲切的语气。可以简单问候,比如“你好!有什么我可以帮助你的吗?”这样既回应了对方的问候,又主动询问是否需要帮助,符合我的设计原则。同时,要避免使用复杂或生硬的表达,让对话显得轻松。另外,考虑到用户可能有各种需求,保持开放式的提问可以引导他们进一步说明具体问题,这样我才能更好地提供帮助。确保回复简洁明了,符合日常交流的习惯。你好!有什么我可以帮助你的吗?
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册