SpringAI入门示例

AI编程简介

纯Prompt模式

纯Prompt模式是AI编程中最基础的交互架构。用户通过输入自然语言文本(即Prompt)向AI模型发出指令,模型依据自身预训练所积累的知识和语言理解能力,直接生成相应的文本响应。其工作原理是,用户的Prompt作为输入触发模型的推理过程,模型在预训练形成的语义空间中进行模式匹配和内容生成,无需额外的外部工具或复杂的流程调用。

Agent+function Calling模式

Agent+function Calling模式引入了"Agent"(智能代理)的概念,结合函数调用机制,使AI能够更灵活地处理复杂任务。在该模式中,Agent充当决策者的角色,它首先对用户的需求进行分析和理解,然后根据需求判断需要调用哪些外部工具或函数来完成任务,最后将工具的返回结果进行整合并生成最终响应。

Rag模式

Rag模式,即检索增强生成(Retrieval-Augmented Generation)模式,结合了信息检索和文本生成技术。其核心思想是在生成内容之前,先从外部知识库或数据库中检索相关的信息,然后基于检索到的信息进行内容生成,从而使生成的内容更具针对性和准确性,尤其是在处理需要依赖最新数据或特定领域知识的任务时表现出色。

Fine-tuning模式

Fine-tuning模式是基于预训练模型进行进一步优化的架构。预训练模型通常在大规模通用数据集上进行训练,具备一定的通用语言理解或任务处理能力,但在特定领域或特定任务上的表现可能不够理想。Fine-tuning模式就是利用特定领域或任务的小规模数据集,对预训练模型进行微调,通过调整模型的部分或全部参数,使其更好地适应目标任务。

示例Demo

pom

xml 复制代码
<?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.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wdz</groupId>
    <artifactId>spring-ai</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-ai</name>
    <description>spring-ai</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>1.0.0-M7</spring-ai.version>
    </properties>
    <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-ollama</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置ChatClient

java 复制代码
package com.wdz.springai.configer;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OllamaConfigure {
    @Bean
    public ChatClient chatClient(OllamaChatModel ollamaModel, ChatMemory chatMemory) {
        return ChatClient.builder(ollamaModel)
                .defaultSystem("你是一个安全顾问,从安全角度回答问题")
                .defaultAdvisors(new SimpleLoggerAdvisor(),new MessageChatMemoryAdvisor(chatMemory))
                .build();
    }
}

配置ChatMemory

java 复制代码
package com.wdz.springai.configer;

import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatMemoryConfigure {
    @Bean
    public ChatMemory chatMemory(){
        return  new InMemoryChatMemory();
    }
}

配置信息

application.properties 复制代码
spring.application.name=spring-ai
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.model=deepseek-r1:1.5b

logging.level.org.springframework.ai=debug

调用chat

kotlin 复制代码
package com.wdz.springai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatController {
    @Autowired
    private ChatClient client;
    /**
     * chat接口
     */
    @RequestMapping("/chat")
    public String chat(String content) {
        return client.prompt().user(content).call().content();
    }

    /**
     * stream chat接口
     */
    @RequestMapping(value = "/streamChat",headers = "Accept=text/html;charset=utf-8")
    public Flux<String> streamChat(String content) {
        return client.prompt().user(content).stream().content();
    }
}
相关推荐
码事漫谈1 天前
智能体颠覆教育行业调研报告:英语、编程、语文、数学学科应用分析
后端
蓝-萧1 天前
使用Docker构建Node.js应用的详细指南
java·后端
码事漫谈1 天前
《C语言点滴》——笑着入门,扎实成长
后端
Tony Bai1 天前
【Go模块构建与依赖管理】09 企业级实践:私有仓库与私有 Proxy
开发语言·后端·golang
咖啡教室1 天前
每日一个计算机小知识:ICMP
后端·网络协议
间彧1 天前
OpenStack在混合云架构中通常扮演什么角色?
后端
咖啡教室1 天前
每日一个计算机小知识:IGMP
后端·网络协议
间彧1 天前
云原生技术栈中的核心组件(如Kubernetes、Docker)具体是如何协同工作的?
后端
清空mega1 天前
从零开始搭建 flask 博客实验(3)
后端·python·flask
努力的小郑1 天前
Elasticsearch 避坑指南:我在项目中总结的 14 条实用经验
后端·elasticsearch·性能优化