Skip to content

Commit

Permalink
Merge pull request #116 from Grt1228/1.0.12
Browse files Browse the repository at this point in the history
1.0.12 tokens计算优化、删除模型接口修改、语音接口更新支持官方最新参数
  • Loading branch information
Grt1228 authored Apr 12, 2023
2 parents 517f9db + fd346c7 commit bb22362
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 77 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
it’s an “unofficial" or "community-maintained” library.

这是一个非官方的社区维护的库。
OpenAi官方文档地址:https://platform.openai.com/docs/api-reference
> 原创发布转载注明出处!
## 此项目不仅仅支持chat对话模型,支持openai官方所有api,包括
- [x] tokens计算
- [x] tokens 计算
- [x] Billing 余额查询
- [x] Models 模型检索
- [x] Completions chatgpt对话
Expand Down Expand Up @@ -31,6 +30,7 @@ WebSocket参考:[OpenAIWebSocketEventSourceListener](https://github.com/Grt122
<img src="https://user-images.githubusercontent.com/27008803/225246389-7b452214-f3fe-4a70-bd3e-832a0ed34288.jpg" width="210" height="300" alt="二维码" /> | <img src="https://g-photo.oss-cn-shanghai.aliyuncs.com/hd15.jpg" width="210" height="210" alt="二维码" /> | <img src="https://user-images.githubusercontent.com/27008803/225246581-15e90f78-5438-4637-8e7d-14c68ca13b59.jpg" width="210" height="300" alt="二维码" />
---
## 更新日志
- [x] 1.0.12 tokens计算优化、删除模型接口修改、语音接口更新支持官方最新参数
- [x] 1.0.11 增加新的余额查询接口参考:[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) ,修复tokens计算慢的问题,
- [x] 1.0.10 支持tokens计算:[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) ,更多详细的资料参考文档:[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)
- [x] 1.0.9 支持自定义key使用策略参考:[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java)[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) ,弃用ChatGPTClient,优化Moderation接口
Expand Down Expand Up @@ -72,7 +72,7 @@ OpenAi官方文档地址:https://platform.openai.com/docs/api-reference
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
</dependency>
```
## 2、流式客户端使用示例:
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
<name>chatgpt-java</name>
<description>OpenAI Java SDK, OpenAI Api for Java. ChatGPT Java SDK .</description>
<url>https://www.unfbx.com</url>
Expand Down Expand Up @@ -115,7 +115,7 @@
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
</dependency>
</dependencies>

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/unfbx/chatgpt/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Single<UploadFileResponse> uploadFile(@Part MultipartBody.Part file,
*
* @return Single DeleteResponse
*/
@GET("v1/models/{model}")
@DELETE("v1/models/{model}")
Single<FineTuneDeleteResponse> deleteFineTuneModel(@Path("model") String model);


Expand Down Expand Up @@ -277,26 +277,26 @@ Single<UploadFileResponse> uploadFile(@Part MultipartBody.Part file,
/**
* 语音转文字
*
* @param model 模型
* @param file 语音文件
* @param file 语音文件
* @param requestBodyMap 参数
* @return 文本
*/
@Multipart
@POST("v1/audio/transcriptions")
Single<WhisperResponse> speechToTextTranscriptions(@Part MultipartBody.Part file,
@Part("model") RequestBody model);
@PartMap() Map<String, RequestBody> requestBodyMap);

/**
* 语音翻译:目前仅支持翻译为英文
*
* @param model 模型
* @param file 语音文件
* @param file 语音文件
* @param requestBodyMap 参数
* @return 文本
*/
@Multipart
@POST("v1/audio/translations")
Single<WhisperResponse> speechToTextTranslations(@Part MultipartBody.Part file,
@Part("model") RequestBody model);
@PartMap() Map<String, RequestBody> requestBodyMap);

/**
* 余额查询
Expand Down
63 changes: 48 additions & 15 deletions src/main/java/com/unfbx/chatgpt/OpenAiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import com.unfbx.chatgpt.entity.models.ModelResponse;
import com.unfbx.chatgpt.entity.moderations.Moderation;
import com.unfbx.chatgpt.entity.moderations.ModerationResponse;
import com.unfbx.chatgpt.entity.whisper.Whisper;
import com.unfbx.chatgpt.entity.whisper.Transcriptions;
import com.unfbx.chatgpt.entity.whisper.Translations;
import com.unfbx.chatgpt.entity.whisper.WhisperResponse;
import com.unfbx.chatgpt.exception.BaseException;
import com.unfbx.chatgpt.exception.CommonError;
Expand Down Expand Up @@ -650,16 +651,32 @@ public ChatCompletionResponse chatCompletion(List<Message> messages) {
/**
* 语音转文字
*
* @param model 模型 Whisper.Model
* @param file 语音文件 最大支持25MB mp3, mp4, mpeg, mpga, m4a, wav, webm
* @param transcriptions 参数
* @param file 语音文件 最大支持25MB mp3, mp4, mpeg, mpga, m4a, wav, webm
* @return 语音文本
*/
public WhisperResponse speechToTextTranscriptions(java.io.File file, Whisper.Model model) {
checkSpeechFileSize(file);
public WhisperResponse speechToTextTranscriptions(java.io.File file, Transcriptions transcriptions) {
//文件
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
RequestBody modelBody = RequestBody.create(MediaType.parse("multipart/form-data"), model.getName());
Single<WhisperResponse> whisperResponse = this.openAiApi.speechToTextTranscriptions(multipartBody, modelBody);
//自定义参数
Map<String,RequestBody> requestBodyMap = new HashMap<>();
if (StrUtil.isNotBlank(transcriptions.getLanguage())) {
requestBodyMap.put(Transcriptions.Fields.language, RequestBody.create(MediaType.parse("multipart/form-data"), transcriptions.getLanguage()));
}
if (StrUtil.isNotBlank(transcriptions.getModel())) {
requestBodyMap.put(Transcriptions.Fields.model, RequestBody.create(MediaType.parse("multipart/form-data"), transcriptions.getModel()));
}
if (StrUtil.isNotBlank(transcriptions.getPrompt())) {
requestBodyMap.put(Transcriptions.Fields.prompt, RequestBody.create(MediaType.parse("multipart/form-data"), transcriptions.getPrompt()));
}
if (StrUtil.isNotBlank(transcriptions.getResponseFormat())) {
requestBodyMap.put(Transcriptions.Fields.responseFormat, RequestBody.create(MediaType.parse("multipart/form-data"), transcriptions.getResponseFormat()));
}
if (Objects.nonNull(transcriptions.getTemperature())) {
requestBodyMap.put(Transcriptions.Fields.temperature, RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(transcriptions.getTemperature())));
}
Single<WhisperResponse> whisperResponse = this.openAiApi.speechToTextTranscriptions(multipartBody, requestBodyMap);
return whisperResponse.blockingGet();
}

Expand All @@ -670,24 +687,39 @@ public WhisperResponse speechToTextTranscriptions(java.io.File file, Whisper.Mod
* @return 语音文本
*/
public WhisperResponse speechToTextTranscriptions(java.io.File file) {
return this.speechToTextTranscriptions(file, Whisper.Model.WHISPER_1);
Transcriptions transcriptions = Transcriptions.builder().build();
return this.speechToTextTranscriptions(file, transcriptions);

}


/**
* 语音翻译:目前仅支持翻译为英文
*
* @param model 模型 Whisper.Model
* @param file 语音文件 最大支持25MB mp3, mp4, mpeg, mpga, m4a, wav, webm
* @param translations 参数
* @param file 语音文件 最大支持25MB mp3, mp4, mpeg, mpga, m4a, wav, webm
* @return 翻译后文本
*/
public WhisperResponse speechToTextTranslations(java.io.File file, Whisper.Model model) {
checkSpeechFileSize(file);
public WhisperResponse speechToTextTranslations(java.io.File file, Translations translations) {
//文件
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part multipartBody = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
RequestBody modelBody = RequestBody.create(MediaType.parse("multipart/form-data"), model.getName());
Single<WhisperResponse> whisperResponse = this.openAiApi.speechToTextTranslations(multipartBody, modelBody);
//自定义参数
Map<String,RequestBody> requestBodyMap = new HashMap<>();

if (StrUtil.isNotBlank(translations.getModel())) {
requestBodyMap.put(Translations.Fields.model, RequestBody.create(MediaType.parse("multipart/form-data"), translations.getModel()));
}
if (StrUtil.isNotBlank(translations.getPrompt())) {
requestBodyMap.put(Translations.Fields.prompt, RequestBody.create(MediaType.parse("multipart/form-data"), translations.getPrompt()));
}
if (StrUtil.isNotBlank(translations.getResponseFormat())) {
requestBodyMap.put(Translations.Fields.responseFormat, RequestBody.create(MediaType.parse("multipart/form-data"), translations.getResponseFormat()));
}
if (Objects.nonNull(translations.getTemperature())) {
requestBodyMap.put(Translations.Fields.temperature, RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(translations.getTemperature())));
}
Single<WhisperResponse> whisperResponse = this.openAiApi.speechToTextTranslations(multipartBody, requestBodyMap);
return whisperResponse.blockingGet();
}

Expand All @@ -698,7 +730,8 @@ public WhisperResponse speechToTextTranslations(java.io.File file, Whisper.Model
* @return 翻译后文本
*/
public WhisperResponse speechToTextTranslations(java.io.File file) {
return this.speechToTextTranslations(file, Whisper.Model.WHISPER_1);
Translations translations = Translations.builder().build();
return this.speechToTextTranslations(file, translations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.unfbx.chatgpt.entity.fineTune;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.unfbx.chatgpt.entity.models.Permission;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class FineTuneDeleteResponse implements Serializable {
Expand All @@ -14,16 +11,6 @@ public class FineTuneDeleteResponse implements Serializable {

private String object;

private long created;

@JsonProperty("owned_by")
private String ownedBy;

@JsonProperty("permission")
private List<Permission> permission;

private String root;

private String parent;
private boolean deleted;

}
43 changes: 43 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/whisper/Transcriptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.unfbx.chatgpt.entity.whisper;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.FieldNameConstants;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Transcriptions extends Whisper {
/**
* 模型目前只支持这一种:WHISPER_1
*/
@Builder.Default
private String model = Whisper.Model.WHISPER_1.getName();
/**
* 提示语,需要与语音语言匹配
*/
private String prompt;
/**
* 输出的格式,采用以下选项之一:json、text、srt、verbose_json 或 vtt。
* 默认值:json
*/
@JsonProperty("response_format")
@Builder.Default
private String responseFormat = ResponseFormat.JSON.getName();
/**
* 温度控制随机效果:0-1,值越大输出更加随机
* 默认值:0
*/
@Builder.Default
private Double temperature = 0d;
/**
* 输入音频的语言,以 ISO-639-1 格式提供输入语言将提高准确性和延迟。
* 参考:<a href=https://baike.baidu.com/item/ISO%20639-1/8292914>ISO-639-1</a>
*/
private String language;

}
38 changes: 38 additions & 0 deletions src/main/java/com/unfbx/chatgpt/entity/whisper/Translations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.unfbx.chatgpt.entity.whisper;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.FieldNameConstants;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Translations {
/**
* 模型目前只支持这一种:WHISPER_1
*/
@Builder.Default
private String model = Whisper.Model.WHISPER_1.getName();
/**
* 提示语,需要与语音语言匹配
*/
private String prompt;
/**
* 输出的格式,采用以下选项之一:json、text、srt、verbose_json 或 vtt。
* 默认值:json
*/
@JsonProperty("response_format")
@Builder.Default
private String responseFormat = Whisper.ResponseFormat.JSON.getName();
/**
* 温度控制随机效果:0-1,值越大输出更加随机
* 默认值:0
*/
@Builder.Default
private double temperature = 0;

}
16 changes: 14 additions & 2 deletions src/main/java/com/unfbx/chatgpt/entity/whisper/Whisper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@
* 描述:语音转文字
*
* @author https:www.unfbx.com
* @since 2023-03-02
* @since 2023-03-02
*/
@Data
public class Whisper implements Serializable {


@Getter
@AllArgsConstructor
public enum Model{
public enum Model {
WHISPER_1("whisper-1"),
;
private String name;
}

@Getter
@AllArgsConstructor
public enum ResponseFormat {
JSON("json"),
TEXT("text"),
SRT("srt"),
VERBOSE_JSON("verbose_json"),
VTT("vtt"),
;
private String name;
}
}
Loading

0 comments on commit bb22362

Please sign in to comment.