diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7f3e5dc..696f680 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,6 +1,8 @@ name: Maven Central Repo Deployment # 触发脚本的事件 这里为发布release之后触发 on: + # 手动触发事件 + workflow_dispatch: release: types: [released] # 定义一个发行任务 @@ -20,8 +22,8 @@ jobs: with: java-version: 1.8 server-id: sonatype-nexus-staging - server-username: ${{ secrets.OSSRH_USER }} - server-password: ${{ secrets.OSSRH_PASSWORD }} + server-username: ${{ secrets.OSSRH_USER2 }} + server-password: ${{ secrets.OSSRH_PASSWORD2 }} gpg-passphrase: ${{ secrets.GPG_PASSWORD }} # 3. 发布到Maven中央仓库 - name: Publish to Maven Central Repo @@ -30,5 +32,5 @@ jobs: with: gpg_private_key: ${{ secrets.GPG_SECRET }} gpg_passphrase: ${{ secrets.GPG_PASSWORD }} - nexus_username: ${{ secrets.OSSRH_USER }} - nexus_password: ${{ secrets.OSSRH_PASSWORD }} + nexus_username: ${{ secrets.OSSRH_USER2 }} + nexus_password: ${{ secrets.OSSRH_PASSWORD2 }} diff --git a/README.md b/README.md index 0e10ac1..52383c8 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,6 @@ OpenAI ChatGPT 的SDK。觉得不错请右上角Star [GPT API 额度购买(微信)](https://work.weixin.qq.com/kfid/kfc6913bb4906e0e597) -点击👇🏻传送链接,购买云服务器炼丹: - -- [**阿里云服务器特惠**](https://51015.cn/ss/3vpds) -- [**【腾讯云】服务器,低至4.2元/月**](https://curl.qcloud.com/NiGEWRdn) 选择 GPU 云服务器 # 功能特性 @@ -59,13 +55,13 @@ maven com.github.plexpt chatgpt - 4.4.0 + 5.1.0 ``` gradle ``` -implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.4.0' +implementation group: 'com.github.plexpt', name: 'chatgpt', version: '5.1.0' ``` diff --git a/README_en.md b/README_en.md index 9555487..dd9b4a1 100644 --- a/README_en.md +++ b/README_en.md @@ -39,14 +39,14 @@ SDK for OpenAI ChatGPT. If you find it helpful, please give it a star in the upp com.github.plexpt chatgpt - 4.4.0 + 5.1.0 ``` #### gradle ``` -implementation group: 'com.github.plexpt', name: 'chatgpt', version: '4.4.0' +implementation group: 'com.github.plexpt', name: 'chatgpt', version: '5.1.0' ``` ### Quick Start diff --git a/pom.xml b/pom.xml index f849966..650c9c2 100644 --- a/pom.xml +++ b/pom.xml @@ -5,9 +5,9 @@ com.github.plexpt chatgpt - 5.0.0 + 5.1.0 chatgpt - ChatGPT4.0、 ChatGPT Java SDK. + Openai ChatGPT Java SDK. https://chat.plexpt.com @@ -132,7 +132,7 @@ com.knuddels jtokkit - 1.0.0 + 1.1.0 @@ -199,12 +199,12 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.13 + 1.7.0 true ossrh https://oss.sonatype.org/ - false + true @@ -214,6 +214,12 @@ org.sonatype.plugins nexus-staging-maven-plugin + true + + ossrh + https://oss.sonatype.org/ + true + org.apache.maven.plugins diff --git a/src/main/java/com/plexpt/chatgpt/ChatGPT.java b/src/main/java/com/plexpt/chatgpt/ChatGPT.java index 41937fc..b0c49d0 100644 --- a/src/main/java/com/plexpt/chatgpt/ChatGPT.java +++ b/src/main/java/com/plexpt/chatgpt/ChatGPT.java @@ -7,7 +7,10 @@ import cn.hutool.http.Header; import com.plexpt.chatgpt.api.Api; import com.plexpt.chatgpt.entity.BaseResponse; +import com.plexpt.chatgpt.entity.DeleteResponse; +import com.plexpt.chatgpt.entity.FileResponse; import com.plexpt.chatgpt.entity.audio.AudioResponse; +import com.plexpt.chatgpt.entity.audio.SpeechRequest; import com.plexpt.chatgpt.entity.audio.Transcriptions; import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse; import com.plexpt.chatgpt.entity.billing.SubscriptionData; @@ -32,6 +35,7 @@ import retrofit2.converter.jackson.JacksonConverterFactory; import java.io.File; +import java.io.InputStream; import java.math.BigDecimal; import java.net.Proxy; import java.util.*; @@ -195,8 +199,7 @@ public EmbeddingResult createEmbeddings(String input, String user) { public ImagesRensponse imageGeneration(Generations generations) { - Single imagesRensponse = - this.apiClient.imageGenerations(generations); + Single imagesRensponse = this.apiClient.imageGenerations(generations); return imagesRensponse.blockingGet(); } @@ -229,6 +232,13 @@ public AudioResponse audioTranscription(File audio, Transcriptions transcription return audioResponse.blockingGet(); } + public InputStream audioSpeech(SpeechRequest speechRequest) { + Single audioResponse = this.apiClient.audioSpeech(speechRequest); + ResponseBody response = audioResponse.blockingGet(); + InputStream stream = response.byteStream(); + return stream; + } + public AudioResponse audioTranslation(File audio, Transcriptions transcriptions) { RequestBody a = RequestBody.create(MediaType.parse("multipart/form-data;charset=UTF-8"), audio); MultipartBody.Part aPart = MultipartBody.Part.createFormData("image", audio.getName(), a); @@ -282,4 +292,70 @@ public CreditGrantsResponse creditGrants() { Single creditGrants = this.apiClient.creditGrants(); return creditGrants.blockingGet(); } + + /** + * 列出文件 + * List files + */ + public BaseResponse listFiles() { + Single> files = this.apiClient.listFiles(); + return files.blockingGet(); + } + + /** + * 上传文件 + * Upload file + * + * @param purpose 文件用途 + * The purpose of the file + * @param file 文件部分 + * The file part + * @return 文件响应 + * File response + */ + public FileResponse uploadFile(String purpose, MultipartBody.Part file) { + RequestBody purposeBody = RequestBody.create(MultipartBody.FORM, purpose); + Single files = this.apiClient.uploadFile(purposeBody, file); + return files.blockingGet(); + } + + /** + * 删除文件 + * Delete file + * + * @param fileId 文件ID + * The file ID + * @return 删除响应 + * Delete response + */ + public DeleteResponse deleteFile(String fileId) { + return this.apiClient.deleteFile(fileId).blockingGet(); + } + + /** + * 检索文件 + * Retrieve file + * + * @param fileId 文件ID + * The file ID + * @return 文件响应 + * File response + */ + public FileResponse retrieveFile(String fileId) { + return this.apiClient.retrieveFile(fileId).blockingGet(); + } + + /** + * 检索文件内容 + * Retrieve file content + * + * @param fileId 文件ID + * The file ID + * @return 文件内容响应 + * File content response + */ + public ResponseBody retrieveFileContent(String fileId) { + return this.apiClient.retrieveFileContent(fileId).blockingGet(); + } + } diff --git a/src/main/java/com/plexpt/chatgpt/api/Api.java b/src/main/java/com/plexpt/chatgpt/api/Api.java index a2b5fbf..c207745 100644 --- a/src/main/java/com/plexpt/chatgpt/api/Api.java +++ b/src/main/java/com/plexpt/chatgpt/api/Api.java @@ -1,6 +1,10 @@ package com.plexpt.chatgpt.api; +import com.plexpt.chatgpt.entity.BaseResponse; +import com.plexpt.chatgpt.entity.DeleteResponse; +import com.plexpt.chatgpt.entity.FileResponse; import com.plexpt.chatgpt.entity.audio.AudioResponse; +import com.plexpt.chatgpt.entity.audio.SpeechRequest; import com.plexpt.chatgpt.entity.audio.Transcriptions; import com.plexpt.chatgpt.entity.billing.CreditGrantsResponse; import com.plexpt.chatgpt.entity.billing.SubscriptionData; @@ -15,31 +19,37 @@ import com.plexpt.chatgpt.entity.images.Variations; import io.reactivex.Single; import okhttp3.MultipartBody; +import okhttp3.RequestBody; +import okhttp3.ResponseBody; import retrofit2.http.*; +import java.io.File; /** - * + * API接口 + * API interface */ public interface Api { String DEFAULT_API_HOST = "https://api.openai.com/"; - /** - * chat + * 聊天 + * Chat */ @POST("v1/chat/completions") Single chatCompletion(@Body ChatCompletion chatCompletion); /** - * image_generations + * 图像生成 + * Image generations */ @POST("v1/images/generations") Single imageGenerations(@Body Generations generations); /** - * image_edits + * 图像编辑 + * Image edits */ @Multipart @POST("v1/images/edits") @@ -47,9 +57,9 @@ Single imageEdits(@Part() MultipartBody.Part image, @Part() MultipartBody.Part mask, @PartMap Edits edits); - /** - * image_variations + * 图像变体 + * Image variations */ @Multipart @POST("v1/images/variations") @@ -57,7 +67,15 @@ Single imageVariations(@Part() MultipartBody.Part image, @PartMap Variations variations); /** - * audio_transcriptions + * 生成语音 + * Create speech + */ + @POST("v1/audio/speech") + Single audioSpeech(@Body SpeechRequest speechRequest); + + /** + * 音频转录 + * Audio transcriptions */ @Multipart @POST("v1/audio/transcriptions") @@ -65,38 +83,78 @@ Single audioTranscriptions(@Part() MultipartBody.Part audio, @PartMap Transcriptions transcriptions); /** - * audio_translations + * 音频翻译 + * Audio translations */ @Multipart @POST("v1/audio/translations") Single audioTranslations(@Part() MultipartBody.Part audio, @PartMap Transcriptions transcriptions); - /** * 余额查询 + * Credit grants query */ @GET("dashboard/billing/credit_grants") Single creditGrants(); /** - * 余额查询 + * 订阅查询 + * Subscription query */ @GET("v1/dashboard/billing/subscription") Single subscription(); /** - * 余额查询 + * 使用情况查询 + * Usage query */ @GET("v1/dashboard/billing/usage") Single usage(@Query("start_date") String startDate, @Query("end_date") String endDate); - /** * 生成向量 + * Create embeddings */ @POST("v1/embeddings") Single createEmbeddings(@Body EmbeddingRequest request); - + + /** + * 列出文件 + * List files + */ + @GET("/v1/files") + Single> listFiles(); + + /** + * 上传文件 + * Upload file + */ + @Multipart + @POST("/v1/files") + Single uploadFile(@Part("purpose") RequestBody purpose, @Part MultipartBody.Part file); + + /** + * 删除文件 + * Delete file + */ + @DELETE("/v1/files/{file_id}") + Single deleteFile(@Path("file_id") String fileId); + + /** + * 检索文件 + * Retrieve file + */ + @GET("/v1/files/{file_id}") + Single retrieveFile(@Path("file_id") String fileId); + + /** + * 检索文件内容 + * Retrieve file content + */ + @Streaming + @GET("/v1/files/{file_id}/content") + Single retrieveFileContent(@Path("file_id") String fileId); + } diff --git a/src/main/java/com/plexpt/chatgpt/entity/DeleteResponse.java b/src/main/java/com/plexpt/chatgpt/entity/DeleteResponse.java new file mode 100644 index 0000000..04964aa --- /dev/null +++ b/src/main/java/com/plexpt/chatgpt/entity/DeleteResponse.java @@ -0,0 +1,33 @@ +package com.plexpt.chatgpt.entity; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.Data; + +/** + * 删除对象时的响应 + * A response when deleting an object + */ +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +public class DeleteResponse { + + /** + * 对象的ID + * The id of the object. + */ + private String id; + + /** + * 删除的对象类型,例如 "file" 或 "model" + * The type of object deleted, for example "file" or "model" + */ + private String object; + + /** + * 如果成功删除则为 true + * True if successfully deleted + */ + private boolean deleted; + + +} diff --git a/src/main/java/com/plexpt/chatgpt/entity/FileResponse.java b/src/main/java/com/plexpt/chatgpt/entity/FileResponse.java new file mode 100644 index 0000000..a848433 --- /dev/null +++ b/src/main/java/com/plexpt/chatgpt/entity/FileResponse.java @@ -0,0 +1,70 @@ +package com.plexpt.chatgpt.entity; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +/** + * 上传到 OpenAI 的文件 + * A file uploaded to OpenAI + *

