35互联网站建设怎么样,wordpress 分类目录导航,新乡电商网站建设,东营兴通建设工程公司网站目录 介绍核心功能ChatClientChatModel提示词总结 介绍 Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则 (如可移植性和模块化设计) 应用于 AI 领域#xff0c;并促进使用 POJO 作为应用程序的构建块到 AI 领域。
核心功能 1. 统一的 API 抽象 Spring A… 目录 介绍核心功能ChatClientChatModel提示词总结 介绍 Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则 (如可移植性和模块化设计) 应用于 AI 领域并促进使用 POJO 作为应用程序的构建块到 AI 领域。
核心功能 1. 统一的 API 抽象 Spring AI 提供标准化的 API 接口支持多种主流 AI 服务提供商 (如 OpenAI、阿里云通义千问等)。开发者可通过统一的接口调用不同 AI 服务无需关心底层差异降低代码复杂性和维护成本。
2. 丰富的模型支持
文生图模型 支持创意图像生成 (如 OpenAI 的 DALL-E、Stability AI 的模型)。嵌入模型 将文本或多模态内容转换为向量表示支持语义搜索、推荐系统等场景。音频模型 支持语音识别和语音合成功能。
3. 结构化数据输出 Spring AI 提供 OutputParser 接口可将 AI 模型的响应解析为结构化的 Java 对象 (如 POJO)方便后续数据处理和存储。
4. 流式数据响应 支持 Flux 流式输出适用于实时对话等高并发场景。系统可在数据生成过程中实时返回部分内容提升用户体验同时节省内存资源。
5. 向量数据库集成 支持主流向量数据库 (如 Pinecone、Redis、PostgreSQL/PGVector 等)结合嵌入技术实现语义搜索与知识增强生成 (RAG)提升信息检索的准确性和效率。
6. 函数调用与扩展 允许注册自定义函数使 AI 模型能够调用外部 API 或数据库解决知识陈旧问题。
ChatClient ChatClient 接口提供了构建和配置聊天客户端对象的灵活性以及发起和处理聊天请求的能力。用户可以通过 ChatClient.Builder 来定制客户端的行为然后使用 prompt() 和 prompt(Prompt prompt) 方法设置请求规范最后通过 call() 方法发起聊天请求。
1. 引入依赖
dependencies!-- spring-ai --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-openai-spring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency
/dependencies
dependencyManagementdependenciesdependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion1.0.0-M5/versiontypepom/typescopeimport/scope/dependency/dependencies
/dependencyManagement2. yml配置
server:port: 8001spring:application:name: spring-ai-deepseekai:openai:api-key: sk-bc8e0a85f6f640b28720******base-url: https://api.deepseek.com/v1chat:options:model: deepseek-chat# temperature参数用于控制生成文本的多样性#值越高生成的文本越多样化但也可能包含更多的随机性和不可预测的内容#值越低生成的文本越接近于确定性的结果即生成的文本会更加一致和可预测temperature: 0.73. 主启动类
SpringBootApplication
public class DeepSeekMain {public static void main(String[] args) {SpringApplication.run(DeepSeekMain.class, args);}
}4. 配置类 AIConfig
Configuration
public class AIConfig {Beanpublic ChatClient chatClient(ChatClient.Builder chatClientBuilder) {return chatClientBuilder.build();}
}5. 控制类 ChatClientController
RestController
RequestMapping(/client)
public class ChatClientController {Autowiredprivate ChatClient chatClient;//非流式响应GetMapping(/chatCall)public String chatCall(RequestParam(msg) String msg) {return chatClient.prompt() //提示词.user(msg) //用户输入信息.call() //调用大模型.content(); //返回文本}//流式响应指定编码方式避免乱码GetMapping(value /chatStream, produces text/html;charsetUTF-8)public FluxString chatStream(RequestParam(msg) String msg) {return chatClient.prompt() //提示词.user(msg) //用户输入信息.stream() //调用大模型.content(); //返回文本}
}call和stream的区别
非流式输出 call 等待大模型把回答结果全部生成后输出给用户。流式输出 stream 逐个字符输出一方面符合大模型生成方式的本质另一方面当模型推理效率不是很高时流式输出比起全部生成后再输出大大提高用户体验。
ChatModel ChatModel 接口中带有 String 参数的 call() 方法简化了实际的使用避免了更复杂的 Prompt 和 ChatResponse 类的复杂性。但是在实际应用程序中更常见的是使用 ChatResponse call() 方法该方法采用 Prompt 实例并返回 ChatResponse。
我们使用的 ChatClient 底层是使用 ChatModel 作为属性的在初始化 ChatClient 的时候可以指定 ChatModel。
RestController
RequestMapping(/model)
public class ChatModelController {Autowiredprivate ChatModel chatModel;GetMapping(/chatCall)public String chatCall(RequestParam(msg) String msg) {return chatModel.call(msg);}GetMapping(/chatCallByPrompt)public String chatCallByPrompt(RequestParam(msg) String msg) {OpenAiChatOptions openAiChatOptions OpenAiChatOptions.builder().model(deepseek-chat) //可以更换成其他大模型.temperature(0.8).build();Prompt prompt new Prompt(msg, openAiChatOptions);ChatResponse chatResponse chatModel.call(prompt);return chatResponse.getResult().getOutput().getContent();}
}提示词
提示词是引导大模型生成特定输出的输入提示词的设计和措辞会极大地影响模型的响应结果。
Spring AI 提供了 Prompt Template 提示词模板管理抽象开发者可以预先定义好模板并在运行时替换模板中的关键词。在 Spring AI 与大模型交互的过程中处理提示词首先要创建包含动态内容占位符 {占位符} 的模板然后这些占位符会根据用户请求或应用程序中的其他代码进行替换。在提示词模板中{占位符} 可以用 Map 中的变量动态替换。
RestController
RequestMapping(/model)
public class ChatModelController {Autowiredprivate ChatModel chatModel;//提示词GetMapping(/chatPrompt)public String chatPrompt(RequestParam(name) String name,RequestParam(habit) String habit) {String msg 给我推荐至少三种北京的旅游景点;UserMessage userMessage new UserMessage(msg);String systemText 你的名字是{name}你是一个旅游景点咨询助手你应该用{habit}的旅游习惯给人们推荐全国各地的旅游景点信息。;SystemPromptTemplate systemPromptTemplate new SystemPromptTemplate(systemText);//替换占位符MapString, Object map new HashMap();map.put(name, name);map.put(habit, habit);Message systemMessage systemPromptTemplate.createMessage(map);ListMessage list new ArrayList();list.add(userMessage);list.add(systemMessage);Prompt prompt new Prompt(list);ListGeneration results chatModel.call(prompt).getResults();return results.stream().map(x - x.getOutput().getContent()).collect(Collectors.joining());}
}总结 以上主要介绍了 Spring AI 的 ChatClient 和 ChatModel 接口的相关知识想了解更多 Spring AI 知识的小伙伴请参考 Spring AI 官网 进行学习学习更多 Spring AI 实战实用技巧的小伙伴请关注后期发布的文章认真看完一定能让你有所收获。