idea集成SpringBoot + Ai聊天程序

免费使用

  • 🚀申请领取内测免费API Key
  • 免费版支持gpt-3.5-turbo, embedding, gpt-4。其中gpt-4由于价格过高,每24小时限制10次调用,且不支持流式传输。需要更稳定快速的gpt-4请使用付费版。
  • 转发Host1: https://api.chatanywhere.tech (国内中转,延时更低,host1和host2二选一)
  • 转发Host2: https://api.chatanywhere.com.cn (国内中转,延时更低,host1和host2二选一)
  • 转发Host3: https://api.chatanywhere.cn (国外使用,国内需要全局代理)

我们会定期根据使用量进行相应的扩容,只要不被官方制裁我们会一直提供免费API,如果该项目对你有帮助,还请为我们点一个Star。如果遇到问题可以在Issues中反馈,有空会解答。

该API Key用于转发API,需要将Host改为api.chatanywhere.tech(国内首选)或者api.chatanywhere.cn(国外使用,国内需要全局代理)。

改下pom文件

原先:

修改后:

原先:

修改后:

完整pom文件

<?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.5</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>

Controller层

package com.zzq.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;

@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 called = openAiChatClient.call(msg);
        return called;
    }

    /**
     * 调用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-32k")//使用的模型,32k是参数量//参数量越高,准确率越高
                .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();//数据的序列
    }
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
作为一个AI模型,人脸识别涉及到多个技术领域。在此我们以常规的人脸识别为基础,实现一个简单的基于springboot/vue/mybatis的人脸识别系统。 1、人脸检测和人脸识别的技术选择 我们可以使用开放的Python的人脸识别库(face_recognition),通过Python调用,实现人脸检测和人脸识别。Face_recognition 是使用Dlib进行基于Python的人脸识别的开源项目,仅仅需要几行代码就可以完成一项任务,包括人脸识别,面部比较和面部定位等。 2、技术实现步骤 2.1、环境搭建 开发工具:IntelliJ IDEA 数据库:MySQL 前端技术:Vue.js 后端技术:Spring Boot + MyBatis + Face Recognition Library 2.2、集成Face Recognition Library 通过Python安装Face Recognition Library(face_recognition)库,直接使用Python的Pip命令安装 pip install face_recognition 2.3、指定图片目录 在项目中指定图片目录,将其用于人脸检测和识别,默认将图片存储在本项目路径下的img文件夹中。 2.4、前端设计 使用Vue.js实现前端设计,支持以下功能: - 显示识别结果和置信度 - 支持上传图片,实现人脸识别 - 支持查询人脸信息 2.5、后端设计 使用Spring Boot和MyBatis实现后端功能,包括人脸检测和识别,以及查询人脸信息。 使用Spring Boot实现RESTful API,以处理前端请求和响应。 在MyBatis Mapper文件中定义SQL语句,用于从数据库中检索人脸信息。 2.6、上传图片实现人脸识别 实现上传图片实现人脸识别功能,主要包括以下步骤: - 通过上传功能获取上传图片,并且存储到指定目录下 - 对于新上传的照片进行人脸检测和识别 - 将人脸特征存储到数据库中,用于后续识别和查询 3、总结 我们可以通过Spring Boot和Vue.js对Face Recognition Library(face_recognition)进行集成,实现一个基于人脸识别的系统。同时,我们也应该意识到,在实现基于人脸识别的系统时,我们需要保持对隐私和数据保护的高度警惕性,避免出现不必要的隐私泄露情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃java的羊儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值