+ * https://beta.openai.com/docs/api-reference/files + */ +@Data +@JsonIgnoreProperties(ignoreUnknown = true) +public class FileResponse { + + /** + * 文件的唯一ID + * The unique id of this file. + */ + private String id; + + /** + * 返回的对象类型,应为 "file" + * The type of object returned, should be "file". + */ + private String object; + + /** + * 文件大小(以字节为单位) + * File size in bytes. + */ + private Long bytes; + + /** + * 创建时间(以秒为单位的纪元时间) + * The creation time in epoch seconds. + */ + @JsonProperty("created_at") + private Long createdAt; + + /** + * 文件名 + * The name of the file. + */ + private String filename; + + /** + * 文件用途的描述 + * Description of the file's purpose. + */ + private String purpose; + + /** + * 文件的当前状态,可以是 uploaded, processed, pending, error, deleting 或 deleted + * The current status of the file, which can be either uploaded, processed, pending, error, deleting or deleted. + */ + private String status; + + /** + * 文件状态的附加详细信息 + * 如果文件处于错误状态,这将包括描述错误的消息 + * Additional details about the status of the file. + * If the file is in the error state, this will include a message describing the error. + */ + @JsonProperty("status_details") + private String statusDetails; + + +} diff --git a/src/main/java/com/plexpt/chatgpt/entity/audio/SpeechRequest.java b/src/main/java/com/plexpt/chatgpt/entity/audio/SpeechRequest.java new file mode 100644 index 0000000..828fd5a --- /dev/null +++ b/src/main/java/com/plexpt/chatgpt/entity/audio/SpeechRequest.java @@ -0,0 +1,66 @@ +package com.plexpt.chatgpt.entity.audio; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * 语音请求对象 + * Speech request object + */ +@Data +@Builder +@Slf4j +@AllArgsConstructor +@NoArgsConstructor(force = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class SpeechRequest { + + /** + * 模型 + * Model https://platform.openai.com/docs/models/tts + * + * One of the available TTS models: tts-1 or tts-1-hd + */ + @Builder.Default + private String model = "tts-1"; + + /** + * 输入文本 + * Input text + * The text to generate audio for. The maximum length is 4096 characters. + */ + private String input; + + /** + * 声音 + * Voice + * The voice to use when generating the audio. Supported voices are alloy, echo, fable, onyx, nova, and shimmer. + * Previews of the voices are available in the Text to speech guide. + * + * https://platform.openai.com/docs/guides/text-to-speech/voice-options + */ + @Builder.Default + private String voice ="alloy"; + + /** + * 响应格式 + * Response format + * The format to audio in. Supported formats are mp3, opus, aac, flac, wav, and pcm. + */ + private String response_format; + + /** + * 速度 + * Speed + * + * The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default. + */ + private Double speed; + +} diff --git a/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletion.java b/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletion.java index db1215b..3edea9a 100644 --- a/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletion.java +++ b/src/main/java/com/plexpt/chatgpt/entity/chat/ChatCompletion.java @@ -169,18 +169,12 @@ public interface Model { * gpt-3.5-turbo */ String GPT_3_5_TURBO = "gpt-3.5-turbo"; - String GPT_3_5_TURBO_16K = "gpt-3.5-turbo-16k"; - String GPT_3_5_TURBO_INSTRUCT = "gpt-3.5-turbo-instruct"; /** * GPT4.0 */ String GPT4 = "gpt-4"; - String GPT4V = "gpt-4-vision-preview"; String GPT4o = "gpt-4o"; - /** - * GPT4.0 超长上下文 - */ - String GPT_4_32K = "gpt-4-32k"; + String GPT4oMini = "gpt-4o-mini"; }