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

相关推荐
::呵呵哒::1 小时前
java中SseEmitter
java·开发语言
星恒随风1 小时前
C++ 多态底层原理:静态绑定、动态绑定、虚函数表与工程实践
开发语言·c++·笔记·学习
心念枕惊1 小时前
零基础认识大语言模型(LLM)工作原理(9.从聊天机器人到智能体:AI 为什么必须学会完成任务?)
人工智能·语言模型·机器人
IT_陈寒1 小时前
Python的finally居然不等同于Go的defer,差点坑惨我
前端·人工智能·后端
码农学院1 小时前
GEO效果度量体系:AI搜索可见性、引用率与转化追踪的技术实现
人工智能·dubbo
恋猫de小郭1 小时前
给 AI 的 Agent 实现指南,可控 Agent 的关键
前端·人工智能·ai编程
长不胖的路人甲1 小时前
Spring Bean 完整生命周期
java·spring·rpc
AI导出鸭PC端1 小时前
手机deepseek怎么导出pdf AI导出鸭
人工智能·pdf
蓦然回首却已人去楼空1 小时前
Build a Large Language Model (From Scratch) 第6章 针对分类的微调
人工智能·语言模型·分类