目录

SpringAI实践(一)

最近看了很多大模型的文章,感觉AI可以能做很多事情,例如RAG(文档检索生成)、Function Calling(模型回调)、MCP(模型上下文协议)、AI链式工作流等,接下来会都动手尝试一下这些功能,探索运用大模型可以拓展哪些能力。实现这些功能需要探索的很多,这里先进行第一步使用SpringAI调用大模型实现对话。

一、引入Spring AI

新建一个Spring Boot的工程,在工程中引入Spring AI的依赖,Spring AI支持Ollma、类OpenAI的接口,这两个引入的pom不一样,这里示例中是使用的硅基流动的模型

xml 复制代码
<!-- Spring AI -->  
<properties>
	<spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>

<dependency>  
    <groupId>org.springframework.ai</groupId>  
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>  
    <version>${spring-ai.version}</version>  
</dependency>

这里为了展示调用结果,同时引入Spring Boot Web相关依赖,使用restful方式调用

xml 复制代码
<!-- Spring Boot -->  
<properties>
	<spring-boot.version>3.4.0</spring-boot.version>
</properties>

<dependency>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-web</artifactId>  
    <version>${spring-boot.version}</version>  
</dependency>

二、配置模型接口信息

这里使用的是硅基流动中Qwen/Qwen2.5-7B-Instruct模型,也可以其他的对话模型

yml 复制代码
server:  
  port: 9000  
  
spring:  
  ai:  
    openai:  
      api-key: ${密钥}  
      base-url: https://api.siliconflow.cn/  
      chat:  
        options:  
          model: Qwen/Qwen2.5-7B-Instruct  
        completions-path: /v1/chat/completions

三、使用Spring AI调用模型

编写控制器 OpenAiChatClientController 实现调用逻辑,可以设置默认的系统Prompt,定义模型的参数

java 复制代码
package com.renne.ai.learn.chat.controller;  
  
import jakarta.servlet.http.HttpServletResponse;  
import org.springframework.ai.chat.client.ChatClient;  
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;  
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;  
import org.springframework.ai.chat.memory.InMemoryChatMemory;  
import org.springframework.ai.openai.OpenAiChatOptions;  
import org.springframework.http.MediaType;  
import org.springframework.http.codec.ServerSentEvent;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestParam;  
import org.springframework.web.bind.annotation.RestController;  
import reactor.core.publisher.Flux;  
  
  
@RestController  
@RequestMapping("/openai/chat")  
public class OpenAiChatClientController {  
  
    private final ChatClient openAiChatClient;  
  
    private static final String DEFAULT_PROMPT = "你是一个聊天助手,请根据用户提问回答!";  
  
     public OpenAiChatClientController(ChatClient.Builder chatClientBuilder) {  
       this.openAiChatClient = chatClientBuilder  
             .defaultSystem(DEFAULT_PROMPT)  
              // 实现 Chat Memory 的 Advisor              // 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。  
              .defaultAdvisors(  
                    new MessageChatMemoryAdvisor(new InMemoryChatMemory())  
              )  
              // 实现 Logger 的 Advisor              
              .defaultAdvisors(  
                    new SimpleLoggerAdvisor()  
              )  
              // 设置 ChatClient 中 ChatModel 的 Options 参数  
              .defaultOptions(  
                    OpenAiChatOptions.builder()  
                          .topP(0.7)  
                          .build()  
              )  
             .build();  
     }  
  
    /**  
     * ChatClient 简单调用  
     */  
    @GetMapping("/simple/chat")  
    public String simpleChat(@RequestParam String message) {  
       return openAiChatClient  
             .prompt(message).call().content();  
    }  
    
	/**  
	 * ChatClient 简单调用  
	 * 默认使用 InMemoryChatMemory  
	 ** @param message 消息  
	 * @param chatId  会话ID  
	 */@GetMapping("/simple/chat")  
	public String simpleChat(@RequestParam String message, @RequestParam String chatId) {  
	    return openAiChatClient  
	          .prompt(message)  
	              .advisors(a -> a  
	                       .param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId) // 设置聊天会话ID  
	                       .param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 100)) // 设置聊天记录检索数量  
	          .call().content();  
	}
  
    /**  
     * ChatClient 流式调用  
     */  
    @GetMapping("/stream/chat")  
    public Flux<String> streamChat(@RequestParam String message,  
                            HttpServletResponse response) {  
  
       response.setCharacterEncoding("UTF-8");  
       return openAiChatClient.prompt(message).stream().content();  
    }  
  
    /**  
     * ChatClient 流式响应  
     */  
    @GetMapping(value = "/stream/response", produces = MediaType.TEXT_EVENT_STREAM_VALUE)  
    public Flux<ServerSentEvent<String>> streamChat(@RequestParam String message) {  
       return openAiChatClient.prompt()  
             .user(message)  
             .stream()  
             .content()  
             .map(content -> ServerSentEvent.<String>builder()  
                   .data(content)  
                   .build());  
    }  
  
}

使用InMemoryChatMemory聊天记录的存储,可以让模型记住对话记录,结合上下文去回答,chatId就是会话窗口的id,在这个id不变的情况下,它会自定义的去在总结100条对话记录,然后再回答你的问题

调用streamChat接口可以得到模型的返回值

这里实现了使用SpringAI调用大模型的类OpenAI接口实现对话。

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
win4r1 天前
🚀颠覆MCP!Open WebUI新技术mcpo横空出世!支持ollama!轻松支持各种MCP Server!Cline+Claude3.7轻松开发论文检索
openai·mcp·cline
AskHarries2 天前
dify开启多租户模式
openai
骑猪兜风2332 天前
没有人知道“他妈的” 智能体到底是什么
人工智能·openai·ai编程
拉丁解牛说技术2 天前
AI大模型进阶系列(01)AI大模型的主流技术 | AI对普通人的本质影响是什么?
后端·架构·openai
x-cmd2 天前
[250401] OpenAI 向免费用户开放 GPT-4o 图像生成功能 | Neovim 0.11 新特性解读
人工智能·gpt·文生图·openai·命令行·neovim
鸿蒙布道师3 天前
OpenAI战略转向:开源推理模型背后的行业博弈与技术趋势
人工智能·深度学习·神经网络·opencv·自然语言处理·openai·deepseek
紫电青霜_FullStack3 天前
浅谈用Azure AI Search实现RAG (1)---基本概念
openai
ssshooter3 天前
2025 最新 AI 模型深度对比:ChatGPT、Claude、Gemini到底选谁?
程序员·aigc·openai
新智元3 天前
美国 CS 专业卷上天,满分学霸惨遭藤校全拒!父亲大受震撼引爆热议
人工智能·openai
新智元3 天前
美国奥数题撕碎 AI 数学神话,顶级模型现场翻车!最高得分 5%,DeepSeek 唯一逆袭
人工智能·openai