给AI一个角色设定

继续上一章的内容,创建一个 Config 配置类

java 复制代码
package com.hza.ai.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {


    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder.defaultSystem("你是一个超市的客服,超市的信息是:小萌超市,超市里有一款热卖的马超周边。")
                .build();
    }


}

修改 Controller 代码

java 复制代码
package com.hza.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai")
public class AiController {

    //智能对话客户端
    @Autowired
    private ChatClient chatClient;


    @GetMapping("/chart")
    public String generation(@RequestParam(value="message",defaultValue = "给我讲个笑话") String message) {
        //prompt:提示词
        return this.chatClient.prompt()
                //用户信息
                .user(message)
                //远程请求大模型
                .call()
                //返回文本
                .content();
    }

}

运行项目进行验证:

相关推荐
鲁Q同志13 小时前
IDEA无限重置激活三十天
intellij-idea·1024程序员节
ch.ju14 小时前
Java——idea知识:Collection父接口
java·开发语言·intellij-idea
开开心心就好1 天前
发票信息录入太麻烦?离线工具一键转表格
运维·服务器·前端·pdf·ocr·maven·intellij-idea
qq_366086222 天前
idea项目重新启动时报错找不到jar
pycharm·intellij-idea·jar
齐 飞3 天前
idea中git误操作撤回git reset --hard ‘HEAD@{1}‘
java·git·intellij-idea
golang学习记3 天前
IDEA 的 Java 智能,现在跑到 Cursor 和 VS Code 里去了
java·ide·vscode·intellij-idea·claude code
熊出没4 天前
CommitLoom 使用教程:在 IntelliJ IDEA 中自动生成 Git 提交信息
java·git·intellij-idea
golang学习记4 天前
IDEA 2026年的玩法已经完全不同
java·ide·intellij-idea
阿萨德528号4 天前
IDEA 项目 SVN 配置消失、版本控制变成 Git 且无 VCS 菜单的解决办法
git·svn·intellij-idea
开开心心就好5 天前
白天晚上自动调蓝光,护眼工具亮度可调
服务器·开发语言·算法·pdf·ocr·intellij-idea·idea