Skip to content

Commit

Permalink
Merge pull request #3 from namankhurpia/dev
Browse files Browse the repository at this point in the history
Adding Support for Vision API
  • Loading branch information
namankhurpia authored Dec 9, 2023
2 parents 501a38e + d7ec1f3 commit d1fabb5
Show file tree
Hide file tree
Showing 20 changed files with 466 additions and 62 deletions.
51 changes: 51 additions & 0 deletions src/main/java/io/github/namankhurpia/DAO/AsyncDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.github.namankhurpia.Pojo.Completion.CompletionResponse;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;
import io.github.namankhurpia.Pojo.Vision.VisionApiRequest;
import io.github.namankhurpia.Pojo.Vision.VisionApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.Call;
Expand All @@ -27,6 +29,8 @@ public class AsyncDAOImpl implements AsyncApiInterface {
CompletionResponse completionResponseObj;

ChatCompletionResponse chatCompletionResponseObj;

VisionApiResponse visionApiResponseObj;
private static Logger LOGGER = LoggerFactory.getLogger(AsyncDAOImpl.class);

RetrofitApiInterface retrofitApiInterfaceObj;
Expand Down Expand Up @@ -132,6 +136,53 @@ public void onFailure(Call<ChatCompletionResponse> call, Throwable throwable) {



return future.get();
}

@Override
public VisionApiResponse getAsyncVisionAPI(String accessToken, VisionApiRequest request) throws IOException, ExecutionException, InterruptedException {

retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);

LOGGER.info("making req" + accessToken + " with request "+ request.toString());

CompletableFuture<VisionApiResponse> future = new CompletableFuture<>();

Call<VisionApiResponse> call = retrofitApiInterfaceObj.visionAPI("Bearer "+accessToken,request);

call.enqueue(new Callback<VisionApiResponse>() {
@Override
public void onResponse(Call<VisionApiResponse> call, Response<VisionApiResponse> response) {
if(response.isSuccessful())
{
visionApiResponseObj = response.body();
future.complete(response.body());
LOGGER.info("Correct response" + visionApiResponseObj.toString());

}
else
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
LOGGER.error("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBody);

future.completeExceptionally(new MalformedRequestException(errorBody, new Throwable(errorBody)));

}
}

@Override
public void onFailure(Call<VisionApiResponse> call, Throwable throwable) {
future.completeExceptionally(throwable);
}



});




return future.get();
}
}
38 changes: 34 additions & 4 deletions src/main/java/io/github/namankhurpia/DAO/DAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;

import io.github.namankhurpia.Pojo.Vision.VisionApiRequest;
import io.github.namankhurpia.Pojo.Vision.VisionApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import retrofit2.*;
Expand All @@ -24,6 +26,8 @@ public class DAOImpl implements DaoInterface {
CompletionResponse completionResponseObj;

ChatCompletionResponse chatCompletionResponseObj;

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

RetrofitApiInterface retrofitApiInterfaceObj;
Expand Down Expand Up @@ -54,9 +58,11 @@ public ModerationAPIResponse getmoderation(String accessToken, ModerationAPIRequ
} else {
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
System.out.println("HTTP Status Code: " + httpStatusCode);
System.out.println("Error Body: " + errorBody.toString());
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));

}
} catch (IOException e) {
// Handle IO exception
Expand Down Expand Up @@ -90,7 +96,8 @@ public CompletionResponse getCompletion(String accessToken, CompletionRequest re
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? response.errorBody().string() : "Empty error body";
LOGGER.error("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBody);
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));
}

Expand Down Expand Up @@ -126,7 +133,8 @@ public ChatCompletionResponse chatCompletion(String accessToken, ChatCompletionR
{
int httpStatusCode = response.code();
String errorBody = response.errorBody() != null ? String.valueOf(response.errorBody()) : "Empty error body";
LOGGER.error("Unsuccessful response with HTTP status code " + httpStatusCode + " and error body: " + errorBody);
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));

}
Expand All @@ -137,10 +145,32 @@ public ChatCompletionResponse chatCompletion(String accessToken, ChatCompletionR
return chatCompletionResponseObj;
}

