Skip to content

Commit

Permalink
files api
Browse files Browse the repository at this point in the history
  • Loading branch information
PlexPt committed Jul 31, 2024
1 parent bc06364 commit 64cfa61
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 3 deletions.
80 changes: 78 additions & 2 deletions src/main/java/com/plexpt/chatgpt/ChatGPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -195,8 +199,7 @@ public EmbeddingResult createEmbeddings(String input, String user) {


public ImagesRensponse imageGeneration(Generations generations) {
Single<ImagesRensponse> imagesRensponse =
this.apiClient.imageGenerations(generations);
Single<ImagesRensponse> imagesRensponse = this.apiClient.imageGenerations(generations);
return imagesRensponse.blockingGet();
}

Expand Down Expand Up @@ -229,6 +232,13 @@ public AudioResponse audioTranscription(File audio, Transcriptions transcription
return audioResponse.blockingGet();
}

public InputStream audioSpeech(SpeechRequest speechRequest) {
Single<ResponseBody> 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);
Expand Down Expand Up @@ -282,4 +292,70 @@ public CreditGrantsResponse creditGrants() {
Single<CreditGrantsResponse> creditGrants = this.apiClient.creditGrants();
return creditGrants.blockingGet();
}

/**
* 列出文件
* List files
*/
public BaseResponse<FileResponse> listFiles() {
Single<BaseResponse<FileResponse>> 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<FileResponse> 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();
}

}
46 changes: 45 additions & 1 deletion src/main/java/com/plexpt/chatgpt/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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;
Expand All @@ -16,8 +19,12 @@
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
Expand Down Expand Up @@ -64,7 +71,7 @@ Single<ImagesRensponse> imageVariations(@Part() MultipartBody.Part image,
* Create speech
*/
@POST("v1/audio/speech")
Single<AudioResponse> audioSpeech(@Body SpeechRequest speechRequest);
Single<ResponseBody> audioSpeech(@Body SpeechRequest speechRequest);

/**
* 音频转录
Expand Down Expand Up @@ -113,4 +120,41 @@ Single<UseageResponse> usage(@Query("start_date") String startDate,
@POST("v1/embeddings")
Single<EmbeddingResult> createEmbeddings(@Body EmbeddingRequest request);

/**
* 列出文件
* List files
*/
@GET("/v1/files")
Single<BaseResponse<FileResponse>> listFiles();

/**
* 上传文件
* Upload file
*/
@Multipart
@POST("/v1/files")
Single<FileResponse> uploadFile(@Part("purpose") RequestBody purpose, @Part MultipartBody.Part file);

/**
* 删除文件
* Delete file
*/
@DELETE("/v1/files/{file_id}")
Single<DeleteResponse> deleteFile(@Path("file_id") String fileId);

/**
* 检索文件
* Retrieve file
*/
@GET("/v1/files/{file_id}")
Single<FileResponse> retrieveFile(@Path("file_id") String fileId);

/**
* 检索文件内容
* Retrieve file content
*/
@Streaming
@GET("/v1/files/{file_id}/content")
Single<ResponseBody> retrieveFileContent(@Path("file_id") String fileId);

}
33 changes: 33 additions & 0 deletions src/main/java/com/plexpt/chatgpt/entity/DeleteResponse.java
Original file line number Diff line number Diff line change
@@ -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;


}
70 changes: 70 additions & 0 deletions src/main/java/com/plexpt/chatgpt/entity/FileResponse.java
Original file line number Diff line number Diff line change
@@ -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
* <p>
* 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;


}

0 comments on commit 64cfa61

Please sign in to comment.