Skip to content

Commit

Permalink
Merge pull request #21 from namankhurpia/dev
Browse files Browse the repository at this point in the history
release v1.0.7 and adding Models for engine
  • Loading branch information
namankhurpia authored Dec 30, 2023
2 parents b6b69cd + bd77d3f commit e865367
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Easy-open-ai
This repo contains the community library for OpenAI's API in java, the easiest way to use GPT 3/4 in your applications.
This repository contains the community-maintained library for OpenAI's API in java, the easiest way to use GPT 3/4 in your applications.

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.namankhurpia/easyopenai/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.namankhurpia/easyopenai)

Expand All @@ -9,7 +9,7 @@ This repo contains the community library for OpenAI's API in java, the easiest w

## Overview

This Java library provides a convenient way to interact with OpenAI's API for both Moderation and Chat Completion. The library encapsulates the necessary details, making it easy to integrate OpenAI's powerful models into your Java applications.
This Java library (community-maintained Library) provides a convenient way to interact with OpenAI's API for both Moderation and Chat Completion. The library encapsulates the necessary details, making it easy to integrate OpenAI's powerful models into your Java applications. This project is not maintained by OPENAI, this is an unofficial library.

## Table of Contents

Expand All @@ -35,6 +35,14 @@ This Java library provides a convenient way to interact with OpenAI's API for bo
- [Multithreaded Async Moderation API](#multithreaded-async-moderation-api)


# Contributing Guidelines


# Running guidelines
All "OPENAI_KEYS" must be read through the readKeys() function defined [here](https://github.com/namankhurpia/Easy-open-ai/blob/main/src/main/java/io/github/namankhurpia/Documentation/RunnerForSingleInstance.java), this function allows you to read multiple keys at the same time, and for multithreaded task it is adviced to use multiple keys to avoid rate limiting. To run this function you need to have keys.txt in your project root folder (feel free to edit).



## Chat Completion API

To use the Chat Completion API, follow these steps:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.namankhurpia</groupId>
<artifactId>easyopenai</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>

<distributionManagement>
<snapshotRepository>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/github/namankhurpia/DAO/DAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.Models.ModelResponse;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;

Expand Down Expand Up @@ -42,6 +43,9 @@ public class DAOImpl implements DaoInterface {
ResponseBody responseBodyObj;

ImageResponse imageResponse;

ModelResponse modelResponse;

private static Logger LOGGER = LoggerFactory.getLogger(DAOImpl.class);

RetrofitApiInterface retrofitApiInterfaceObj;
Expand Down Expand Up @@ -270,5 +274,33 @@ public ImageResponse createImage(String accessToken, ImageRequest imageRequest)
return imageResponse;
}

@Override
public ModelResponse getAllModels(String accessToken) throws IOException {
retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);
LOGGER.info("making req" + accessToken );

Call<ModelResponse> call = retrofitApiInterfaceObj.getAllModels("Bearer "+ accessToken);
Response<ModelResponse> response = call.execute();

if(response.isSuccessful())
{
modelResponse= response.body();
LOGGER.info("Correct response" + modelResponse.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 modelResponse;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.Models.ModelResponse;
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 @@ -40,10 +41,13 @@ public static void main(String[] args) throws IOException, ExecutionException, I
//TestForSpeech();
//TestForTranscription();
//TestForEasyTranscription();
TestForImageGeneration();
//TestForImageGeneration();
GetAllModels();

}



public static void runEasyVisionAPI()throws IOException
{

Expand Down Expand Up @@ -279,6 +283,13 @@ public static void TestForImageGeneration()throws IOException{

}

private static void GetAllModels() throws IOException {
ArrayList<String> keys = readKeys();
ModelResponse response = new EasyopenaiService(new DAOImpl()).getAllModels(keys.get(0));
System.out.println(response);

}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.Models.ModelResponse;
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 @@ -41,5 +42,6 @@ public interface DaoInterface {

ImageResponse createImage(@Header("Authorization")String accessToken, ImageRequest imageRequest) throws IOException;

ModelResponse getAllModels(@Header("Authorization")String accessToken)throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.Models.ModelResponse;
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,8 +60,15 @@ Call<ResponseBody> createTranscriptions(@Header("Authorization")String accessTok
@POST("/v1/images/generations")
Call<ImageResponse> createImage(@Header("Authorization")String accessToken, @Body ImageRequest imageRequest) throws IOException;

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

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

@GET("/v1/models")
Call<ModelResponse> getAllModels(@Header("Authorization")String accessToken)throws IOException;

}
41 changes: 41 additions & 0 deletions src/main/java/io/github/namankhurpia/Pojo/Models/ModelField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.namankhurpia.Pojo.Models;

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 ModelField {

@SerializedName("object")
@JsonProperty("object")
public String object;

@SerializedName("id")
@JsonProperty("id")
public String id;

@SerializedName("ready")
@JsonProperty("ready")
public Boolean ready;

@SerializedName("owner")
@JsonProperty("owner")
public String owner;

@SerializedName("permissions")
@JsonProperty("permissions")
public Object permissions;

@SerializedName("created")
@JsonProperty("created")
public Object created;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.namankhurpia.Pojo.Models;

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 ModelResponse {

@SerializedName("object")
@JsonProperty("object")
public String object;

@SerializedName("data")
@JsonProperty("data")
public List<ModelField> data;


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.Models.ModelResponse;
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 @@ -66,5 +67,10 @@ public ImageResponse createImage(String accessToken, ImageRequest imageRequest)
return dao.createImage(accessToken,imageRequest);
}

@Override
public ModelResponse getAllModels(String accessToken) throws IOException {
return dao.getAllModels(accessToken);
}


}

0 comments on commit e865367

Please sign in to comment.