@Override
public VisionApiResponse visionAPI(String accessToken, VisionApiRequest request) throws IOException {

retrofitApiInterfaceObj = RetrofitAPIClient.getClient().create(RetrofitApiInterface.class);
LOGGER.info("making req" + accessToken + " with request "+ request.toString());

Call<VisionApiResponse> call = retrofitApiInterfaceObj.visionAPI("Bearer "+accessToken,request);
Response<VisionApiResponse> response = call.execute();

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


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

import io.github.namankhurpia.DAO.AsyncDAOImpl;
import io.github.namankhurpia.DAO.DAOImpl;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatMessage;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;
import io.github.namankhurpia.Pojo.Vision.*;
import io.github.namankhurpia.Service.EasyopenaiAsyncService;
import io.github.namankhurpia.Service.EasyopenaiService;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import static io.github.namankhurpia.Interfaces.EndPoints.OPENAI_KEY;

public class RunnerForAsync {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {


RunnerForAsync_chatCompletion();
RunnerForAsync_ModerationAPI();
RunnerForAsync_VisionAPI();



}

public static void RunnerForAsync_chatCompletion() throws IOException, ExecutionException, InterruptedException {
/**
* Asynchronous chat
*/
ArrayList<String> keys = readKeys();
EasyopenaiAsyncService easyopenaiAsyncService_chat = new EasyopenaiAsyncService(new AsyncDAOImpl());

ChatMessage chatMessage = new ChatMessage();
chatMessage.setRole("user");
chatMessage.setContent("what is the capital of combodia?");

List<ChatMessage> messages = new ArrayList<>();
messages.add(chatMessage);

ChatCompletionRequest request_chat = new ChatCompletionRequest();
request_chat.setModel("gpt-3.5-turbo");
request_chat.setMessages(messages); //old conversations as well
ChatCompletionResponse response_chat = easyopenaiAsyncService_chat.getAsyncChatCompletion(keys.get(0),request_chat);
}

public static void RunnerForAsync_ModerationAPI() throws IOException, ExecutionException, InterruptedException {
/**
* Asynchronous moderation
*/
ArrayList<String> keys = readKeys();
EasyopenaiAsyncService easyopenaiAsyncService_mod = new EasyopenaiAsyncService(new AsyncDAOImpl());

ModerationAPIRequest request_mod = new ModerationAPIRequest();
request_mod.setInput("kill me now");
request_mod.setModel("text-moderation-latest");

ModerationAPIResponse response_mod = easyopenaiAsyncService_mod.getASyncModeration(keys.get(0),request_mod);
}

public static void RunnerForAsync_VisionAPI() throws IOException, ExecutionException, InterruptedException {
/**
* Vision API Single Instance
*/
ArrayList<String> keys = readKeys();
VisionApiRequest request = new VisionApiRequest();

ImageUrl url = new ImageUrl();
url.setUrl("https://images.pexels.com/photos/18907092/pexels-photo-18907092/free-photo-of-a-photo-of-the-golden-gate-bridge-in-the-sky.jpeg");
url.setDetail("low");

Content content1 = new Content();
content1.setText("What’s in this image?");
content1.setType("text");

Content content2 = new Content();
content2.setImageUrl(url);
content2.setType("image_url");

ArrayList<Content> listofContent = new ArrayList<>();
listofContent.add(content1);
listofContent.add(content2);

MessageList messageList = new MessageList();
messageList.setRole("user");
messageList.setContent(listofContent);

ArrayList<MessageList> listofMessage= new ArrayList<>();
listofMessage.add(messageList);

request.setModel("gpt-4-vision-preview");
request.setMaxTokens(300);
request.setMessages(listofMessage);

EasyopenaiAsyncService easyopenaiAsyncService_mod = new EasyopenaiAsyncService(new AsyncDAOImpl());
VisionApiResponse res = easyopenaiAsyncService_mod.getAsyncVisionAPI(keys.get(0),request);
System.out.println("Response is:"+res);



}

public static ArrayList<String> readKeys()
{
String filePath = "keys.txt";
ArrayList<String> keyList = new ArrayList<>();

// Open the file using Scanner
try {
File file = new File(filePath);
Scanner scanner = new Scanner(file);

// Read each line and extract keys
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// Assuming each line contains a key
keyList.add(line);
//System.out.println("Key: " + line);
}

// Close the scanner
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filePath);
e.printStackTrace();
}
return keyList;
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.namankhurpia;
package io.github.namankhurpia.Documentation;

import io.github.namankhurpia.DAO.AsyncDAOImpl;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.namankhurpia;
package io.github.namankhurpia.Documentation;

import io.github.namankhurpia.DAO.DAOImpl;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionResponse;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatMessage;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIResponse;
import io.github.namankhurpia.Pojo.Vision.*;
import io.github.namankhurpia.Service.EasyopenaiService;

import java.io.File;
Expand All @@ -19,10 +20,55 @@
public class RunnerForSingleInstance {

public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
runMoDerationSingleInstance();
//runMoDerationSingleInstance();
runVisionAPI();
}

public static void runVisionAPI() throws IOException
{
/**
* Vision API Single Instance
*/
ArrayList<String> keys = readKeys();
VisionApiRequest request = new VisionApiRequest();

ImageUrl url = new ImageUrl();
url.setUrl("https://images.pexels.com/photos/18907092/pexels-photo-18907092/free-photo-of-a-photo-of-the-golden-gate-bridge-in-the-sky.jpeg");
url.setDetail("low");

Content content1 = new Content();
content1.setText("What’s in this image?");
content1.setType("text");

Content content2 = new Content();
content2.setImageUrl(url);
content2.setType("image_url");

ArrayList<Content> listofContent = new ArrayList<>();
listofContent.add(content1);
listofContent.add(content2);

MessageList messageList = new MessageList();
messageList.setRole("user");
messageList.setContent(listofContent);

ArrayList<MessageList> listofMessage= new ArrayList<>();
listofMessage.add(messageList);

request.setModel("gpt-4-vision-preview");
request.setMaxTokens(300);
request.setMessages(listofMessage);

System.out.println(request);

EasyopenaiService obj = new EasyopenaiService(new DAOImpl());
VisionApiResponse res = obj.visionAPI(keys.get(0),request);
System.out.println("Response is:"+res);




}

public static void runMoDerationSingleInstance() throws IOException {
/**
Expand Down Expand Up @@ -62,6 +108,8 @@ public static void runChatCompletionSingleInstance() throws IOException{
}




public static ArrayList<String> readKeys()
{
String filePath = "keys.txt";
Expand Down
Loading

0 comments on commit d1fabb5

Please sign in to comment.