2024版本idea集成SpringBoot + Ai 手写一个chatgpt 【推荐】

题目:SpringBoot + OpenAi

在这里获取key和url:获取免费key

base-url为这两个:

话不多说直接来!

一、简介

Spring AI 是 AI 工程的应用框架。其目标是将 Spring 生态系统设计原则(如可移植性和模块化设计)应用于 AI,并推广使用 POJO 作为 AI 领域应用程序的构建块。

跨 AI 提供商的可移植 API 支持,适用于聊天、文本到图像和嵌入模型。支持同步和流 API 选项。还支持下拉以访问特定于模型的功能

二、Ai聊天程序代码

1、 创建项目工程

  • 在父工程下面创建新的模块
  • 勾选上依赖然后创建
  • 具体的依赖如下
java 复制代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- Generated by https://start.springboot.io -->
    <!-- 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn -->
    <groupId>com.ysl</groupId>
    <artifactId>SpringAi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>SpringAi</name>
    <description>SpringAi</description>
    <modules>
        <module>spring-ai-01-chat</module>
    </modules>

    <properties>
        <java.version>17</java.version>
<!--        快照版本-->
        <spring-ai.version>1.0.0-SNAPSHOT</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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
<!--    相当于继承一个父项目:spring-ai-bom-->
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!--~配置本项目的仓库:因maven中心仓库还设有更新spring ai的jar包-->
    <repositories>
        <repository>
<!--            里程碑版本releases的仓库,改成快照版本的snapshot-->
            <id>spring-snapshot</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

</project>
  • 编写yml配置
  • openai有自动配置类OpenAiAutoConfiguration

    其中有聊天客户端,图片客户端...等(看下面源码)
java 复制代码
//聊天客户端
@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = OpenAiChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
			matchIfMissing = true)
	public OpenAiChatClient openAiChatClient(OpenAiConnectionProperties commonProperties,
			OpenAiChatProperties chatProperties, RestClient.Builder restClientBuilder,
			List<FunctionCallback> toolFunctionCallbacks, FunctionCallbackContext functionCallbackContext,
			RetryTemplate retryTemplate, ResponseErrorHandler responseErrorHandler) {

		var openAiApi = openAiApi(chatProperties.getBaseUrl(), commonProperties.getBaseUrl(),
				chatProperties.getApiKey(), commonProperties.getApiKey(), restClientBuilder, responseErrorHandler);

		if (!CollectionUtils.isEmpty(toolFunctionCallbacks)) {
			chatProperties.getOptions().getFunctionCallbacks().addAll(toolFunctionCallbacks);
		}

		return new OpenAiChatClient(openAiApi, chatProperties.getOptions(), functionCallbackContext, retryTemplate);
	}
java 复制代码
//图片客户端
@Bean
	@ConditionalOnMissingBean
	@ConditionalOnProperty(prefix = OpenAiImageProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
			matchIfMissing = true)
	public OpenAiImageClient openAiImageClient(OpenAiConnectionProperties commonProperties,
			OpenAiImageProperties imageProperties, RestClient.Builder restClientBuilder, RetryTemplate retryTemplate,
			ResponseErrorHandler responseErrorHandler) {

		String apiKey = StringUtils.hasText(imageProperties.getApiKey()) ? imageProperties.getApiKey()
				: commonProperties.getApiKey();

		String baseUrl = StringUtils.hasText(imageProperties.getBaseUrl()) ? imageProperties.getBaseUrl()
				: commonProperties.getBaseUrl();

		Assert.hasText(apiKey, "OpenAI API key must be set");
		Assert.hasText(baseUrl, "OpenAI base URL must be set");

		var openAiImageApi = new OpenAiImageApi(baseUrl, apiKey, restClientBuilder, responseErrorHandler);

		return new OpenAiImageClient(openAiImageApi, imageProperties.getOptions(), retryTemplate);
	}

二、一个简单的示例

1、直接写一个Controller层就可以

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

import jakarta.annotation.Resource;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatClient;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
* @Author Ysl
* @Date 2024/5/11
* @name SpringAi
**/
@RestController
public class ChatController {
    /**
     * OpenAi自动装配,可以直接注入使用
     */
    @Resource
    private OpenAiChatClient openAiChatClient;

    /**
     * 调用OpenAi的接口,call方法为同步的api
     * @param msg 你要问的问题
     * @return
     */
    @RequestMapping ("/ai/chat")
    public String chat(@RequestParam("msg") String msg) {
        String call = openAiChatClient.call(msg);
        return call;
    }

    /**
     * 调用OpenAi的接口
     * @param msg 你要问的问题
     * @return  Object--json对象
     */
    @RequestMapping ("/ai/chat1")
    public Object chat1(@RequestParam("msg") String msg) {
        ChatResponse response = openAiChatClient.call(new Prompt(msg));
        return response;
//        return response.getResult().getOutput().getContent();//只拿到内容
    }

    /**
     * 调用OpenAi的接口
     * @param msg 你要问的问题
     * @return
     */
    @RequestMapping ("/ai/chat3")
    public String chat3(@RequestParam("msg") String msg) {
        //可选参数在yml配置,同时在代码中也配置,那么会以代码为准
        ChatResponse response = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()
//                .withModel("gpt-4")//使用的模型
                .withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
                .build()));
        return response.getResult().getOutput().getContent();
    }

    /**
     * 调用OpenAi的接口 stream是流式的api
     * @param msg 你要问的问题
     * @return
     */
    @RequestMapping ("/ai/chat4")
    public Object chat4(@RequestParam("msg") String msg) {
        //可选参数在yml配置,同时在代码中也配置,那么会以代码为准
        Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder()
//                .withModel("gpt-3.5")//使用的模型
                .withTemperature(0.3F)//温度越高回答越慢,温度越低回答越快
                .build()));
        flux.toStream().forEach(chatResponse ->{
            System.out.println(chatResponse.getResult().getOutput().getContent());
                });
        return flux.collectList();//数据的序列
    }
}

2、直接在浏览器访问

相关推荐
Daniel 大东40 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
不去幼儿园43 分钟前
【MARL】深入理解多智能体近端策略优化(MAPPO)算法与调参
人工智能·python·算法·机器学习·强化学习
想成为高手4991 小时前
生成式AI在教育技术中的应用:变革与创新
人工智能·aigc
YSGZJJ2 小时前
股指期货的套保策略如何精准选择和规避风险?
人工智能·区块链
无脑敲代码,bug漫天飞2 小时前
COR 损失函数
人工智能·机器学习
HPC_fac130520678163 小时前
以科学计算为切入点:剖析英伟达服务器过热难题
服务器·人工智能·深度学习·机器学习·计算机视觉·数据挖掘·gpu算力
Theodore_10225 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
小陈phd5 小时前
OpenCV从入门到精通实战(九)——基于dlib的疲劳监测 ear计算
人工智能·opencv·计算机视觉
冰帝海岸6 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试