Skip to content

Commit

Permalink
adding Image generation API - DALL E
Browse files Browse the repository at this point in the history
  • Loading branch information
namankhurpia committed Dec 30, 2023
1 parent 85bc750 commit dd80051
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 15 deletions.
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
```
Expand All @@ -204,6 +207,10 @@ Click [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/jav

## 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");
MultipartBody.Part filePart = MultipartBody.Part.createFormData(
Expand All @@ -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:
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/github/namankhurpia/DAO/DAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ImageResponse> call = retrofitApiInterfaceObj.createImage("Bearer "+ accessToken, imageRequest);
Response<ImageResponse> 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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,7 +39,8 @@ public static void main(String[] args) throws IOException, ExecutionException, I
//runEasyChatCompletionSingleInstance();
//TestForSpeech();
//TestForTranscription();
TestForEasyTranscription();
//TestForEasyTranscription();
TestForImageGeneration();

}

Expand Down Expand Up @@ -254,14 +257,28 @@ public static void TestForTranscription() throws IOException {
public static void TestForEasyTranscription() throws IOException {
ArrayList<String> 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<String> 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);

}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,4 +56,11 @@ Call<ResponseBody> createTranscriptions(@Header("Authorization")String accessTok
@Part("response_format") RequestBody response_format,
@Part("temperature")RequestBody temperature ) throws IOException;

@POST("/v1/images/generations")
Call<ImageResponse> createImage(@Header("Authorization")String accessToken, @Body ImageRequest imageRequest) throws IOException;

@POST(".v1/images/edits")
@Multipart
Call<ResponseBody> createImageEdit(@Header("Authorization")String accessToken, @Part MultipartBody.Part image, @Part("prompt") RequestBody prompt );

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public class ChatCompletionRequest {
@JsonProperty("logit_bias")
Map<String, Integer> 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).
Expand All @@ -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.
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/io/github/namankhurpia/Pojo/Image/ImageRequest.java
Original file line number Diff line number Diff line change
@@ -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;

}
27 changes: 27 additions & 0 deletions src/main/java/io/github/namankhurpia/Pojo/Image/ImageResponse.java
Original file line number Diff line number Diff line change
@@ -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<ModelData> data;


}
24 changes: 24 additions & 0 deletions src/main/java/io/github/namankhurpia/Pojo/Image/ModelData.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


}

0 comments on commit dd80051

Please sign in to comment.