Skip to content

Commit

Permalink
Merge pull request #1 from namankhurpia/dev
Browse files Browse the repository at this point in the history
Concurrent Async Chat Completion with Multiple keys
  • Loading branch information
namankhurpia authored Nov 30, 2023
2 parents 7cfc7d6 + a7175e9 commit d54ad4a
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 97 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target/
*.iws
*.iml
*.ipr
keys.txt

### NetBeans ###
/nbproject/private/
Expand All @@ -31,3 +32,6 @@ build/

### VS Code ###
.vscode/

#my files
keys.txt
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ This Java library provides a convenient way to interact with OpenAI's API for bo

### Multi-Asynchronous

- [Multiple Async Chat Completion API](#multi-async-chat-completion-api)
- [Multiple Async Moderation API](#multi-async-moderation-api)
- [Multithreaded Async Chat Completion API](#multithreaded-async-chat-completion-api)
- [Multithreaded Async Moderation API](#multithreaded-async-moderation-api)


## Chat Completion API
Expand Down Expand Up @@ -170,7 +170,7 @@ Click [here](#async-moderation-api) to jump to the code example.



## Multi-Asynchronous Chat Completion API
## Multithreaded Async Chat Completion API

For multi-threading and concurrent calls with Chat Completion API, follow these steps:

Expand Down Expand Up @@ -207,7 +207,7 @@ For multi-threading and concurrent calls with Chat Completion API, follow these

Click [here](#multi-async-chat-completion-api) to jump to the code example.

## Multi-Asynchronous Moderation API
## Multithreaded Async Moderation API

For multi-threading and concurrent calls with the Moderation API, follow these steps:

Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/main/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.github.namankhurpia.Interfaces;

import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
import io.github.namankhurpia.Pojo.MyModels.ChatCompletionRequestList;
import io.github.namankhurpia.Pojo.MyModels.ChatCompletionResponseList;
import io.github.namankhurpia.Pojo.MyModels.ModerationRequestList;
import io.github.namankhurpia.Pojo.MyModels.ModerationResponseList;

import java.util.ArrayList;

public interface ConcurrentApiInterface {

ModerationResponseList CallMultipleModerationAPI(String key,ModerationRequestList requestList);

ChatCompletionResponseList CallMultipleChatCompletionAPI(String key, ChatCompletionRequestList requestList);

ChatCompletionResponseList CallMultipleChatCompletionAPI(ArrayList<String> keyList, ChatCompletionRequestList requestList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
public interface EndPoints {

static final String BASE_URL = "https://api.openai.com/";
static final String OPENAI_KEY = "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";
static final String OPENAI_KEY = "";
}
92 changes: 0 additions & 92 deletions src/main/java/io/github/namankhurpia/Main.java

This file was deleted.

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

import io.github.namankhurpia.DAO.AsyncDAOImpl;
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.Service.EasyopenaiAsyncService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 {

/**
* Asynchronous chat
*/

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(OPENAI_KEY,request_chat);

/**
* Asynchronous moderation
*/

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(OPENAI_KEY,request_mod);



}
}
92 changes: 92 additions & 0 deletions src/main/java/io/github/namankhurpia/RunnerForConcurrent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import io.github.namankhurpia.Pojo.MyModels.ModerationResponseList;
import io.github.namankhurpia.Service.EasyopenaiConcurrentService;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

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

Expand Down Expand Up @@ -52,6 +55,15 @@ public static void main(String[] args) {
*/


CallMultipleChatCompletionAPI_multikey_Test();




}

public static void CallMultipleChatCompletionAPI_Test()
{
/**
* Chat completion Multiple requests
*/
Expand Down Expand Up @@ -104,8 +116,88 @@ public static void main(String[] args) {

ChatCompletionResponseList responseList = concurrentCalls.CallMultipleChatCompletionAPI(OPENAI_KEY, list);
System.out.println(responseList);
}

public static void CallMultipleChatCompletionAPI_multikey_Test()
{
ArrayList<String> keyList = readKeys();
EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();

ChatCompletionRequestList list = new ChatCompletionRequestList(new ArrayList<ChatCompletionRequest>());

ChatCompletionRequest requestchat = new ChatCompletionRequest();
requestchat.setModel("gpt-3.5-turbo");
ChatMessage chatMessage = new ChatMessage();
chatMessage.setRole("user");
chatMessage.setContent("what is the capital of India?");
List<ChatMessage> messages = new ArrayList<>();
messages.add(chatMessage);
requestchat.setMessages(messages);
list.add(requestchat);


ChatCompletionRequest requestchat2 = new ChatCompletionRequest();
requestchat2.setModel("gpt-3.5-turbo");
ChatMessage chatMessage2 = new ChatMessage();
chatMessage2.setRole("user");
chatMessage2.setContent("what is the capital of combodia?");
List<ChatMessage> messages2 = new ArrayList<>();
messages2.add(chatMessage2);
requestchat2.setMessages(messages2);
list.add(requestchat2);


ChatCompletionRequest requestchat3 = new ChatCompletionRequest();
requestchat3.setModel("gpt-3.5-turbo");
ChatMessage chatMessage3 = new ChatMessage();
chatMessage3.setRole("user");
chatMessage3.setContent("what is the capital of new zealand?");
List<ChatMessage> messages3 = new ArrayList<>();
messages3.add(chatMessage3);
requestchat3.setMessages(messages3);
list.add(requestchat3);


ChatCompletionRequest requestchat4 = new ChatCompletionRequest();
requestchat4.setModel("gpt-3.5-turbo");
ChatMessage chatMessage4 = new ChatMessage();
chatMessage4.setRole("user");
chatMessage4.setContent("what is the capital of hawaii? and what is 2+2?");
List<ChatMessage> messages4 = new ArrayList<>();
messages4.add(chatMessage4);
requestchat4.setMessages(messages4);
list.add(requestchat4);



ChatCompletionResponseList responseList = concurrentCalls.CallMultipleChatCompletionAPI(keyList, list);
System.out.println("response is"+responseList);
}

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;
}
}
46 changes: 46 additions & 0 deletions src/main/java/io/github/namankhurpia/RunnerForSingleInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.namankhurpia;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class RunnerForSingleInstance {
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
System.out.println("Hello world!");

/**
* Moderation API single
*/
/*
ModerationAPIRequest request = new ModerationAPIRequest();
request.setInput("kill me now");
request.setModel("text-moderation-latest");
EasyopenaiService obj = new EasyopenaiService(new DAOImpl());
ModerationAPIResponse res = obj.getmoderation(OPENAI_KEY,request);
*/

/**
* Chat Completion API single
*/
/*
EasyopenaiService obj = new EasyopenaiService(new DAOImpl());
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 = new ChatCompletionRequest();
request.setModel("gpt-3.5-turbo");
request.setMessages(messages); //old conversations as well
ChatCompletionResponse res = obj.chatCompletion(OPENAI_KEY,request);
*/





}
}
Loading

0 comments on commit d54ad4a

Please sign in to comment.