admin管理员组

文章数量:1530518

前言

在当今快速发展的技术世界中,人工智能(AI)正逐渐成为开发者的得力助手。而ChatGPT作为一种强大的自然语言处理模型,已经被广泛应用于聊天机器人、智能助手、自动回复系统等领域。本文将介绍如何使用Spring AI集成ChatGPT,让它成为你的开发助手。

什么是Spring AI?

Spring AI是一个基于Spring Boot框架的AI应用开发工具包。它旨在简化AI模型的集成和部署,使开发者能够更快地构建智能应用。Spring AI提供了一系列功能,包括对话管理、自然语言处理、图像识别等,其中ChatGPT就是其中之一。

ChatGPT:智能对话的利器

ChatGPT是由OpenAI开发的自然语言处理模型,它基于大规模的预训练数据,能够理解和生成自然语言。通过集成ChatGPT,你可以实现以下功能:

  1. 自动回复:ChatGPT可以根据用户的输入自动产生回复,无需手动编写每一条回复。
  2. 智能问答:将ChatGPT嵌入到你的应用中,让它成为用户的智能问答助手。
  3. 聊天机器人:构建一个智能聊天机器人,让用户可以与之进行自然对话。

集成ChatGPT到Spring Boot应用

创建springboot项目

选择Spring Initializr ,添加相应名称,JDK17及以上版本,完成点击下一步

分别添加Spring Web与OpenAI的依赖项

项目创建完成,接下来查看pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/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>
    <groupId>com.example</groupId>
    <artifactId>springai</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springai</name>
    <description>springai</description>
    <properties>
        <java.version>17</java.version>
        <spring-ai.version>0.8.1</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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

创建对话类ChatController

package com.example.springai;

import org.springframework.ai.chat.ChatClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @PostMapping("generate")
    public Map<String, String> generate(@RequestParam(value = "message",defaultValue = "请用JAVA写一个冒泡排序") String message) {
        String call = chatClient.call(message);
        return Map.of("message", call);
    }
}

配置application.yaml,
大家可自行通过https://platform.openai/申请apiKey

spring:
  application:
    name: springai
  ai:
    openai: ##
      api-key: sk-DZWLxwZ2I6vr98GO*****IssYT3Bl****
      chat:
        options:
          temperature: 0.7
          model: gpt-3.5-turbo

server:
  port: 8080

构建启动类:SpringAIApplication

由于目前国内不能直接访问OpenAI,所以这里需要根据你科学上网的工具来科学配置一下,你才可以科学的在代码中使用。
当然,如果你的网络支持可以忽略启动类中的配置!

package com.example.springai;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAIApplication {

    public static void main(String[] args) {
        String proxy = "127.0.0.1";
        int port = 7890;
        System.setProperty("proxyType", "4");
        System.setProperty("proxyPort", Integer.toString(port));
        System.setProperty("proxyHost", proxy);
        System.setProperty("proxySet", "true");
        SpringApplication.run(SpringAIApplication.class, args);
    }

}

自此一个简单的聊天应用已经构建完成,接下来让我们验证一下



OK!成功接入了chatGPT!

结论

Spring AI与ChatGPT的结合,为开发者提供了一个强大的工具,使得构建智能应用变得更加简单和高效。无论是构建聊天机器人、智能助手还是自动回复系统,Spring AI都能让ChatGPT成为你的得力助手。

如果你对Spring AI和ChatGPT感兴趣,不妨试试集成它们,让你的应用变得更加智能和灵活吧!
更多问题请浏览官方文档:
https://spring.io/projects/spring-ai#overview
https://docs.spring.io/spring-ai/reference/api/chatclient.html

本文标签: 助手SpringAIchatGPT