|
| 1 | +package com.intellijava.core.function; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import com.intellijava.core.model.OpenaiChatResponse; |
| 10 | +import com.intellijava.core.model.OpenaiChatResponse.Choice; |
| 11 | +import com.intellijava.core.model.SupportedChatModels; |
| 12 | +import com.intellijava.core.model.input.ChatGPTInput; |
| 13 | +import com.intellijava.core.model.input.ChatModelInput; |
| 14 | +import com.intellijava.core.wrappers.OpenAIWrapper; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * Chatbot controller for most sophisticated AI chatbots. |
| 19 | + * |
| 20 | + * @author github.com/Barqawiz |
| 21 | + * |
| 22 | + */ |
| 23 | +public class Chatbot { |
| 24 | + |
| 25 | + private SupportedChatModels keyType; |
| 26 | + private OpenAIWrapper openaiWrapper; |
| 27 | + |
| 28 | + /** |
| 29 | + * |
| 30 | + * Constructor for the Chatbot class. |
| 31 | + * |
| 32 | + * Creates class instance and set the API type and key. |
| 33 | + * |
| 34 | + * @param keyValue the API key. |
| 35 | + * @param keyTypeString either openai (default) or cohere or send empty string |
| 36 | + * for default value. |
| 37 | + * |
| 38 | + * @throws IllegalArgumentException if the keyType passed is not "openai". |
| 39 | + * |
| 40 | + */ |
| 41 | + public Chatbot(String keyValue, String keyTypeString) { |
| 42 | + |
| 43 | + if (keyTypeString.isEmpty()) { |
| 44 | + keyTypeString = SupportedChatModels.openai.toString(); |
| 45 | + } |
| 46 | + |
| 47 | + List<String> supportedModels = this.getSupportedModels(); |
| 48 | + |
| 49 | + if (supportedModels.contains(keyTypeString)) { |
| 50 | + this.initiate(keyValue, SupportedChatModels.valueOf(keyTypeString)); |
| 51 | + } else { |
| 52 | + String models = String.join(" - ", supportedModels); |
| 53 | + throw new IllegalArgumentException("The received keyValue not supported. Send any model from: " + models); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructor for the Chatbot class. |
| 59 | + * |
| 60 | + * Creates class instance and set the API enum type and key. |
| 61 | + * |
| 62 | + * @param keyValue the API key. |
| 63 | + * @param keyType enum version from the key type (SupportedModels). |
| 64 | + * |
| 65 | + * @throws IllegalArgumentException if the keyType passed is not "openai". |
| 66 | + * |
| 67 | + */ |
| 68 | + public Chatbot(String keyValue, SupportedChatModels keyType) { |
| 69 | + this.initiate(keyValue, keyType); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get the supported models names as array of string |
| 74 | + * |
| 75 | + * @return supportedModels |
| 76 | + */ |
| 77 | + public List<String> getSupportedModels() { |
| 78 | + SupportedChatModels[] values = SupportedChatModels.values(); |
| 79 | + List<String> enumValues = new ArrayList<>(); |
| 80 | + |
| 81 | + for (int i = 0; i < values.length; i++) { |
| 82 | + enumValues.add(values[i].name()); |
| 83 | + } |
| 84 | + |
| 85 | + return enumValues; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Common function to initiate the class from any constructor. |
| 90 | + * |
| 91 | + * @param keyValue the API key. |
| 92 | + * @param keyType enum version from the key type (SupportedModels). |
| 93 | + */ |
| 94 | + private void initiate(String keyValue, SupportedChatModels keyType) { |
| 95 | + // set the model type |
| 96 | + this.keyType = keyType; |
| 97 | + |
| 98 | + // generate the related model |
| 99 | + if (keyType.equals(SupportedChatModels.openai)) { |
| 100 | + this.openaiWrapper = new OpenAIWrapper(keyValue); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * |
| 106 | + * Call a chat model to generate response based on the received messages history. |
| 107 | + * |
| 108 | + * To support multiple response call the variation function generateMultiText. |
| 109 | + * |
| 110 | + * @param modelInput language model parameters. |
| 111 | + * |
| 112 | + * @return the model response. |
| 113 | + * |
| 114 | + * @throws IOException if there is an error when connecting to the chat model. |
| 115 | + * |
| 116 | + */ |
| 117 | + public List<String> chat(ChatModelInput modelInput) throws IOException { |
| 118 | + |
| 119 | + if (this.keyType.equals(SupportedChatModels.openai)) { |
| 120 | + return this.chatGPT((ChatGPTInput) modelInput); |
| 121 | + } else { |
| 122 | + throw new IllegalArgumentException("the keyType not supported"); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** |
| 128 | + * |
| 129 | + * Call a chatGPT to generate response based on the received messages history. |
| 130 | + * |
| 131 | + * @param modelInput for chatGPT. |
| 132 | + * |
| 133 | + * @return string for the model response. |
| 134 | + * |
| 135 | + * @throws IOException if there is an error when connecting to the chatGPT. |
| 136 | + */ |
| 137 | + private List<String> chatGPT(ChatGPTInput modelInput) throws IOException { |
| 138 | + |
| 139 | + Map<String, Object> params = new HashMap<>(); |
| 140 | + params.put("model", modelInput.getModel()); |
| 141 | + params.put("messages", modelInput.getMessages()); |
| 142 | + if (modelInput.getNumberOfOutputs() >= 0) { |
| 143 | + params.put("n", modelInput.getNumberOfOutputs()); |
| 144 | + } |
| 145 | + |
| 146 | + if (modelInput.getTemperature() >= 0) { |
| 147 | + params.put("temperature", modelInput.getTemperature()); |
| 148 | + } |
| 149 | + |
| 150 | + if (modelInput.getMaxTokens() >= 1) { |
| 151 | + params.put("max_tokens", modelInput.getMaxTokens()); |
| 152 | + } |
| 153 | + |
| 154 | + OpenaiChatResponse resModel = (OpenaiChatResponse) openaiWrapper.generateChatText(params); |
| 155 | + |
| 156 | + List<String> outputs = new ArrayList<>(); |
| 157 | + for (Choice item : resModel.getChoices()) { |
| 158 | + outputs.add(item.getMessage().getContent()); |
| 159 | + } |
| 160 | + |
| 161 | + return outputs; |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | +} |
0 commit comments