Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
<dependency>
<groupId>io.github.barqawiz</groupId>
<artifactId>intellijava.core</artifactId>
<version>0.6.0</version>
<version>0.6.2</version>
</dependency>
```

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):
Expand Down Expand Up @@ -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:
```
Expand Down
2 changes: 1 addition & 1 deletion core/com.intellijava.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.barqawiz</groupId>
<artifactId>intellijava.core</artifactId>
<version>0.6.0</version>
<version>0.6.2</version>

<name>Intellijava</name>
<description>IntelliJava allows java developers to easily integrate with the latest language models, image generation, and deep learning frameworks.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<String> 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");
}
Expand All @@ -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<String> generateOpenaiText(String model, String prompt, float temperature,
int maxTokens, int numberOfOutputs)
throws IOException {

if (model.equals(""))
Expand All @@ -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<String> outputs = new ArrayList<>();
for (Choice item : resModel.getChoices()) {
outputs.add(item.getText());
}

return outputs;

}

Expand All @@ -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<String> generateCohereText(String model, String prompt, float temperature,
int maxTokens, int numberOfOutputs)
throws IOException {

if (model.equals(""))
Expand All @@ -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<String> outputs = new ArrayList<>();
for (Generation item: resModel.getGenerations()) {
outputs.add(item.getText());
}

return outputs;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public class CohereLanguageResponse extends BaseRemoteModel{
private List<Generation> generations;
private String prompt;

/**
* CohereLanguageResponse default constructor.
*/
public CohereLanguageResponse() {

}

/**
*
* Generation is wrapper for the response
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,6 +28,7 @@ private LanguageModelInput(Builder builder) {
this.prompt = builder.prompt;
this.temperature = builder.temperature;
this.maxTokens = builder.maxTokens;
this.numberOfOutputs = builder.numberOfOutputs;
}
/**
*
Expand All @@ -38,6 +40,7 @@ public static class Builder {
private String prompt;
private float temperature;
private int maxTokens;
private int numberOfOutputs = 1;

/**
* Language input Constructor.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ public static String readStream(InputStream stream) throws IOException {
}
}
return result.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/**
*
* Wrapper for the Cohere API models.
*
* @author github.com/Barqawiz
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,7 +71,7 @@ public BaseRemoteModel generateText(Map<String, Object> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<String> 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() {

Expand Down
Loading