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();
    }
}
相关推荐
淬渊阁5 小时前
Hello world program of Go
开发语言·后端·golang
Pandaconda6 小时前
【新人系列】Golang 入门(十五):类型断言
开发语言·后端·面试·golang·go·断言·类型
周Echo周6 小时前
16、堆基础知识点和priority_queue的模拟实现
java·linux·c语言·开发语言·c++·后端·算法
魔道不误砍柴功6 小时前
Spring Boot自动配置原理深度解析:从条件注解到spring.factories
spring boot·后端·spring
风象南7 小时前
基于Redis的3种分布式ID生成策略
redis·后端
魔道不误砍柴功7 小时前
Spring Boot 核心注解全解:@SpringBootApplication背后的三剑客
java·spring boot·后端
Asthenia04128 小时前
分布式唯一ID实现方案详解:数据库自增主键/uuid/雪花算法/号段模式
后端
Asthenia04128 小时前
内部类、外部类与静态内部类的区别详解
后端
Asthenia04128 小时前
类加载流程之初始化:静态代码块的深入拷打
后端