diff --git a/README.md b/README.md index a7bf90e..7821df4 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ This Java library provides a convenient way to interact with OpenAI's API for bo - [Easy Vision API](#easy-vision-api) or [Vision API](#vision-api) (original provided by OpenAI) - [Speech API](#speech-api) - [Easy Transcription API](#easy-transcription-api) or [Transcription API](#transcription-api) (original provided by OpenAI) +- [Image generation API / DALL-E](#image-generation-api-/-DALL-E) + ### Asynchronous - [Async Chat Completion API](#async-chat-completion-api) @@ -192,9 +194,10 @@ Click [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/jav Transcription API can be used like this, feel free to tweak- ```java -EasyTranscriptionRequest request = new EasyTranscriptionRequest(); -request.setFilepath("PATH-TO-AUDIO-FILE"); -request.setModel("whisper-1"); +EasyTranscriptionRequest request = EasyTranscriptionRequest.builder() + .filepath("/Users/namankhurpia/Desktop/audio.mp3") + .model("whisper-1") + .build(); ResponseBody response = new EasyTranscriptionService().EasyTranscription("OPENAI_KEY", request); ``` @@ -202,7 +205,11 @@ ResponseBody response = new EasyTranscriptionService().EasyTranscription("OPENAI Click [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java) to jump to the code example. -## Transcription API (original - provided by OpenAI) +## Transcription API + +### Original Transcription API by OpenAI +Transcription API can be used like this, feel free to tweak- + ```java File audioFile = new File("/Users/namankhurpia/Desktop/audio.mp3"); @@ -224,6 +231,21 @@ ResponseBody response = new EasyopenaiService(new DAOImpl()).createTranscription Click [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java) to jump to the code example. +## Image Generation API / DALL-E + +Image Generation API can be used like this, feel free to tweak- + +```java +ImageRequest imageRequest = ImageRequest.builder() + .prompt("Generate a cute dog playing with frizbee") + .model("dall-e-2") + .build(); + +ImageResponse response = new EasyopenaiService(new DAOImpl()).createImage("OPENAI_KEY",imageRequest); +``` +Click [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java) to jump to the code example. + + ## Async Chat Completion API To use the Chat Completion API asynchronously, follow these steps: diff --git a/src/main/java/io/github/namankhurpia/DAO/DAOImpl.java b/src/main/java/io/github/namankhurpia/DAO/DAOImpl.java index 4a6cf9a..b68195a 100644 --- a/src/main/java/io/github/namankhurpia/DAO/DAOImpl.java +++ b/src/main/java/io/github/namankhurpia/DAO/DAOImpl.java @@ -7,6 +7,8 @@ import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse; import io.github.namankhurpia.Pojo.Completion.CompletionRequest; import io.github.namankhurpia.Pojo.Completion.CompletionResponse; +import io.github.namankhurpia.Pojo.Image.ImageRequest; +import io.github.namankhurpia.Pojo.Image.ImageResponse; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse; @@ -38,6 +40,8 @@ public class DAOImpl implements DaoInterface { VisionApiResponse visionApiResponseObj; ResponseBody responseBodyObj; + + ImageResponse imageResponse; private static Logger LOGGER = LoggerFactory.getLogger(DAOImpl.class); RetrofitApiInterface retrofitApiInterfaceObj; @@ -238,8 +242,33 @@ public ResponseBody createTranscriptions(String accessToken, MultipartBody.Part return responseBodyObj; } + @Override + public ImageResponse createImage(String accessToken, ImageRequest imageRequest) throws IOException { + retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class); + LOGGER.info("making req" + accessToken + " with request "+ imageRequest); + Call call = retrofitApiInterfaceObj.createImage("Bearer "+ accessToken, imageRequest); + Response response = call.execute(); + + if(response.isSuccessful()) + { + imageResponse= response.body(); + LOGGER.info("Correct response" + imageResponse.toString()); + } + else { + int httpStatusCode = response.code(); + String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body"; + String errorBodyString = response.errorBody().string(); + System.out.println("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBodyString); + + throw new MalformedRequestException(errorBody, new Throwable(errorBody)); + + + } + + return imageResponse; + } } diff --git a/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java b/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java index d0d0721..2ae7cb2 100644 --- a/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java +++ b/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java @@ -4,6 +4,8 @@ import io.github.namankhurpia.Pojo.ChatCompletion.Message; import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest; import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse; +import io.github.namankhurpia.Pojo.Image.ImageRequest; +import io.github.namankhurpia.Pojo.Image.ImageResponse; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse; import io.github.namankhurpia.Pojo.MyModels.EasyVisionRequest; @@ -37,7 +39,8 @@ public static void main(String[] args) throws IOException, ExecutionException, I //runEasyChatCompletionSingleInstance(); //TestForSpeech(); //TestForTranscription(); - TestForEasyTranscription(); + //TestForEasyTranscription(); + TestForImageGeneration(); } @@ -254,14 +257,28 @@ public static void TestForTranscription() throws IOException { public static void TestForEasyTranscription() throws IOException { ArrayList keys = readKeys(); - EasyTranscriptionRequest request = new EasyTranscriptionRequest(); - request.setFilepath("/Users/namankhurpia/Desktop/audio.mp3"); - request.setModel("whisper-1"); + EasyTranscriptionRequest request = EasyTranscriptionRequest.builder() + .filepath("/Users/namankhurpia/Desktop/audio.mp3") + .model("whisper-1") + .build(); ResponseBody response = new EasyTranscriptionService().EasyTranscription(keys.get(0), request); System.out.println(response.string()); } + public static void TestForImageGeneration()throws IOException{ + ArrayList keys = readKeys(); + + ImageRequest imageRequest = ImageRequest.builder() + .prompt("Generate a cute dog playing with frizbee") + .model("dall-e-2") + .build(); + + ImageResponse response = new EasyopenaiService(new DAOImpl()).createImage(keys.get(0),imageRequest); + System.out.println(response); + + } + diff --git a/src/main/java/io/github/namankhurpia/Interfaces/DaoInterface.java b/src/main/java/io/github/namankhurpia/Interfaces/DaoInterface.java index a4c2791..290b314 100644 --- a/src/main/java/io/github/namankhurpia/Interfaces/DaoInterface.java +++ b/src/main/java/io/github/namankhurpia/Interfaces/DaoInterface.java @@ -4,6 +4,8 @@ import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse; import io.github.namankhurpia.Pojo.Completion.CompletionRequest; import io.github.namankhurpia.Pojo.Completion.CompletionResponse; +import io.github.namankhurpia.Pojo.Image.ImageRequest; +import io.github.namankhurpia.Pojo.Image.ImageResponse; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse; import io.github.namankhurpia.Pojo.Speech.SpeechRequest; @@ -12,8 +14,10 @@ import okhttp3.MultipartBody; import okhttp3.RequestBody; import okhttp3.ResponseBody; +import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.Header; +import retrofit2.http.POST; import retrofit2.http.Part; import java.io.IOException; @@ -35,5 +39,7 @@ public interface DaoInterface { ResponseBody createTranscriptions(@Header("Authorization")String accessToken, @Part MultipartBody.Part file, @Part RequestBody model, @Part RequestBody language, @Part RequestBody prompt, @Part RequestBody response_format, @Part RequestBody temperature ) throws IOException; + ImageResponse createImage(@Header("Authorization")String accessToken, ImageRequest imageRequest) throws IOException; + } diff --git a/src/main/java/io/github/namankhurpia/Interfaces/RetrofitApiInterface.java b/src/main/java/io/github/namankhurpia/Interfaces/RetrofitApiInterface.java index bd55856..4f6d358 100644 --- a/src/main/java/io/github/namankhurpia/Interfaces/RetrofitApiInterface.java +++ b/src/main/java/io/github/namankhurpia/Interfaces/RetrofitApiInterface.java @@ -3,6 +3,8 @@ import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse; import io.github.namankhurpia.Pojo.Completion.CompletionRequest; import io.github.namankhurpia.Pojo.Completion.CompletionResponse; +import io.github.namankhurpia.Pojo.Image.ImageRequest; +import io.github.namankhurpia.Pojo.Image.ImageResponse; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse; import io.github.namankhurpia.Pojo.Speech.SpeechRequest; @@ -54,4 +56,11 @@ Call createTranscriptions(@Header("Authorization")String accessTok @Part("response_format") RequestBody response_format, @Part("temperature")RequestBody temperature ) throws IOException; + @POST("/v1/images/generations") + Call createImage(@Header("Authorization")String accessToken, @Body ImageRequest imageRequest) throws IOException; + + @POST(".v1/images/edits") + @Multipart + Call createImageEdit(@Header("Authorization")String accessToken, @Part MultipartBody.Part image, @Part("prompt") RequestBody prompt ); + } diff --git a/src/main/java/io/github/namankhurpia/Pojo/ChatCompletion/ChatCompletionRequest.java b/src/main/java/io/github/namankhurpia/Pojo/ChatCompletion/ChatCompletionRequest.java index ab80d69..300ee45 100644 --- a/src/main/java/io/github/namankhurpia/Pojo/ChatCompletion/ChatCompletionRequest.java +++ b/src/main/java/io/github/namankhurpia/Pojo/ChatCompletion/ChatCompletionRequest.java @@ -45,6 +45,20 @@ public class ChatCompletionRequest { @JsonProperty("logit_bias") Map logitBias; + /** + * Whether to return log probabilities of the output tokens or not. If true, returns the log probabilities of each output token returned in the content of message. This option is currently not available on the gpt-4-vision-preview model. + */ + @JsonProperty("logprobs") + boolean logprobs; + + /** + * An integer between 0 and 5 specifying the number of most likely tokens to return at each token position, each with an associated log probability. logprobs must be set to true if this parameter is used. + */ + @JsonProperty("top_logprobs") + Integer topLogprobs; + + + /** * The maximum number of tokens allowed for the generated answer. By default, the number of tokens the model can return will * be (4096 - prompt tokens). @@ -65,19 +79,17 @@ public class ChatCompletionRequest { Double presencePenalty; /** - * FOR - response_format - * An object specifying the format that the model must output. - * - * Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON. * - * Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length. */ - String type; + @JsonProperty("response_format") + Object responseFormat; + + /** * This feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result. Determinism is not guaranteed, and you should refer to the system_fingerprint response parameter to monitor changes in the backend. */ - Integer seed; + Double seed; /** * Up to 4 sequences where the API will stop generating further tokens. diff --git a/src/main/java/io/github/namankhurpia/Pojo/Image/ImageRequest.java b/src/main/java/io/github/namankhurpia/Pojo/Image/ImageRequest.java new file mode 100644 index 0000000..672c383 --- /dev/null +++ b/src/main/java/io/github/namankhurpia/Pojo/Image/ImageRequest.java @@ -0,0 +1,54 @@ +package io.github.namankhurpia.Pojo.Image; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; +import lombok.*; + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Data +public class ImageRequest { + + /** + * This input is your prompt which needs to be tested against moderations API + */ + @NonNull + @JsonProperty("prompt") + @SerializedName("prompt") + public String prompt; + + /** + * Defaults to dall-e-2 + * dall-e-3 + * + */ + @JsonProperty("model") + @SerializedName("model") + public String model; + + @JsonProperty("n") + @SerializedName("n") + Integer n; + + @JsonProperty("quality") + @SerializedName("quality") + String quality; + + @JsonProperty("response_format") + @SerializedName("response_format") + String response_format; + + @JsonProperty("size") + @SerializedName("size") + String size; + + @JsonProperty("style") + @SerializedName("style") + String style; + + @JsonProperty("user") + @SerializedName("user") + String user; + +} diff --git a/src/main/java/io/github/namankhurpia/Pojo/Image/ImageResponse.java b/src/main/java/io/github/namankhurpia/Pojo/Image/ImageResponse.java new file mode 100644 index 0000000..301090c --- /dev/null +++ b/src/main/java/io/github/namankhurpia/Pojo/Image/ImageResponse.java @@ -0,0 +1,27 @@ +package io.github.namankhurpia.Pojo.Image; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Data +public class ImageResponse { + + @JsonProperty("created") + @SerializedName("created") + public Integer created; + + @JsonProperty("data") + @SerializedName("data") + public List data; + + +} diff --git a/src/main/java/io/github/namankhurpia/Pojo/Image/ModelData.java b/src/main/java/io/github/namankhurpia/Pojo/Image/ModelData.java new file mode 100644 index 0000000..c5f8695 --- /dev/null +++ b/src/main/java/io/github/namankhurpia/Pojo/Image/ModelData.java @@ -0,0 +1,24 @@ +package io.github.namankhurpia.Pojo.Image; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.google.gson.annotations.SerializedName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Data +public class ModelData { + + @JsonProperty("revised_prompt") + @SerializedName("revised_prompt") + public String revisedPrompt; + + @JsonProperty("url") + @SerializedName("url") + public String url; + +} diff --git a/src/main/java/io/github/namankhurpia/Service/EasyopenaiService.java b/src/main/java/io/github/namankhurpia/Service/EasyopenaiService.java index 345d6c5..b70c08b 100644 --- a/src/main/java/io/github/namankhurpia/Service/EasyopenaiService.java +++ b/src/main/java/io/github/namankhurpia/Service/EasyopenaiService.java @@ -7,6 +7,8 @@ import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse; import io.github.namankhurpia.Pojo.Completion.CompletionRequest; import io.github.namankhurpia.Pojo.Completion.CompletionResponse; +import io.github.namankhurpia.Pojo.Image.ImageRequest; +import io.github.namankhurpia.Pojo.Image.ImageResponse; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest; import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse; import io.github.namankhurpia.Pojo.Speech.SpeechRequest; @@ -59,7 +61,10 @@ public ResponseBody createTranscriptions(String accessToken, MultipartBody.Part return dao.createTranscriptions(accessToken,file,model, language, prompt, response_format, temperature); } - + @Override + public ImageResponse createImage(String accessToken, ImageRequest imageRequest) throws IOException { + return dao.createImage(accessToken,imageRequest); + } }