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 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

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

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();
    }
}

配置信息

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

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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庄隐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值