diff --git a/README.md b/README.md index 2449d38..e33cd03 100644 --- a/README.md +++ b/README.md @@ -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 io.github.barqawiz intellijava.core - 0.5.5 + 0.6.0 ``` 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. @@ -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. diff --git a/core/com.intellijava.core/pom.xml b/core/com.intellijava.core/pom.xml index 624725c..4ae96f6 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.5.5 + 0.6.0 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 d507fca..f2d8573 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 @@ -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 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 getSupportedModels() { + SupportedLangModels[] values = SupportedLangModels.values(); + List 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 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 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(); + } } 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 new file mode 100644 index 0000000..da84320 --- /dev/null +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/CohereLanguageResponse.java @@ -0,0 +1,102 @@ +package com.intellijava.core.model; + +import java.util.List; + +/** + * + * CohereTextResponse is a model class used to parse the response from the Cohere text API. + * + * @author github.com/Barqawiz + * + */ +public class CohereLanguageResponse extends BaseRemoteModel{ + + /** A unique identifier for the response.*/ + private List generations; + private String prompt; + + /** + * + * Generation is wrapper for the response + * + * @author github.com/Barqawiz + * + */ + public static class Generation { + private String id; + private String text; + + /** + * Get the unique identifier for the generation. + * + * @return the unique identifier for the generation. + */ + public String getId() { + return id; + } + + /** + * Sets the unique identifier for the generation. + * + * @param id the unique identifier for the generation. + */ + public void setId(String id) { + this.id = id; + } + + /** + * Get the model generated text. + * + * @return the generated text. + */ + public String getText() { + return text; + } + + /** + * Sets the model generated text. + * + * @param text the generated text. + */ + public void setText(String text) { + this.text = text; + } + } + + /** + * Get the list of generated texts. + * + * @return the list of generated texts. + */ + public List getGenerations() { + return generations; + } + + /** + * Sets the list of generated texts. + * + * @param generations the list of generated texts. + */ + public void setGenerations(List generations) { + this.generations = generations; + } + + /** + * + * Get the user prompt. + * + * @return prompt the user input. + */ + public String getPrompt() { + return prompt; + } + + /** + * Sets the user prompt. + * + * @param prompt the user input. + */ + public void setPrompt(String prompt) { + this.prompt = prompt; + } +} diff --git a/core/com.intellijava.core/src/main/java/com/intellijava/core/model/SupportedLangModels.java b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/SupportedLangModels.java new file mode 100644 index 0000000..6b70686 --- /dev/null +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/model/SupportedLangModels.java @@ -0,0 +1,11 @@ +package com.intellijava.core.model; + +/** + * Supported language models. + * + * @author github.com/Barqawiz + * + */ +public enum SupportedLangModels { + /** openai model */openai, /** cohere model */cohere; +} 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 831ff5d..2c7834f 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 @@ -4,6 +4,10 @@ * * LanguageModelInput handle the input parameters for the majority of the remote language models. * + * Language models documentations: + * - Openai : https://beta.openai.com/docs/api-reference/completions. + * - Cohere : https://docs.cohere.ai/reference/generate + * * @author github.com/Barqawiz * */ @@ -45,7 +49,11 @@ public Builder(String prompt) { /** * Setter for model. - * @param model the model name. The largest OpenAI model is text-davinci-002. + * @param model the model name. + * + * The largest OpenAI model is text-davinci-003. + * The largest cohere model is xlarge. + * * @return instance of Builder */ public Builder setModel(String model) { 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 new file mode 100644 index 0000000..9ca36fb --- /dev/null +++ b/core/com.intellijava.core/src/main/java/com/intellijava/core/wrappers/CohereAIWrapper.java @@ -0,0 +1,77 @@ +package com.intellijava.core.wrappers; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +import com.intellijava.core.model.BaseRemoteModel; +import com.intellijava.core.model.CohereLanguageResponse; +import com.intellijava.core.model.OpenaiLanguageResponse; +import com.intellijava.core.utils.Config2; +import com.intellijava.core.utils.ConnHelper; + +/** + * + * + * @author github.com/Barqawiz + * + */ +public class CohereAIWrapper implements LanguageModelInterface{ + + private final String API_BASE_URL = Config2.getInstance().getProperty("url.cohere.base"); + private final String COHERE_VERSION = Config2.getInstance().getProperty("url.cohere.version"); + private String API_KEY; + + /** + * CohereAIWrapper constructor with the API key + * + * @param apiKey cohere API key, generate if from your account. + */ + public CohereAIWrapper(String apiKey) { + this.API_KEY = apiKey; + } + + /** + * + * Generate text from remote large language model based on the received prompt. + * + * @param params key and value for the API parameters + * model the model name, either medium or xlarge. + * prompt text of the required action or the question. + * temperature higher values means more risks and creativity. + * max_tokens maximum size of the model input and output. + * @return BaseRemoteModel for model response + * @throws IOException if there is an error when connecting to the OpenAI API. + */ + @Override + public BaseRemoteModel generateText(Map params) throws IOException { + + String url = API_BASE_URL + Config2.getInstance().getProperty("url.cohere.completions"); + + String json = ConnHelper.convertMaptToJson(params); + + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Authorization", "Bearer " + API_KEY); + connection.setRequestProperty("Cohere-Version", COHERE_VERSION); + connection.setDoOutput(true); + + try (OutputStream outputStream = connection.getOutputStream()) { + outputStream.write(json.getBytes(StandardCharsets.UTF_8)); + } + + if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { + String errorMessage = ConnHelper.getErrorMessage(connection); + throw new IOException(errorMessage); + } + + // get the response and convert to model + CohereLanguageResponse resModel = ConnHelper.convertSteamToModel(connection.getInputStream(), CohereLanguageResponse.class); + return resModel; + } + +} diff --git a/core/com.intellijava.core/src/main/resources/config.properties b/core/com.intellijava.core/src/main/resources/config.properties index 74d398c..5e0ff7f 100644 --- a/core/com.intellijava.core/src/main/resources/config.properties +++ b/core/com.intellijava.core/src/main/resources/config.properties @@ -1,4 +1,8 @@ url.openai.base=https://api.openai.com url.openai.completions=/v1/completions url.openai.imagegenerate=/v1/images/generations -url.openai.testkey= \ No newline at end of file +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/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 new file mode 100644 index 0000000..e1bbeb4 --- /dev/null +++ b/core/com.intellijava.core/src/test/java/com/intellijava/core/CohereModelConnectionTest.java @@ -0,0 +1,119 @@ +package com.intellijava.core; + +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +import com.intellijava.core.controller.RemoteLanguageModel; +import com.intellijava.core.model.CohereLanguageResponse; +import com.intellijava.core.model.CohereLanguageResponse.Generation; +import com.intellijava.core.model.input.LanguageModelInput; +import com.intellijava.core.utils.Config2; +import com.intellijava.core.wrappers.CohereAIWrapper; + +public class CohereModelConnectionTest { + + /** + * coherKey - change the coherKey + */ + private final String coherKey = Config2.getInstance().getProperty("url.cohere.testkey"); + + @Test + public void testLanguageWrapper() { + + // prepare the object + CohereAIWrapper cohereWrapper = new CohereAIWrapper(coherKey); + + // prepare the prompt with training data + String targetIndustryIdea = "electric cars"; + String prompt = "This program generates startup idea and name given the industry." + "\n\nIndustry: Workplace" + + "\nStartup Idea: A platform that generates slide deck contents automatically based on a given outline" + + "\nStartup Name: Deckerize" + "\n--" + "\nIndustry: Home Decor" + + "\nStartup Idea: An app that calculates the best position of your indoor plants for your apartment" + + "\nStartup Name: Planteasy" + "\n--" + "\nIndustry: Healthcare" + + "\nStartup Idea: A hearing aid for the elderly that automatically adjusts its levels and with a battery lasting a whole week" + + "\nStartup Name: Hearspan" + "\n\n--" + "\nIndustry: Education" + + "\nStartup Idea: An online school that lets students mix and match their own curriculum based on their interests and goals" + + "\nStartup Name: Prime Age" + "\n\n--" + + "\nIndustry: {industry}".replace("{industry}", targetIndustryIdea); + + // prepare the input parameters + Map params = new HashMap<>(); + params.put("prompt", prompt); + params.put("model", "xlarge"); + params.put("max_tokens", 40); + params.put("truncate", "END"); + params.put("return_likelihoods", "NONE"); + + // call the API + try { + if (coherKey.isBlank()) + return; + + CohereLanguageResponse resModel = (CohereLanguageResponse) cohereWrapper.generateText(params); + + List responses = resModel.getGenerations(); + + assert responses.size() > 0; + + for (Generation data : responses) { + System.out.println(data.getText().toString()); + } + + } catch (IOException e) { + if (coherKey.isBlank()) { + System.out.print("testLanguageWrapper: set the API key to run the test case."); + } else { + fail("testLanguageWrapper failed with exception: " + e.getMessage()); + } + } + } + + @Test + public void testCohereCompletionRemoteModel() { + + try { + + // prepare the prompt with training data + String targetIndustryIdea = "electric cars"; + String prompt = "This program generates startup idea and name given the industry." + "\n\nIndustry: Workplace" + + "\nStartup Idea: A platform that generates slide deck contents automatically based on a given outline" + + "\nStartup Name: Deckerize" + "\n--" + "\nIndustry: Home Decor" + + "\nStartup Idea: An app that calculates the best position of your indoor plants for your apartment" + + "\nStartup Name: Planteasy" + "\n--" + "\nIndustry: Healthcare" + + "\nStartup Idea: A hearing aid for the elderly that automatically adjusts its levels and with a battery lasting a whole week" + + "\nStartup Name: Hearspan" + "\n\n--" + "\nIndustry: Education" + + "\nStartup Idea: An online school that lets students mix and match their own curriculum based on their interests and goals" + + "\nStartup Name: Prime Age" + "\n\n--" + + "\nIndustry: {industry}".replace("{industry}", targetIndustryIdea); + + RemoteLanguageModel wrapper = new RemoteLanguageModel(coherKey, "cohere"); + + LanguageModelInput input = new LanguageModelInput.Builder(prompt) + .setModel("xlarge").setTemperature(0.7f).setMaxTokens(50).build(); + + if (coherKey.isBlank()) + return; + + String resValue = wrapper.generateText(input); + + System.out.print(resValue); + + assert resValue.length() > 0; + assert resValue.toLowerCase().contains("startup name"); + + } catch (IOException e) { + if (coherKey.isBlank()) { + System.out.print("testLanguageWrapper: set the API key to run the test case."); + } else { + fail("Test case failed with exception: " + e.getMessage()); + } + + } + } + +} diff --git a/core/com.intellijava.core/src/test/java/com/intellijava/core/RemoteModelConnectionTest.java b/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java similarity index 99% rename from core/com.intellijava.core/src/test/java/com/intellijava/core/RemoteModelConnectionTest.java rename to core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java index b5cf7e5..e18743b 100644 --- a/core/com.intellijava.core/src/test/java/com/intellijava/core/RemoteModelConnectionTest.java +++ b/core/com.intellijava.core/src/test/java/com/intellijava/core/OpenaiModelConnectionTest.java @@ -36,7 +36,7 @@ /** * Unit test for Remote Language Model */ -public class RemoteModelConnectionTest { +public class OpenaiModelConnectionTest { /** * openaiKey - change the openaiKey diff --git a/core/com.intellijava.core/target/classes/config.properties b/core/com.intellijava.core/target/classes/config.properties index 74d398c..5e0ff7f 100644 --- a/core/com.intellijava.core/target/classes/config.properties +++ b/core/com.intellijava.core/target/classes/config.properties @@ -1,4 +1,8 @@ url.openai.base=https://api.openai.com url.openai.completions=/v1/completions url.openai.imagegenerate=/v1/images/generations -url.openai.testkey= \ No newline at end of file +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 080298a..1ff9255 100644 --- a/sample_code/.classpath +++ b/sample_code/.classpath @@ -7,7 +7,7 @@ - + diff --git a/sample_code/.gitignore b/sample_code/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/sample_code/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/sample_code/jars/com.intellijava.core-0.4.0.jar b/sample_code/jars/com.intellijava.core-0.4.0.jar deleted file mode 100644 index b2514b6..0000000 Binary files a/sample_code/jars/com.intellijava.core-0.4.0.jar and /dev/null differ diff --git a/sample_code/jars/intellijava.core-0.6.0.jar b/sample_code/jars/intellijava.core-0.6.0.jar new file mode 100644 index 0000000..2525d3c Binary files /dev/null and b/sample_code/jars/intellijava.core-0.6.0.jar differ diff --git a/sample_code/src/com/intelliJava/test/CohereApp.java b/sample_code/src/com/intelliJava/test/CohereApp.java new file mode 100644 index 0000000..2edca7c --- /dev/null +++ b/sample_code/src/com/intelliJava/test/CohereApp.java @@ -0,0 +1,46 @@ +package com.intelliJava.test; + +import java.io.IOException; + +import com.intellijava.core.controller.RemoteLanguageModel; +import com.intellijava.core.model.input.LanguageModelInput; + +public class CohereApp { + + public static void main(String[] args) { + + System.out.println("Start calling the API!"); + + try { + + // get the api key from https://dashboard.cohere.ai/ + // TODO: replace with your API key. + String apiKey = ""; + + /********************************/ + /** 1- Call the language model **/ + /********************************/ + tryTheLanguageModel(apiKey); + + } catch (IOException e) { + e.printStackTrace(); + } + + } + + private static void tryTheLanguageModel(String apiKey) throws IOException { + // initiate the remote language model wrapper with cohere details + RemoteLanguageModel langModel = new RemoteLanguageModel(apiKey, "cohere"); + + // prepare the input parameters - change the prompt to any text like "write a + // funny short story" + LanguageModelInput langInput = new LanguageModelInput.Builder("Once upon a time in a magical land called") + .setModel("xlarge").setTemperature(0.7f).setMaxTokens(50).build(); + + String resValue = langModel.generateText(langInput); + + // print language model output + System.out.println("Language model output:\n" + resValue); + } + +} diff --git a/sample_code/src/com/intelliJava/test/App.java b/sample_code/src/com/intelliJava/test/OpenaiApp.java similarity index 99% rename from sample_code/src/com/intelliJava/test/App.java rename to sample_code/src/com/intelliJava/test/OpenaiApp.java index b31200b..966d805 100644 --- a/sample_code/src/com/intelliJava/test/App.java +++ b/sample_code/src/com/intelliJava/test/OpenaiApp.java @@ -28,7 +28,7 @@ * * */ -public class App { +public class OpenaiApp { public static void main(String[] args) {