【Spring AI 学习(三)】实现简单的对话

接下来用spring ai演示如何实现简单的对话功能

首先在spring_ai工程下,创建一个子模块spring_ai_chat,如下图

整体项目结构如下

Maven依赖添加

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.company</groupId>
        <artifactId>spring_ai</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>spring_ai_chat</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-ai.version>1.0.0-M5</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-openai-spring-boot-starter</artifactId>
        </dependency>

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

</project>

application.properties增加配置信息(需要注意的是,spring.ai.openai.api-key项的值,需要使用自己在deepseek开发平台申请到的key值 参照 https://platform.deepseek.com/api_keys )

java 复制代码
server.port=8080
spring.application.name=spring-ai-demo

spring.ai.openai.api-key=sk-d02d7cb557f4686868
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.chat.options.temperature=0.7

编写启动类

java 复制代码
package com.company;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ChatApplication {

    public static void main(String[] args) {
        SpringApplication.run(ChatApplication.class, args);
    }
}

编写控制器

java 复制代码
package com.company.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    private static final Logger logger = LoggerFactory.getLogger(ChatController.class);

    private ChatClient chatClient;

    public ChatController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/chat")
    public String chat(String message) {
        return chatClient
                //提示词
                .prompt()
                //用户输入的信息
                .user(message)
                //调用大模型
                .call()
                //返回结果
                .content();
    }
}

启动项目,然后访问链接 localhost:8080/chat?message=你会哪些技能

相关推荐
石狮松松几秒前
第 05 章 你的第一行"AI"代码:拟合一条直线
人工智能
Promising_GEO几秒前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
用户360055579001 分钟前
Kestrel_VLLM - 14 · router 两个方向判否,与 decode 的真瓶颈
人工智能
狒狒AI3 分钟前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
大文说跨境7 分钟前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
自养Agent8 分钟前
Agent Harness 从用到会造:报错的那层,往往不是坏掉的那层
人工智能
RockChinQ9 分钟前
一个 Telegram 群,三条事件路由:欢迎、群规问答与群管理各归一个 Agent
人工智能
果霸大叔11 分钟前
从根上吃透AI Agent:Agent拆解系列之DeerFlow2.0深度拆解
人工智能
2025的限定小孙12 分钟前
AI自己写的SQL自己跑?把执行权从模型手里拿走的三层闸
人工智能
用户4270192301213 分钟前
排障实录 :OpenClaw 升级失败三次,我是怎么把系统救回来的
人工智能