diff --git a/README.md b/README.md index 42a6e45..4bc41b8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Intelligent Java -*IntelliJava V0.6.0* +*IntelliJava V0.6.1* Intelligent java (IntelliJava) is the ultimate tool for Java developers looking to integrate with the latest language models and deep learning frameworks. The library provides a simple and intuitive API with convenient methods for sending text input to models like GPT-3 and DALLĀ·E, and receiving generated text or images in return. With just a few lines of code, you can easily access the power of cutting-edge AI models to enhance your projects. @@ -14,32 +14,32 @@ The supported models: 3. Call the ``RemoteLanguageModel`` for the language models and ``RemoateImageModel`` for image generation. ## Integration -The package released to [Maven Central Repository](https://central.sonatype.dev/artifact/io.github.barqawiz/intellijava.core/0.6.0). +The package released to [Maven Central Repository](https://central.sonatype.com/artifact/io.github.barqawiz/intellijava.core/0.6.2). Maven: ```xml io.github.barqawiz intellijava.core - 0.6.0 + 0.6.2 ``` Gradle: ``` -implementation group: 'io.github.barqawiz', name: 'intellijava.core', version: '0.6.0' +implementation 'io.github.barqawiz:intellijava.core:0.6.2' ``` Gradle(Kotlin): ``` -implementation("io.github.barqawiz:intellijava.core:0.6.0") +implementation("io.github.barqawiz:intellijava.core:0.6.2") ``` Jar download: -[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.6.0/intellijava.core-0.6.0.jar). +[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.6.2/intellijava.core-0.6.2.jar). -For ready integration: try the sample_code. +For ready integration: try the [sample_code](https://github.com/Barqawiz/IntelliJava/tree/main/sample_code). ## Code Example **Language model code** (2 steps): @@ -72,6 +72,7 @@ For full example check the code inside sample_code project. ## Third-party dependencies The only dependencies is **GSON**. +*Required to add manually when using IntelliJava jar. However, if you imported this repo through Maven, it will handle the dependencies.* For Maven: ``` diff --git a/core/com.intellijava.core/pom.xml b/core/com.intellijava.core/pom.xml index 4ae96f6..5f2c917 100644 --- a/core/com.intellijava.core/pom.xml +++ b/core/com.intellijava.core/pom.xml @@ -6,7 +6,7 @@ io.github.barqawiz intellijava.core - 0.6.0 + 0.6.2 Intellijava IntelliJava allows java developers to easily integrate with the latest language models, image generation, and deep learning frameworks. diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteLanguageModel.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteLanguageModel.java index f2d8573..0a55a0e 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteLanguageModel.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteLanguageModel.java @@ -21,8 +21,11 @@ import java.util.List; import java.util.Map; import com.intellijava.core.model.CohereLanguageResponse; +import com.intellijava.core.model.CohereLanguageResponse.Generation; import com.intellijava.core.model.OpenaiLanguageResponse; +import com.intellijava.core.model.OpenaiLanguageResponse.Choice; import com.intellijava.core.model.SupportedLangModels; +import com.intellijava.core.model.OpenaiImageResponse.Data; import com.intellijava.core.model.input.LanguageModelInput; import com.intellijava.core.wrappers.CohereAIWrapper; import com.intellijava.core.wrappers.OpenAIWrapper; @@ -131,8 +134,10 @@ private void initiate(String keyValue, SupportedLangModels keyType) { * * Call a remote large model to generate any text based on the received prompt. * - * @param langInput flexible builder for language model parameters. + * To support multiple response call the variation function generateMultiText. * + * @param langInput flexible builder for language model parameters. + * * @return string for the model response. * @throws IOException if there is an error when connecting to the * OpenAI API. @@ -143,11 +148,42 @@ private void initiate(String keyValue, SupportedLangModels keyType) { public String generateText(LanguageModelInput langInput) throws IOException { if (this.keyType.equals(SupportedLangModels.openai)) { - return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(), - langInput.getMaxTokens()); + return this.generateOpenaiText(langInput.getModel(), + langInput.getPrompt(), langInput.getTemperature(), + langInput.getMaxTokens(), langInput.getNumberOfOutputs()).get(0); + } else if (this.keyType.equals(SupportedLangModels.cohere)) { + return this.generateCohereText(langInput.getModel(), + langInput.getPrompt(), langInput.getTemperature(), + langInput.getMaxTokens(), langInput.getNumberOfOutputs()).get(0); + } else { + throw new IllegalArgumentException("This version support openai keyType only"); + } + + } + + /** + * + * Call a remote large model to generate any text based on the received prompt. + * + * @param langInput flexible builder for language model parameters. + * + * @return list of model responses. + * @throws IOException if there is an error when connecting to the + * OpenAI API. + * @throws IllegalArgumentException if the keyType passed in the constructor is + * not "openai". + * + */ + public List generateMultiText(LanguageModelInput langInput) throws IOException { + + if (this.keyType.equals(SupportedLangModels.openai)) { + return this.generateOpenaiText(langInput.getModel(), + langInput.getPrompt(), langInput.getTemperature(), + langInput.getMaxTokens(), langInput.getNumberOfOutputs()); } else if (this.keyType.equals(SupportedLangModels.cohere)) { - return this.generateCohereText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(), - langInput.getMaxTokens()); + return this.generateCohereText(langInput.getModel(), + langInput.getPrompt(), langInput.getTemperature(), + langInput.getMaxTokens(), langInput.getNumberOfOutputs()); } else { throw new IllegalArgumentException("This version support openai keyType only"); } @@ -163,11 +199,13 @@ public String generateText(LanguageModelInput langInput) throws IOException { * @param prompt text of the required action or the question. * @param temperature higher values means more risks and creativity. * @param maxTokens maximum size of the model input and output. + * @param numberOfOutputs number of model outputs. * @return string model response. * @throws IOException if there is an error when connecting to the OpenAI API. * */ - private String generateOpenaiText(String model, String prompt, float temperature, int maxTokens) + private List generateOpenaiText(String model, String prompt, float temperature, + int maxTokens, int numberOfOutputs) throws IOException { if (model.equals("")) @@ -178,10 +216,16 @@ private String generateOpenaiText(String model, String prompt, float temperature params.put("prompt", prompt); params.put("temperature", temperature); params.put("max_tokens", maxTokens); + params.put("n", numberOfOutputs); OpenaiLanguageResponse resModel = (OpenaiLanguageResponse) openaiWrapper.generateText(params); - return resModel.getChoices().get(0).getText(); + List outputs = new ArrayList<>(); + for (Choice item : resModel.getChoices()) { + outputs.add(item.getText()); + } + + return outputs; } @@ -192,11 +236,13 @@ private String generateOpenaiText(String model, String prompt, float temperature * @param prompt text of the required action or the question. * @param temperature higher values means more risks and creativity. * @param maxTokens maximum size of the model input and output. + * @param numberOfOutputs number of model outputs. * @return string model response. * @throws IOException if there is an error when connecting to the API. * */ - private String generateCohereText(String model, String prompt, float temperature, int maxTokens) + private List generateCohereText(String model, String prompt, float temperature, + int maxTokens, int numberOfOutputs) throws IOException { if (model.equals("")) @@ -207,10 +253,16 @@ private String generateCohereText(String model, String prompt, float temperature params.put("prompt", prompt); params.put("temperature", temperature); params.put("max_tokens", maxTokens); + params.put("num_generations", numberOfOutputs); CohereLanguageResponse resModel = (CohereLanguageResponse) cohereWrapper.generateText(params); - - return resModel.getGenerations().get(0).getText(); + + List outputs = new ArrayList<>(); + for (Generation item: resModel.getGenerations()) { + outputs.add(item.getText()); + } + + return outputs; } } diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/model/CohereLanguageResponse.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/CohereLanguageResponse.java index da84320..e49bd5f 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/model/CohereLanguageResponse.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/CohereLanguageResponse.java @@ -15,6 +15,13 @@ public class CohereLanguageResponse extends BaseRemoteModel{ private List generations; private String prompt; + /** + * CohereLanguageResponse default constructor. + */ + public CohereLanguageResponse() { + + } + /** * * Generation is wrapper for the response @@ -26,6 +33,13 @@ public static class Generation { private String id; private String text; + /** + * Generation default constructor. + */ + public Generation() { + + } + /** * Get the unique identifier for the generation. * diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/model/input/LanguageModelInput.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/input/LanguageModelInput.java index 2c7834f..c2b788c 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/model/input/LanguageModelInput.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/input/LanguageModelInput.java @@ -17,6 +17,7 @@ public class LanguageModelInput { private String prompt; private float temperature; private int maxTokens; + private int numberOfOutputs = 1; /** * Private Constructor for the Builder. @@ -27,6 +28,7 @@ private LanguageModelInput(Builder builder) { this.prompt = builder.prompt; this.temperature = builder.temperature; this.maxTokens = builder.maxTokens; + this.numberOfOutputs = builder.numberOfOutputs; } /** * @@ -38,6 +40,7 @@ public static class Builder { private String prompt; private float temperature; private int maxTokens; + private int numberOfOutputs = 1; /** * Language input Constructor. @@ -90,6 +93,22 @@ public Builder setMaxTokens(int maxTokens) { this.maxTokens = maxTokens; return this; } + + /** + * Setter for numberOfOutputs + * @param numberOfOutputs number of model outputs, default value is 1. + * + * Cohere maximum value is five. + * + * @return instance of Builder + */ + public Builder setNumberOfOutputs(int numberOfOutputs) { + if (this.numberOfOutputs < 0) + this.numberOfOutputs = 0; + + this.numberOfOutputs = numberOfOutputs; + return this; + } /** * Build the final LanguageModelInput object. @@ -130,5 +149,15 @@ public float getTemperature() { public int getMaxTokens() { return maxTokens; } + + /** + * Getter for number of model outputs. + * @return numberOfOutputs + */ + public int getNumberOfOutputs() { + return numberOfOutputs; + } + + } diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/utils/ConnHelper.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/utils/ConnHelper.java index 522d1a1..ce72beb 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/utils/ConnHelper.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/utils/ConnHelper.java @@ -108,5 +108,5 @@ public static String readStream(InputStream stream) throws IOException { } } return result.toString(); - } + } } diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/CohereAIWrapper.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/CohereAIWrapper.java index 9ca36fb..f9782fe 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/CohereAIWrapper.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/CohereAIWrapper.java @@ -15,6 +15,7 @@ /** * + * Wrapper for the Cohere API models. * * @author github.com/Barqawiz * diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/OpenAIWrapper.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/OpenAIWrapper.java index eb5eb4d..84a8d00 100644 --- a/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/OpenAIWrapper.java +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/OpenAIWrapper.java @@ -15,6 +15,7 @@ */ package com.intellijava.core.wrappers; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; @@ -70,7 +71,7 @@ public BaseRemoteModel generateText(Map params) throws IOExcepti String url = API_BASE_URL + Config2.getInstance().getProperty("url.openai.completions"); String json = ConnHelper.convertMaptToJson(params); - + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); diff --git a/core/com.intellijava.core/src/test/java/com/intellijava/core/CohereModelConnectionTest.java b/core/com.intellijava.core/src/test/java/com/intellijava/core/CohereModelConnectionTest.java index e1bbeb4..4e33574 100644 --- a/core/com.intellijava.core/src/test/java/com/intellijava/core/CohereModelConnectionTest.java +++ b/core/com.intellijava.core/src/test/java/com/intellijava/core/CohereModelConnectionTest.java @@ -15,6 +15,10 @@ import com.intellijava.core.utils.Config2; import com.intellijava.core.wrappers.CohereAIWrapper; +/** + * + * Unit test for Remote Language Model + */ public class CohereModelConnectionTest { /** diff --git a/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java b/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java index e18743b..1e9ac54 100644 --- a/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java +++ b/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java @@ -27,6 +27,7 @@ import com.intellijava.core.controller.RemoteLanguageModel; import com.intellijava.core.model.OpenaiImageResponse; import com.intellijava.core.model.OpenaiImageResponse.Data; +import com.intellijava.core.model.SupportedLangModels; import com.intellijava.core.model.input.ImageModelInput; import com.intellijava.core.model.input.LanguageModelInput; import com.intellijava.core.utils.Config2; @@ -51,10 +52,10 @@ public void testOpenaiCompletionRemoteModel() { try { - RemoteLanguageModel wrapper = new RemoteLanguageModel(openaiKey, "openai"); + RemoteLanguageModel wrapper = new RemoteLanguageModel(openaiKey, SupportedLangModels.openai); LanguageModelInput input = new LanguageModelInput.Builder("return a java code that print hello world") - .setModel("text-davinci-002").setTemperature(0.7f).setMaxTokens(50).build(); + .setModel("text-davinci-003").setTemperature(0.7f).setMaxTokens(50).build(); if (openaiKey.isBlank()) return; @@ -75,6 +76,37 @@ public void testOpenaiCompletionRemoteModel() { } } + + @Test + public void testOpenaiMultiTextCompletionRemoteModel() { + + try { + + RemoteLanguageModel wrapper = new RemoteLanguageModel(openaiKey, "openai"); + + LanguageModelInput input = new LanguageModelInput.Builder("Summarize the plot of the 'Inception' movie in two sentences") + .setModel("text-davinci-003").setTemperature(0.7f) + .setMaxTokens(80).setNumberOfOutputs(2).build(); + + if (openaiKey.isBlank()) return; + + List resValues = wrapper.generateMultiText(input); + + for (String result : resValues) + System.out.print("- " + result); + + assert resValues.size() == 2; + + } catch (IOException e) { + if (openaiKey.isBlank()) { + System.out.print("testOpenaiCompletion: set the API key to run the test case."); + } else { + fail("Test case failed with exception: " + e.getMessage()); + } + + } + } + @Test public void testImageWrapper() { diff --git a/core/com.intellijava.core/target/classes/config.properties b/core/com.intellijava.core/target/classes/config.properties deleted file mode 100644 index 5e0ff7f..0000000 --- a/core/com.intellijava.core/target/classes/config.properties +++ /dev/null @@ -1,8 +0,0 @@ -url.openai.base=https://api.openai.com -url.openai.completions=/v1/completions -url.openai.imagegenerate=/v1/images/generations -url.openai.testkey= -url.cohere.base=https://api.cohere.ai -url.cohere.completions=/generate -url.cohere.version=2022-12-06 -url.cohere.testkey= \ No newline at end of file diff --git a/sample_code/.classpath b/sample_code/.classpath index 1ff9255..0b3eac9 100644 --- a/sample_code/.classpath +++ b/sample_code/.classpath @@ -8,6 +8,6 @@ - + diff --git a/sample_code/jars/intellijava.core-0.6.0.jar b/sample_code/jars/intellijava.core-0.6.0.jar deleted file mode 100644 index 2525d3c..0000000 Binary files a/sample_code/jars/intellijava.core-0.6.0.jar and /dev/null differ diff --git a/sample_code/jars/intellijava.core-0.6.1.jar b/sample_code/jars/intellijava.core-0.6.1.jar new file mode 100644 index 0000000..5993989 Binary files /dev/null and b/sample_code/jars/intellijava.core-0.6.1.jar differ diff --git a/sample_code/src/com/intelliJava/test/OpenaiApp.java b/sample_code/src/com/intelliJava/test/OpenaiApp.java index 0356e77..5b0071c 100644 --- a/sample_code/src/com/intelliJava/test/OpenaiApp.java +++ b/sample_code/src/com/intelliJava/test/OpenaiApp.java @@ -60,16 +60,33 @@ private static void tryTheLanguageModel(String apiKey) throws IOException { // initiate the remote language model wrapper with openai details RemoteLanguageModel langModel = new RemoteLanguageModel(apiKey, "openai"); - // prepare the input parameters - change the prompt to any text like "write a - // funny short story" + // prepare the input parameters - change the prompt to any text LanguageModelInput langInput = new LanguageModelInput.Builder("Summarize the plot of the 'Inception' movie in two sentences") .setModel("text-davinci-003").setTemperature(0.7f).setMaxTokens(80).build(); + String resValue = langModel.generateText(langInput); // print language model output System.out.println("Language model output:\n" + resValue); } + + private static void tryTheLanguageModelMultiText(String apiKey) throws IOException { + // initiate the remote language model wrapper with openai details + RemoteLanguageModel langModel = new RemoteLanguageModel(apiKey, "openai"); + + // prepare the input parameters - change the prompt to any text + LanguageModelInput langInput = new LanguageModelInput.Builder("Summarize the plot of the 'Inception' movie in two sentences") + .setModel("text-davinci-003").setTemperature(0.7f) + .setMaxTokens(80).setNumberOfOutputs(2).build(); + + + List resValues = langModel.generateMultiText(langInput); + + // print language model output + for (String result : resValues) + System.out.print("- " + result); + } private static void tryTheImageModel(String apiKey) throws IOException { // initiate the remote image model wrapper