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

相关推荐
桃西西呀2 小时前
Laya 源码级原理拆解之一:整体架构与运行入口
人工智能·llm·ai编程
拉你进_教堂2 小时前
小龙虾技能之【Intelligent Public Smoking Detection Skill | 公共场所吸烟行为智能检测技能】简介
人工智能·计算机视觉·多模态大模型·openclaw小龙虾技能
ShineWinsu2 小时前
RAG 进阶:知识库构建优化全解析——从分块策略、元数据增强到层级索引与知识图谱
人工智能·知识图谱
吴佳浩2 小时前
Agent 安全红线:越狱防御、间接注入与数据防泄漏实战
人工智能·agent·ai编程
前端的阶梯2 小时前
AI Agent 之 深度解析上下文工程
人工智能
Omics Pro2 小时前
Cell封面|衰老生物学开源AI工具包
数据库·人工智能·算法·机器学习·自然语言处理
hoLzwEge2 小时前
把 WorkBuddy 每日签到搬上云端
人工智能·程序员
吴佳浩2 小时前
Claude Code 与 Hermes Agent 深度拆解:顶级 Coding Agent 为什么都放弃了纯 Chat 模式?
人工智能·agent·ai编程
Csvn2 小时前
前言与后记 · 全书完结
人工智能·aigc·agent
Csvn2 小时前
附录 C+D+E 参数速查表 · 术语表 · 中文模型 API 上手
人工智能·aigc·agent