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 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 be very convenient The introduction and use of related functions provided by ChatGPT. 

2. Quick access

lucy-chat provides two forms of access services. 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.

image.png

image.pngNext, we start the project, if there are no errors. When we enter: http://localhost:8080 in the browser , the following content will be output.

image.png  

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.

image.png

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
# 运行端口
server.port=8080
# swagger 匹配
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# chat-gpt api-key
# 申请地址 https://platform.openai.com/account/api-keys
openai.chat.key=
# chat-gpt proxy host
# 配置代理地址 请参阅 https://www.v2ex.com/t/921689
openai.chat.host=
# 连接池最大连接数
forest.max-connections=1000
# 连接超时时间,单位为毫秒
forest.connect-timeout=30000
# 数据读取超时时间,单位为毫秒
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:

platform.openai.com/account/api…

image.png

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 relevant commented out content, as follows.

<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.image.png  

Lucy-chat source code: gitee.com/Kindear/luc…

Guess you like

Origin juejin.im/post/7213149830444122171