SpringAI实战之快速上手

1. 快速上手

介绍

  • 模型集成:支持OpenAI、Hugging Face等主流AI服务,轻松切换不同大模型(如GPT-4、Llama 2、Deepseek等)

  • 向量数据库:集成Pinecone、Chroma等向量数据库,支持语义搜索与相似度匹配

  • 文档处理:自动解析PDF、Markdown等文档,提取文本内容生成向量嵌入

  • 对话记忆:内置对话历史管理,支持上下文感知的多轮对话

  • 工具调用:通过@Tool注解快速集成外部API(如天气查询、数据库操作)

  • 优化求解:集成Timefold Solver解决资源调度等优化问题

  • 可观测性:提供指标监控(延迟、Token消耗)和日志追踪

  • 代理模式:支持工作流驱动和自主决策两种代理模式,实现复杂任务自动化

  • MCP: 支持MCP 客户端和 MCP服务端

    申请API key

查看可用的模型

点击链接跳转 https://www.bigmodel.cn/console/modelcenter/square,后续从这里找模型

创建Spring Boot项目

创建项目之后,引入依赖

配置依赖管理

复制代码
 <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0</spring-ai.version>
</properties>
 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>${spring-ai.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

引入Spring AI相关依赖

复制代码
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-zhipuai</artifactId>
        </dependency>
</dependencies>

添加配置文件

复制代码
server:
  port: 8080
spring:
  ai:
    zhipuai:
      api-key: ${创建的key}

到这一步基本工作已经完成

使用Chat Client 完成大模型调用

在调用过程中,我们需要使用spring ai的chat client和 chat model

复制代码
@Autowired
private ChatModel zhiPuAiChatModel;

    @GetMapping("/ai")
    public String generation(@RequestParam(defaultValue = "你觉得知识能改变命运么?") String userInput) {
        ChatClient chatClient = ChatClient.create(zhiPuAiChatModel);
        String content = chatClient.prompt(userInput).call().content();
        return content;
    }

请求API http://localhost:8080/ai,得到相应结果:

那么到这里我们已经完成了Spring AI 与大模型交互的第一步,接下来会给大家分享更多Spring AI提供的能力。具体详情可见本人的SpringAI系列专栏。

相关推荐
Seven977 小时前
两小时入门Sentinel
java
Agent手记7 小时前
安全生产巡检全流程自动化与隐患预警方案:2026工业Agent落地实战指南
数据库·人工智能·安全·ai·自动化
tongluowan0077 小时前
Java中atomic底层原理 - ABA 问题与解决方案
java·juc·atomic
一切皆是因缘际会8 小时前
本源投影内生智能:从概率拟合到硅基生命的底层重构
人工智能·深度学习·机器学习·ai·重构
无关86888 小时前
Spring Boot 项目标准化部署打包实战
java·spring boot·后端
老王谈企服8 小时前
AI Agent将如何重构制造业的市场竞争战略决策模式?[2026数智转型深度洞察与技术解决方案]
人工智能·ai·重构
jay神8 小时前
基于微信小程序课外创新实践学分认定系统
java·spring boot·小程序·vue·毕业设计
winlife_8 小时前
把 Godot 编辑器接入 AI:Funplay MCP for Godot 介绍
人工智能·编辑器·godot·ai编程·游戏开发·mcp
Gauss松鼠会8 小时前
GaussDB(DWS) GUC参数修改、查看
java·数据库·sql·数据库开发·gaussdb
AIFQuant8 小时前
Java 对接全球股票实时报价:高可用架构与异常处理
java·开发语言·websocket·金融·架构·股票api