Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cohere integration #2

Merged
merged 7 commits into from
Jan 19, 2023
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
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
# IntelliJava-OpenaiAPI
*IntelliJava V0.5.5*
*IntelliJava V0.6.0*

IntelliJava allows java developers to easily integrate with the latest language models and deep learning frameworks using few lines of java code.
The first version supports only Openai APIs. It 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.
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.

The supported models in this version:
- OpenAI: Access GPT-3 to generate text and DALL·E to generate images. OpenAI is preferred when you want quality results without tuning.
- Cohere.ai: generate text; Cohere allows you to generate your language model to suit your specific needs.


# How to use
1. Import the core jar file to your project or add the maven package (check Integration section).
2. Add gson dependency using maven or the jar file (check dependencies section).
3. Call the ``RemoteLanguageModel`` for the language model and ``RemoateImageModel`` for image generation.
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.5.5).
The package released to [Maven Central Repository](https://central.sonatype.dev/artifact/io.github.barqawiz/intellijava.core/0.6.0).

Maven:
```xml
<dependency>
<groupId>io.github.barqawiz</groupId>
<artifactId>intellijava.core</artifactId>
<version>0.5.5</version>
<version>0.6.0</version>
</dependency>
```

Gradle:

```
implementation group: 'io.github.barqawiz', name: 'intellijava.core', version: '0.5.5'
implementation group: 'io.github.barqawiz', name: 'intellijava.core', version: '0.6.0'
```

Gradle(Kotlin):
```
implementation("io.github.barqawiz:intellijava.core:0.5.5")
implementation("io.github.barqawiz:intellijava.core:0.6.0")
```

Jar download:
[intellijava.jar](https://insta-answer-public.s3.amazonaws.com/opensource/IntelliJava/version0.5.5/intellijava.core-0.5.5.jar).
[intellijava.jar](https://repo1.maven.org/maven2/io/github/barqawiz/intellijava.core/0.6.0/intellijava.core-0.6.0.jar).

For ready integration: try the sample_code.

Expand Down Expand Up @@ -95,7 +98,7 @@ Call for contributors:
- [x] Add support to OpenAI Completion API.
- [x] Add support to OpenAI DALL·E 2.
- [ ] Add support to other OpenAI functions.
- [ ] Add support to cohere generate API.
- [x] Add support to cohere generate API.
- [ ] Add support to Google language models.
- [ ] Add support to Amazon language models.
- [ ] Add support to Midjourney image generation.
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.5.5</version>
<version>0.6.0</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 @@ -16,93 +16,201 @@
package com.intellijava.core.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.intellijava.core.model.CohereLanguageResponse;
import com.intellijava.core.model.OpenaiLanguageResponse;
import com.intellijava.core.model.SupportedLangModels;
import com.intellijava.core.model.input.LanguageModelInput;
import com.intellijava.core.wrappers.CohereAIWrapper;
import com.intellijava.core.wrappers.OpenAIWrapper;

/**
* A class to call the most sophisticated remote language models.
*
* This class provides an API for interacting with OpenAI's GPT-3 language model. It is designed to be easily extensible
* to support other models in the future.
* RemoteLanguageModel class to call the most sophisticated remote language
* models.
*
* This class support: - Openai: - url: openai.com - description: provides an
* API for interacting with OpenAI's GPT-3 language model. - model names :
* text-davinci-003, text-curie-001, text-babbage-001, more.
*
* - cohere: - url: cohere.ai - description: provides an API for interacting
* with generate language model. - it is recommended to fine tune your model or
* add example of the response in the prompt when calling cohere models. - model
* names : medium or xlarge
*
* @author github.com/Barqawiz
*
*/
public class RemoteLanguageModel {
private String keyType;

private SupportedLangModels keyType;
private OpenAIWrapper openaiWrapper;

private CohereAIWrapper cohereWrapper;

/**
* Constructor for the RemoteLanguageModel class.
*
* Creates an instance of the class and sets up the API key and the key type.
* Currently, only the "openai" key type is supported.
*
* @param keyValue the API key.
* @param keyType support openai only.
*
* @throws IllegalArgumentException if the keyType passed is not "openai".
*
*/
public RemoteLanguageModel(String keyValue, String keyType) {

if (keyType.isEmpty() || keyType.equals("openai")) {
this.keyType = "openai";
openaiWrapper = new OpenAIWrapper(keyValue);
* Constructor for the RemoteLanguageModel class.
*
* Creates an instance of the class and sets up the key and the API type.
*
* @param keyValue the API key.
* @param keyTypeString either openai (default) or cohere or send empty string
* for default value.
*
* @throws IllegalArgumentException if the keyType passed is not "openai".
*
*/
public RemoteLanguageModel(String keyValue, String keyTypeString) {

if (keyTypeString.isEmpty()) {
keyTypeString = SupportedLangModels.openai.toString();
}

List<String> supportedModels = this.getSupportedModels();

if (supportedModels.contains(keyTypeString)) {
this.initiate(keyValue, SupportedLangModels.valueOf(keyTypeString));
} else {
throw new IllegalArgumentException("This version support openai keyType only");
String models = String.join(" - ", supportedModels);
throw new IllegalArgumentException("The received keyValue not supported. Send any model from: " + models);
}
}


/**
* Constructor for the RemoteLanguageModel class.
*
* Creates an instance of the class and sets up the API key and the enum key
* type.
*
* @param keyValue the API key.
* @param keyType enum version from the key type (SupportedModels).
*
* @throws IllegalArgumentException if the keyType passed is not "openai".
*
*/
public RemoteLanguageModel(String keyValue, SupportedLangModels keyType) {
this.initiate(keyValue, keyType);
}

/**
* Get the supported models names as array of string
*
* @return supportedModels
*/
public List<String> getSupportedModels() {
SupportedLangModels[] values = SupportedLangModels.values();
List<String> enumValues = new ArrayList<>();

for (int i = 0; i < values.length; i++) {
enumValues.add(values[i].name());
}

return enumValues;
}

/**
* Common function to initiate the class from any constructor.
*
* @param keyValue the API key.
* @param keyType enum version from the key type (SupportedModels).
*/
private void initiate(String keyValue, SupportedLangModels keyType) {
// set the model type
this.keyType = keyType;

// generate the related model
if (keyType.equals(SupportedLangModels.openai)) {
this.openaiWrapper = new OpenAIWrapper(keyValue);
} else if (keyType.equals(SupportedLangModels.cohere)) {
this.cohereWrapper = new CohereAIWrapper(keyValue);
}
}

/**
*
* Call a remote large model to generate any text based on the received prompt.
*
* @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.
* @throws IllegalArgumentException if the keyType passed in the constructor is not "openai".
* @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 String generateText(LanguageModelInput langInput) throws IOException {

if (this.keyType.equals("openai")) {
return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(),
langInput.getTemperature(), langInput.getMaxTokens());
public String generateText(LanguageModelInput langInput) throws IOException {

if (this.keyType.equals(SupportedLangModels.openai)) {
return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(),
langInput.getMaxTokens());
} else if (this.keyType.equals(SupportedLangModels.cohere)) {
return this.generateCohereText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(),
langInput.getMaxTokens());
} else {
throw new IllegalArgumentException("This version support openai keyType only");
}

}

/**
* Private helper method for generating text from OpenAI GPT-3 model.
*
* @param model the model name, example: text-davinci-002. For more details about GPT-3 models, see: https://beta.openai.com/docs/models/gpt-3
* @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.
* @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) throws IOException {

* Private helper method for generating text from OpenAI GPT-3 model.
*
* @param model the model name, example: text-davinci-003. For more
* details about GPT-3 models, see:
* https://beta.openai.com/docs/models/gpt-3
* @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.
* @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)
throws IOException {

if (model.equals(""))
model = "text-davinci-003";

Map<String, Object> params = new HashMap<>();
params.put("model", model);
params.put("prompt", prompt);
params.put("temperature", temperature);
params.put("max_tokens", maxTokens);
params.put("model", model);
params.put("prompt", prompt);
params.put("temperature", temperature);
params.put("max_tokens", maxTokens);

OpenaiLanguageResponse resModel = (OpenaiLanguageResponse) openaiWrapper.generateText(params);

return resModel.getChoices().get(0).getText();


}

/**
* Private helper method for generating text from Cohere model.
*
* @param model the model name, either medium or xlarge.
* @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.
* @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)
throws IOException {

if (model.equals(""))
model = "xlarge";

Map<String, Object> params = new HashMap<>();
params.put("model", model);
params.put("prompt", prompt);
params.put("temperature", temperature);
params.put("max_tokens", maxTokens);

CohereLanguageResponse resModel = (CohereLanguageResponse) cohereWrapper.generateText(params);

return resModel.getGenerations().get(0).getText();

}
}
Loading