Spring Boot quick access to ChatGPT

This article is participating in the Artificial Intelligence Creator Support Program

1. Introduction

Since OpenAI-ChatGPT became popular, topics surrounding the application of OpenAI-ChatGPT have emerged one after another, and the development of large-scale artificial intelligence is an unstoppable trend. lucy-chat It is a Java solution for quickly accessing OpenAI-ChatGPT large-scale model artificial intelligence in the Java environment. We cannot create tools, but we need to use tools better. This package simplifies the access process, and k developers can easily introduce and Use the relevant functions provided by ChatGPT. the

2. Quick access

lucy-chat Two forms of access services are provided. After integration or independent deployment, you can visit [deployment address]/doc.html to call related interfaces.

2.1 Create a project

First, use IntelliJ IDEA to build a Spring Boot project.


Next, we start the project, if there are no errors. When we type in the browser: http://localhost:8080 will output the following.


 

2.2 Jar introduction

Before introducing any Lucy series dependencies, you need to complete the configuration of the jitpack mirror warehouse, as follows.

<repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://www.jitpack.io</url>
        </repository>
</repositories>

Then, we add the lucy-chat dependency to the Spring Boot project, which is currently 1.0.0-r4 by default.

<dependency>
    <groupId>com.gitee.kindear</groupId>
    <artifactId>lucy-chat</artifactId>
    <version>${version}</version>
</dependency>

After adding dependencies, you need to refresh the project to complete the lucy-chat dependencies, as shown below.

After the dependency is completed, we open the startup file of the project, and then enable the knife4j document, that is, we need to configure @EnableKnife4j on the startup class, and change the startup entry to LucyChatApplication.

@EnableKnife4j
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(LucyChatApplication.class, args);
    }
}

Before using lucy-chat, you need to configure the following file information in the configuration file.

spring.application.name=lucy-chat
# run port
server.port=8080
# swagger match
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# chat-gpt api-key
# Application address https://platform.openai.com/account/api-keys
openai.chat.key=
# chat-gpt proxy host
# Configure proxy address, please refer to https://www.v2ex.com/t/921689
openai.chat.host=
# The maximum number of connections in the connection pool
forest.max-connections=1000
# Connection timeout, in milliseconds
forest.connect-timeout=30000
# Data read timeout, in milliseconds
forest.read-timeout=30000

To be able to access openAi's Api normally, you need to go to openAi's official website to obtain an api-key. The link to apply is:

https://platform.openai.com/account/api-keys

2.3 Independent Services

Of course, we can also deploy lucy-chat as an independent service. First, you need to download the project from the open source address:

git clone https://gitee.com/Kindear/lucy-chat

Next, modify the packaging method in the POM file, that is, restore the commented out content related to <build>, refer to the following.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Then refer to the relevant content of the configuration file above to modify the relevant configuration file, and set the key provided in the project as a private key.

 

3. Test

After completing the configuration, you can visit [service address]/chat/web to enter the WebChat page, and you can directly use the Iframe tag to import it in other front-end applications.

 

lucy-chat source code: https://gitee.com/Kindear/lucy-chat

Tags: ChatGPT

Posted by ivytony on Wed, 22 Mar 2023 16:38:13 +1030