Spring Boot 快速接入 ChatGPT

語言: CN / TW / HK

本文正在參加 人工智慧創作者扶持計劃

一、簡介

自從OpenAI-ChatGPT火了之後,圍繞OpenAI-ChatGPT的應用的話題就層出不窮,大模型人工智慧的發展是不可阻擋的趨勢。lucy-chat是Java環境下快速接入OpenAI-ChatGPT大模型人工智慧的Java解決方案,我們無法創造工具,但也要更好的使用工具,該包簡化了接入流程,k開發者可以非常方便的引入並使用ChatGPT提供的相關功能。 

二、快速接入

lucy-chat提供了兩種形式接入服務,完成整合或者獨立部署後可以訪問[部署地址]/doc.html呼叫相關介面。

2.1 建立專案

首先,使用IntelliJ IDEA構建一個Spring Boot工程。

image.png

image.png 接著,我們啟動專案,如果沒有任何的報錯。當我們在瀏覽器中輸入:http://localhost:8080時會輸出如下內容。

image.png  

2.2 Jar引入

在引入任何 Lucy系列依賴之前,需要完成jitpack映象倉庫的配置,如下。

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

然後,我們在Spring Boot專案中新增lucy-chat依賴,當前預設1.0.0-r4。

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

新增依賴後,需要重新整理一下專案才能完成lucy-chat依賴,如下圖。

image.png

依賴完成之後,我們開啟專案的啟動檔案,然後啟用 knife4j 文件,即需要在啟動類上配置 @EnableKnife4j,並將啟動的入口改為LucyChatApplication。

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

使用lucy-chat前,還需要在配置檔案中配置如下檔案資訊。

``` spring.application.name=lucy-chat

執行埠

server.port=8080

swagger 匹配

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

chat-gpt api-key

申請地址 http://platform.openai.com/account/api-keys

openai.chat.key=

chat-gpt proxy host

配置代理地址 請參閱 http://www.v2ex.com/t/921689

openai.chat.host=

連線池最大連線數

forest.max-connections=1000

連線超時時間,單位為毫秒

forest.connect-timeout=30000

資料讀取超時時間,單位為毫秒

forest.read-timeout=30000 ```

要想能夠正常訪問openAi的Api,需要去openAi的官網獲取一個api-key,申請的連結為:

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

image.png

2.3 獨立服務

當然,我們也可以將lucy-chat部署成獨立的服務。首先,需要從開源地址下載專案:

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

接著,修改POM檔案中打包方式,即恢復 相關注釋掉的內容,參考如下。

<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>

然後參考上文的配置檔案相關內容修改相關配置檔案, 將專案中提供的 key為私人 key就可以了。

三、測試

完成配置後,可以訪問[服務地址]/chat/web進入WebChat頁面,可以在其他前端應用中,直接使用Iframe標籤引入。 image.png  

lucy-chat原始碼:http://gitee.com/Kindear/lucy-chat