【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=你会哪些技能

相关推荐
弯_弯30 分钟前
AI 项目验收:别在上线那天才谈怎么算成
人工智能·企业ai落地·企业ai培训·fde
.道阻且长.38 分钟前
AI 按数据模态分类:计算机视觉、NLP、语音处理,一篇理清
人工智能·计算机视觉·分类
Java后端的Ai之路1 小时前
RAG 还是 Agent 自主检索?Claude Code 删掉向量库之后,我把选型讲透了
开发语言·人工智能·prompt·agent·rag
笃行3502 小时前
用 Rokid AIUI 开发日语学习助手
人工智能
段一凡-华北理工大学4 小时前
工业智能体:从Codex/Claude到项目实战~系列文章06:一张蓝图看懂“工业智能体×高炉炼铁“
人工智能·claude·codex·工业智能体·高炉炼铁智能化·高炉智能体技术栈
怕浪猫4 小时前
十个人,630 万订阅:一张 AI 学习清单里的五种老师
人工智能
JonaHu5 小时前
5分钟搭建帮你赚钱的 AI API 网站:从品牌门户到客户 Key 交付
人工智能·api·开发者·ai创业·acedatacloud
Csvn5 小时前
模型挂了全站 502?降级链让它先保住“有响应”(R04)
人工智能·aigc·agent
isNotNullX5 小时前
仓储管理最全框架:6大流程、8项指标、10个管理动作、12个常见问题
大数据·人工智能
2601_962106646 小时前
【Java Set综合实战】商品管理系统 + 图书收藏系统|Product、Book自定义类全套代码
java