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系列专栏。

相关推荐
郑世录34 分钟前
codex 接本地模型,单次输出卡在 3208 token:一份照着做就能改的完整教程
ai编程
Forget the Dream39 分钟前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
JavaGuide2 小时前
Gemini 真的变强了,写代码做视频的能力接近 Claude Opus5.5 了!
前端·ai编程
qinglongcn2 小时前
四面统一:记忆/Session/观测/Cron
agent·ai编程
qinglongcn2 小时前
从 metadata 键到独立列
agent·ai编程
黑十2 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能
zdr尽职尽责2 小时前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
神明不懂浪漫3 小时前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
YanisJiang3 小时前
别只把 AI 聊天当成「又一个网页」:把状态驱动的陪伴内核接到你自己的项目里
ai编程
用户54817845909503 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python