当前位置: 首页 > news >正文

spring-ai简单示例

文章目录

  • 一、不使用spring-ai时直接使用RestClient调用接口
  • 二、使用spring ai
    • 1、新增maven依赖
    • 2、application.properties设置
    • 3、controller

spring ai主要用于提供一致的编程模型,屏蔽不同 AI 服务提供商(如 OpenAI、Azure AI、Hugging Face 等)的底层差异。

一、不使用spring-ai时直接使用RestClient调用接口

controller:

package com.yogi.controller;import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestClient;import java.util.List;@RestController
@RequestMapping("/ai")
public class AITestController {@GetMapping("/debug-response")@Observedpublic String debugResponse() {try {RestClient restClient = RestClient.create();String response = restClient.post().uri("https://maas.xxxxx.com/v1/chat/completions").header("Authorization", "Bearer sk-xxxxxxxxxx").contentType(MediaType.APPLICATION_JSON).body("{\"model\": \"DeepSeek-V3.1\", \"messages\": [{\"role\": \"user\", \"content\": \"Hello\"}]}").retrieve().body(String.class);return "API响应: " + response;} catch (Exception e) {return "错误: " + e.getMessage();}}
}

二、使用spring ai

1、新增maven依赖

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

注意我这里用的Spring AI 0.8.1 需要java17及以上版本 spring boot3.2.0及以上版本
这里我用的3.2.6,完整的pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.6</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.yogi</groupId><artifactId>SpringAITest</artifactId><version>0.0.1-SNAPSHOT</version><name>SpringAITest</name><description>SpringAITest</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>0.8.1</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2、application.properties设置

注意这里的baseUrl和直接调用相比少了"/v1/chat/completions",因为spring ai会自动拼接。

spring.ai.openai.api-key=sk-xxxxxxxxxx
spring.ai.openai.base-url=https://maas.xxxxxxx.com
spring.ai.openai.chat.options.model=DeepSeek-V3.1

3、controller

package com.yogi.controller;import org.springframework.ai.chat.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/ai")
public class AITestController {private final ChatClient chatClient;// 直接注入统一的 ChatClient@Autowiredpublic AITestController(ChatClient chatClient) {this.chatClient = chatClient;}@GetMapping("/ask")public String ask(@RequestParam String question) {// 调用 call 方法,直接获取字符串回复return chatClient.call(question);}
}
http://www.dtcms.com/a/406404.html

相关文章:

  • sqlsugar sqlite
  • IP 授权管理标识:守护 IP 价值,解锁商业新可能
  • 深度学习学习路线图:从MNIST到MobileNetV4,从理论到实践的完整指南——基础理论与经典模型实践
  • C++项目:仿muduo库高并发服务器-------poller模块
  • 建设项目审批后公示在哪个网站国内重大新闻2022
  • 联雅网站建设公司谷城网站定制
  • Rust语言入门难,难在哪?所有权、借用检查器、生命周期和泛型介绍
  • 告别轮询!WebSocket 实战宝典:构建高效实时应用的完整解决方案
  • 【rust】: use of unstable library feature ‘os_str_display‘
  • seo如何根据网站数据做报表视频号推广方法
  • 政务网站队伍建设情况wordpress主题更改
  • PostIn入门到实战(3) - 如何快速设计并分享接口文档
  • Golang学习笔记: 常用标准库
  • 华为OD最新机试题A卷双机位-增强的strstr-2025年
  • SpringBoot+Vue学生选课管理系统
  • Golang语言基础篇007_结构体详解
  • 跨周期共振效应在ETF网格参数适配中的应用技巧
  • 济南网站开发招聘有赞商城官网登录
  • 湘潭网站设计外包公司定制v软件下载
  • 论文阅读:NeurIPS 2024 LLM Evaluators Recognize and Favor Their Own Generations
  • 软件设计师——02 程序设计语言基础知识
  • 数据科学入门
  • P11227[CSP-J 2024] T1 扑克牌
  • 快文CopyDone:营销文案AI生成工具
  • 北京网站开发专员优化科技
  • 商城门户网站源码政务公开 加强门户网站建设
  • 计算机视觉 - 物体检测 开山鼻祖 R-CNN系列:Fast R-CNN、Faster R-CNN、Mask R-CNN
  • 基于Go语言实现分布式类地球模拟系统
  • AI视频特效转场教程:用Nano Banana和Kling 2.1创造病毒式传播效果
  • 广州网站建设 易点网站企业管理培训课程