最懂chatGPT Openai的开发库,没有之一,SpringBoot3也能直接用

2,391 阅读3分钟

本文正在参加「技术视角深入 ChatGPT」

导读|时隔1个月chatGPT4、文心一言的发布,AIGC又一次惊艳的出现在大家的视线。按照这个趋势,到年底AIGC将会有一个普遍的持续发展,真希望这一天早到来,为此勇哥也爆肝为该事业做一点微不足道的贡献,发布如下API,帮助开发者们相对遍历的开发和对接openai。因此阅读本文你会了解到最好用的java api、以及官方API的一些弊端是什么,进而少走弯路?以及SpringBoot如何快速对接OpenAi?

1.官方API的问题

Openai官方推荐的JavaApi库是openai-java,该库缺失一些封装的经验,因此使用起来会有一些不便之处:

  • 不能便捷的调整连接超时、读取超时等参数;(这点很重要,因为国内访问网速本身很慢,默认值绝对不够用)
  • 不支持本机代理;(这点也很重要,国内开发者本机按照VPN,但是这个库不能直接使用本机VPN,导致开发者非常痛苦)
  • API设计略有提升之处,比如接口可以默认值,方便开发时重复写一些模型名称;
  • 该库不能直接在SpringBoot中使用;

由于以上原因,勇哥自己重新封装了一下,支持以下功能,希望大家多多Star支持一下:

  • 支持本机代理和代理脚本多种模式,程序自动识别;大大方便开发者使用VPN开发;
  • 支持连接超时、读取超时、代理等参数的灵活设置;
  • 支持固定接口中的参数默认值,方法重复编写;
  • 支持在springboot3 starter自动装配;

2.介绍 openai使用

快速调用OpenAi官方 GPT-3 Api的Java库,更适合国内开发者,因为你懂的。Java libraries for using OpenAI's GPT-3 api.

Git地址 : gitee.com/miukoo/open…

提供APIs Supported APIs

更多的功能后续会持续提供

特色Feature

  • 底层使用OkHttp封装并启用连接池,请求效率高
  • 底层超时参数可以动态调整
  • 自动识别本机的常用代理设置

导入 Importing

<dependency>
    <groupId>cn.gjsm</groupId>
    <artifactId>openai</artifactId>
    <version>0.1.3</version>       
</dependency>

使用 Use

基本使用 Basic

// 实例化请求客户端 Instance the OpenAiClient
OpenAiClient openAiClient = OpenAiClientFactory.createClient(OPENAPI_TOKEN);

// 实例化发送的消息 Instance the message
ChatMessage chatMessage = new ChatMessage();
chatMessage.setRole("user");
chatMessage.setContent("今天天气怎么样?");

// 实例化发送的请求 Instance the request
ChatCompletionRequest request = ChatCompletionRequest.builder()
    .messages(Arrays.asList(chatMessage))
    .model("gpt-3.5-turbo")
    .build();

// 执行请求 Execute request
Call<ChatCompletionResponse> chatCompletion = openAiClient.callChatCompletion(request);
Response<ChatCompletionResponse> response = chatCompletion.execute();

// 解析结果 Analysis results
if(response.isSuccessful()){
    System.out.println(JSON.toJSONString(response.body()));
}

自定义参数 Custom Parameter

OpenAiClient openAiClient =  OpenAiClientFactory.builder()
                .readTimeout(Duration.ofMillis(openAiProperties.getTimeout()))
                .connectTimeout(Duration.ofMillis(openAiProperties.getTimeout()))
                .build()
                .createClient(openAiProperties.getToken());

代理客户端 Proxy Client

OpenAiClient openAiClient =  OpenAiClientFactory.getInstance()
                .createHttpProxyClient(openAiProperties.getToken(),"代理IP","代理端口");

动态刷新Token Refresh Token

框架是直接从环境变量中获取TOKEN,获取的变量名是OPENAPI_TOKEN。

OpenAiClientFactory.refreshToken("您的新TOKEN");

高级 Advanced

在程序启动命令中增加一下参数,可更好的支持本机代理

Add the following parameters to the program startup command to better support the native agent

-Djava.net.useSystemProxies=true

3.介绍 openai-spring-boot-starter使用

本项目是基于SpringBoot3.0封装的OpenAi快速开发类,支持49种场景调用。

Git地址:gitee.com/miukoo/open…

注意:该项目还未提交到中央仓库,因此你需要下载源码到本地安装。

使用步骤

1、导入依赖

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>cn.gjsm</groupId>
            <artifactId>openai-spring</artifactId>
            <version>1.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>cn.gjsm</groupId>
        <artifactId>openai-spring-boot-starter</artifactId>
        <version>1.0.1</version>
    </dependency>
</dependencies>

2、配置秘钥

在application.yml中配置如下参数:

openai:
  token: 你的秘钥
  timeout: 5000 // 超时时间

3、注入使用

@Autowired
OpenAiClient openAiClient;

@Test
public void test(){
   // 实例化发送的消息 Instance the message
    ChatMessage chatMessage = new ChatMessage();
    chatMessage.setRole("user");
    chatMessage.setContent("今天天气怎么样?");

    // 实例化发送的请求 Instance the request
    ChatCompletionRequest request = ChatCompletionRequest.builder()
        .messages(Arrays.asList(chatMessage))
        .model("gpt-3.5-turbo")
        .build();

    // 执行请求 Execute request
    Call<ChatCompletionResponse> chatCompletion = openAiClient.callChatCompletion(request);
    Response<ChatCompletionResponse> response = chatCompletion.execute();

    // 解析结果 Analysis results
    if(response.isSuccessful()){
        System.out.println(JSON.toJSONString(response.body()));
    }
}