Skip to content

Commit 7d55282

Browse files
authored
Merge pull request #2 from Barqawiz/cohere-integration
Cohere integration and flexible language model.
2 parents 3312a80 + 1249941 commit 7d55282

File tree

17 files changed

+553
-70
lines changed

17 files changed

+553
-70
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
# IntelliJava-OpenaiAPI
2-
*IntelliJava V0.5.5*
2+
*IntelliJava V0.6.0*
33

4-
IntelliJava allows java developers to easily integrate with the latest language models and deep learning frameworks using few lines of java code.
5-
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.
4+
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.
5+
6+
The supported models in this version:
7+
- OpenAI: Access GPT-3 to generate text and DALL·E to generate images. OpenAI is preferred when you want quality results without tuning.
8+
- Cohere.ai: generate text; Cohere allows you to generate your language model to suit your specific needs.
69

710

811
# How to use
912
1. Import the core jar file to your project or add the maven package (check Integration section).
1013
2. Add gson dependency using maven or the jar file (check dependencies section).
11-
3. Call the ``RemoteLanguageModel`` for the language model and ``RemoateImageModel`` for image generation.
14+
3. Call the ``RemoteLanguageModel`` for the language models and ``RemoateImageModel`` for image generation.
1215

1316
## Integration
14-
The package released to [Maven Central Repository](https://central.sonatype.dev/artifact/io.github.barqawiz/intellijava.core/0.5.5).
17+
The package released to [Maven Central Repository](https://central.sonatype.dev/artifact/io.github.barqawiz/intellijava.core/0.6.0).
1518

1619
Maven:
1720
```xml
1821
<dependency>
1922
<groupId>io.github.barqawiz</groupId>
2023
<artifactId>intellijava.core</artifactId>
21-
<version>0.5.5</version>
24+
<version>0.6.0</version>
2225
</dependency>
2326
```
2427

2528
Gradle:
2629

2730
```
28-
implementation group: 'io.github.barqawiz', name: 'intellijava.core', version: '0.5.5'
31+
implementation group: 'io.github.barqawiz', name: 'intellijava.core', version: '0.6.0'
2932
```
3033

3134
Gradle(Kotlin):
3235
```
33-
implementation("io.github.barqawiz:intellijava.core:0.5.5")
36+
implementation("io.github.barqawiz:intellijava.core:0.6.0")
3437
```
3538

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

3942
For ready integration: try the sample_code.
4043

@@ -95,7 +98,7 @@ Call for contributors:
9598
- [x] Add support to OpenAI Completion API.
9699
- [x] Add support to OpenAI DALL·E 2.
97100
- [ ] Add support to other OpenAI functions.
98-
- [ ] Add support to cohere generate API.
101+
- [x] Add support to cohere generate API.
99102
- [ ] Add support to Google language models.
100103
- [ ] Add support to Amazon language models.
101104
- [ ] Add support to Midjourney image generation.

core/com.intellijava.core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.barqawiz</groupId>
88
<artifactId>intellijava.core</artifactId>
9-
<version>0.5.5</version>
9+
<version>0.6.0</version>
1010

1111
<name>Intellijava</name>
1212
<description>IntelliJava allows java developers to easily integrate with the latest language models, image generation, and deep learning frameworks.</description>

core/com.intellijava.core/src/main/java/com/intellijava/core/controller/RemoteLanguageModel.java

Lines changed: 161 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,93 +16,201 @@
1616
package com.intellijava.core.controller;
1717

1818
import java.io.IOException;
19+
import java.util.ArrayList;
1920
import java.util.HashMap;
21+
import java.util.List;
2022
import java.util.Map;
21-
23+
import com.intellijava.core.model.CohereLanguageResponse;
2224
import com.intellijava.core.model.OpenaiLanguageResponse;
25+
import com.intellijava.core.model.SupportedLangModels;
2326
import com.intellijava.core.model.input.LanguageModelInput;
27+
import com.intellijava.core.wrappers.CohereAIWrapper;
2428
import com.intellijava.core.wrappers.OpenAIWrapper;
2529

2630
/**
27-
* A class to call the most sophisticated remote language models.
28-
*
29-
* This class provides an API for interacting with OpenAI's GPT-3 language model. It is designed to be easily extensible
30-
* to support other models in the future.
31+
* RemoteLanguageModel class to call the most sophisticated remote language
32+
* models.
33+
*
34+
* This class support: - Openai: - url: openai.com - description: provides an
35+
* API for interacting with OpenAI's GPT-3 language model. - model names :
36+
* text-davinci-003, text-curie-001, text-babbage-001, more.
37+
*
38+
* - cohere: - url: cohere.ai - description: provides an API for interacting
39+
* with generate language model. - it is recommended to fine tune your model or
40+
* add example of the response in the prompt when calling cohere models. - model
41+
* names : medium or xlarge
3142
*
3243
* @author github.com/Barqawiz
3344
*
3445
*/
3546
public class RemoteLanguageModel {
36-
37-
private String keyType;
47+
48+
private SupportedLangModels keyType;
3849
private OpenAIWrapper openaiWrapper;
39-
50+
private CohereAIWrapper cohereWrapper;
51+
4052
/**
41-
* Constructor for the RemoteLanguageModel class.
42-
*
43-
* Creates an instance of the class and sets up the API key and the key type.
44-
* Currently, only the "openai" key type is supported.
45-
*
46-
* @param keyValue the API key.
47-
* @param keyType support openai only.
48-
*
49-
* @throws IllegalArgumentException if the keyType passed is not "openai".
50-
*
51-
*/
52-
public RemoteLanguageModel(String keyValue, String keyType) {
53-
54-
if (keyType.isEmpty() || keyType.equals("openai")) {
55-
this.keyType = "openai";
56-
openaiWrapper = new OpenAIWrapper(keyValue);
53+
* Constructor for the RemoteLanguageModel class.
54+
*
55+
* Creates an instance of the class and sets up the key and the API type.
56+
*
57+
* @param keyValue the API key.
58+
* @param keyTypeString either openai (default) or cohere or send empty string
59+
* for default value.
60+
*
61+
* @throws IllegalArgumentException if the keyType passed is not "openai".
62+
*
63+
*/
64+
public RemoteLanguageModel(String keyValue, String keyTypeString) {
65+
66+
if (keyTypeString.isEmpty()) {
67+
keyTypeString = SupportedLangModels.openai.toString();
68+
}
69+
70+
List<String> supportedModels = this.getSupportedModels();
71+
72+
if (supportedModels.contains(keyTypeString)) {
73+
this.initiate(keyValue, SupportedLangModels.valueOf(keyTypeString));
5774
} else {
58-
throw new IllegalArgumentException("This version support openai keyType only");
75+
String models = String.join(" - ", supportedModels);
76+
throw new IllegalArgumentException("The received keyValue not supported. Send any model from: " + models);
5977
}
6078
}
61-
79+
80+
/**
81+
* Constructor for the RemoteLanguageModel class.
82+
*
83+
* Creates an instance of the class and sets up the API key and the enum key
84+
* type.
85+
*
86+
* @param keyValue the API key.
87+
* @param keyType enum version from the key type (SupportedModels).
88+
*
89+
* @throws IllegalArgumentException if the keyType passed is not "openai".
90+
*
91+
*/
92+
public RemoteLanguageModel(String keyValue, SupportedLangModels keyType) {
93+
this.initiate(keyValue, keyType);
94+
}
95+
96+
/**
97+
* Get the supported models names as array of string
98+
*
99+
* @return supportedModels
100+
*/
101+
public List<String> getSupportedModels() {
102+
SupportedLangModels[] values = SupportedLangModels.values();
103+
List<String> enumValues = new ArrayList<>();
104+
105+
for (int i = 0; i < values.length; i++) {
106+
enumValues.add(values[i].name());
107+
}
108+
109+
return enumValues;
110+
}
62111

112+
/**
113+
* Common function to initiate the class from any constructor.
114+
*
115+
* @param keyValue the API key.
116+
* @param keyType enum version from the key type (SupportedModels).
117+
*/
118+
private void initiate(String keyValue, SupportedLangModels keyType) {
119+
// set the model type
120+
this.keyType = keyType;
121+
122+
// generate the related model
123+
if (keyType.equals(SupportedLangModels.openai)) {
124+
this.openaiWrapper = new OpenAIWrapper(keyValue);
125+
} else if (keyType.equals(SupportedLangModels.cohere)) {
126+
this.cohereWrapper = new CohereAIWrapper(keyValue);
127+
}
128+
}
129+
63130
/**
64131
*
65132
* Call a remote large model to generate any text based on the received prompt.
66133
*
67134
* @param langInput flexible builder for language model parameters.
135+
*
68136
* @return string for the model response.
69-
* @throws IOException if there is an error when connecting to the OpenAI API.
70-
* @throws IllegalArgumentException if the keyType passed in the constructor is not "openai".
137+
* @throws IOException if there is an error when connecting to the
138+
* OpenAI API.
139+
* @throws IllegalArgumentException if the keyType passed in the constructor is
140+
* not "openai".
71141
*
72142
*/
73-
public String generateText(LanguageModelInput langInput) throws IOException {
74-
75-
if (this.keyType.equals("openai")) {
76-
return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(),
77-
langInput.getTemperature(), langInput.getMaxTokens());
143+
public String generateText(LanguageModelInput langInput) throws IOException {
144+
145+
if (this.keyType.equals(SupportedLangModels.openai)) {
146+
return this.generateOpenaiText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(),
147+
langInput.getMaxTokens());
148+
} else if (this.keyType.equals(SupportedLangModels.cohere)) {
149+
return this.generateCohereText(langInput.getModel(), langInput.getPrompt(), langInput.getTemperature(),
150+
langInput.getMaxTokens());
78151
} else {
79152
throw new IllegalArgumentException("This version support openai keyType only");
80153
}
81-
154+
82155
}
83156

84157
/**
85-
* Private helper method for generating text from OpenAI GPT-3 model.
86-
*
87-
* @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
88-
* @param prompt text of the required action or the question.
89-
* @param temperature higher values means more risks and creativity.
90-
* @param maxTokens maximum size of the model input and output.
91-
* @return string model response.
92-
* @throws IOException if there is an error when connecting to the OpenAI API.
93-
*
94-
*/
95-
private String generateOpenaiText(String model, String prompt, float temperature, int maxTokens) throws IOException {
96-
158+
* Private helper method for generating text from OpenAI GPT-3 model.
159+
*
160+
* @param model the model name, example: text-davinci-003. For more
161+
* details about GPT-3 models, see:
162+
* https://beta.openai.com/docs/models/gpt-3
163+
* @param prompt text of the required action or the question.
164+
* @param temperature higher values means more risks and creativity.
165+
* @param maxTokens maximum size of the model input and output.
166+
* @return string model response.
167+
* @throws IOException if there is an error when connecting to the OpenAI API.
168+
*
169+
*/
170+
private String generateOpenaiText(String model, String prompt, float temperature, int maxTokens)
171+
throws IOException {
172+
173+
if (model.equals(""))
174+
model = "text-davinci-003";
175+
97176
Map<String, Object> params = new HashMap<>();
98-
params.put("model", model);
99-
params.put("prompt", prompt);
100-
params.put("temperature", temperature);
101-
params.put("max_tokens", maxTokens);
102-
177+
params.put("model", model);
178+
params.put("prompt", prompt);
179+
params.put("temperature", temperature);
180+
params.put("max_tokens", maxTokens);
181+
103182
OpenaiLanguageResponse resModel = (OpenaiLanguageResponse) openaiWrapper.generateText(params);
104-
183+
105184
return resModel.getChoices().get(0).getText();
106-
185+
186+
}
187+
188+
/**
189+
* Private helper method for generating text from Cohere model.
190+
*
191+
* @param model the model name, either medium or xlarge.
192+
* @param prompt text of the required action or the question.
193+
* @param temperature higher values means more risks and creativity.
194+
* @param maxTokens maximum size of the model input and output.
195+
* @return string model response.
196+
* @throws IOException if there is an error when connecting to the API.
197+
*
198+
*/
199+
private String generateCohereText(String model, String prompt, float temperature, int maxTokens)
200+
throws IOException {
201+
202+
if (model.equals(""))
203+
model = "xlarge";
204+
205+
Map<String, Object> params = new HashMap<>();
206+
params.put("model", model);
207+
params.put("prompt", prompt);
208+
params.put("temperature", temperature);
209+
params.put("max_tokens", maxTokens);
210+
211+
CohereLanguageResponse resModel = (CohereLanguageResponse) cohereWrapper.generateText(params);
212+
213+
return resModel.getGenerations().get(0).getText();
214+
107215
}
108216
}

0 commit comments

Comments
 (0)