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

相关推荐
泡海椒1 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
孟健2 小时前
Jev是什么AI模型?不做自然语言生成为何引发热议
ai编程
景熙55232 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7042 小时前
Spring Security权限控制
java·python·spring
ShineWinsu3 小时前
对于Coze—AI:SDK的解析
人工智能·python·ai·sdk·项目·coze·字节跳动
杨运交3 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
代码方舟3 小时前
零信任架构实战:基于天远名下企业A构建自动化B2B供应链准入网关
人工智能·ai·工具分享
Geek-Chow3 小时前
CountDownLatch in Java
java
右耳朵猫AI3 小时前
Python周刊2026W38 | 标准流编码修复、PEP 845/846 草案、解析器提速 10%、集合字典二次复杂度
python·ai·数据科学