Skip to content

Commit

Permalink
adding concurrent request list documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
namankhurpia committed Nov 29, 2023
1 parent 99a1be5 commit 7cfc7d6
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 84 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,77 @@ Click [here](#async-moderation-api) to jump to the code example.




## Multi-Asynchronous Chat Completion API

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

1. Create a `ChatCompletionRequestList` object:

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

2. Add multiple `ChatCompletionRequest` objects to the list:

```java
// Example request 1
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);

// Add more requests as needed (requestchat2, requestchat3, requestchat4, etc.)
```

3. Make the multi-asynchronous API call:

```java
EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();
ChatCompletionResponseList responseList = concurrentCalls.CallMultipleChatCompletionAPI(OPENAI_KEY, list);
System.out.println(responseList);
```

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

## Multi-Asynchronous Moderation API

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

1. Create a `ModerationRequestList` object:

```java
ModerationRequestList requestList = new ModerationRequestList(new ArrayList<ModerationAPIRequest>());
```

2. Add multiple `ModerationAPIRequest` objects to the list:

```java
// Example request 1
ModerationAPIRequest request = new ModerationAPIRequest();
request.setInput("kill me now");
request.setModel("text-moderation-latest");
requestList.add(request);

// Add more requests as needed (request2, request3, request4, etc.)
```

3. Make the multi-asynchronous API call:

```java
EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();
ModerationResponseList responseList = concurrentCalls.CallMultipleModerationAPI(OPENAI_KEY, requestList);
System.out.println(responseList);
```

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


# Dependencies

Ensure you have the required dependencies installed before using the OpenAI API wrapper.
Expand Down
86 changes: 2 additions & 84 deletions src/main/java/io/github/namankhurpia/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,98 +77,16 @@ public static void main(String[] args) throws IOException, ExecutionException, I
/**
* Asynchronous moderation
*/
/*
ModerationAPIRequest request = new ModerationAPIRequest();
request.setInput("kill me now");
request.setModel("text-moderation-latest");
EasyopenaiAsyncService obj = new EasyopenaiAsyncService(new AsyncDAOImpl());
ModerationAPIResponse res = obj.getASyncModeration(OPENAI_KEY,request);

/*
ModerationAPIRequest request = new ModerationAPIRequest();
request.setInput("kill me now");
request.setModel("text-moderation-latest");
ModerationAPIRequest request2 = new ModerationAPIRequest();
request2.setInput("hurray me now");
request2.setModel("text-moderation-latest");
ModerationAPIRequest request3 = new ModerationAPIRequest();
request3.setInput("yippee me now");
request3.setModel("text-moderation-latest");
ModerationAPIRequest request4 = new ModerationAPIRequest();
request4.setInput("KILL me now");
request4.setModel("text-moderation-latest");
ModerationRequestList requestList = new ModerationRequestList(new ArrayList<ModerationAPIRequest>());
requestList.add(request);
requestList.add(request2);
requestList.add(request3);
requestList.add(request4);
//AsyncModerationAPIServiceImpl obj2 = new AsyncModerationAPIServiceImpl(new AsyncDAOImpl());
//ModerationAPIResponse res = obj2.getASyncModeration(OPENAI_KEY,request);
//System.out.println(res);
EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();
//System.out.println(concurrentCalls.CallMultipleModerationAPI(OPENAI_KEY,requestList));
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);
System.out.println(concurrentCalls.CallMultipleChatCompletionAPI(OPENAI_KEY, list));
*/


*/

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

import io.github.namankhurpia.DAO.AsyncDAOImpl;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatCompletionRequest;
import io.github.namankhurpia.Pojo.ChatCompletion.ChatMessage;
import io.github.namankhurpia.Pojo.Moderations.ModerationAPIRequest;
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 io.github.namankhurpia.Service.EasyopenaiConcurrentService;

import java.util.ArrayList;
import java.util.List;

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

public class RunnerForConcurrent {
public static void main(String[] args) {


/**
* Moderation Concurrent call
*/
/*
ModerationAPIRequest request = new ModerationAPIRequest();
request.setInput("kill me now");
request.setModel("text-moderation-latest");
ModerationAPIRequest request2 = new ModerationAPIRequest();
request2.setInput("hurray me now");
request2.setModel("text-moderation-latest");
ModerationAPIRequest request3 = new ModerationAPIRequest();
request3.setInput("yippee me now");
request3.setModel("text-moderation-latest");
ModerationAPIRequest request4 = new ModerationAPIRequest();
request4.setInput("KILL me now");
request4.setModel("text-moderation-latest");
ModerationRequestList requestList = new ModerationRequestList(new ArrayList<ModerationAPIRequest>());
requestList.add(request);
requestList.add(request2);
requestList.add(request3);
requestList.add(request4);
EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();
ModerationResponseList responseList = concurrentCalls.CallMultipleModerationAPI(OPENAI_KEY,requestList);
System.out.println(responseList);
*/


/**
* Chat completion Multiple requests
*/
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);

EasyopenaiConcurrentService concurrentCalls = new EasyopenaiConcurrentService();

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



}
}

0 comments on commit 7cfc7d6

Please sign in to comment.