Skip to content

Commit

Permalink
Merge pull request #16 from jeddict/dev
Browse files Browse the repository at this point in the history
Anthropic support (claude-3-5-sonnet & claude-3-haiku)
  • Loading branch information
jShiwaniGupta authored Oct 17, 2024
2 parents 4529b35 + 5fdeefb commit a02b93a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 43 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@
<artifactId>langchain4j-ollama</artifactId>
<version>0.34.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic</artifactId>
<version>0.34.0</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-google-ai-gemini</artifactId>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
import dev.langchain4j.model.anthropic.AnthropicChatModel;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.googleai.GoogleAiGeminiChatModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
Expand Down Expand Up @@ -51,8 +52,8 @@ public class JeddictChatModel {
PreferencesManager preferencesManager = PreferencesManager.getInstance();

public JeddictChatModel() {
if (null != preferencesManager.getModel().getProvider()) {
switch (preferencesManager.getModel().getProvider()) {
if (null != preferencesManager.getModel()) {
switch (preferencesManager.getProvider()) {
case GOOGLE -> model = GoogleAiGeminiChatModel.builder()
.apiKey(preferencesManager.getApiKey())
.modelName(preferencesManager.getModelName())
Expand All @@ -65,6 +66,10 @@ public JeddictChatModel() {
.baseUrl(preferencesManager.getProviderLocation())
.modelName(preferencesManager.getModelName())
.build();
case ANTHROPIC -> model = AnthropicChatModel.builder()
.apiKey(preferencesManager.getApiKey())
.modelName(preferencesManager.getModelName())
.build();
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/io/github/jeddict/ai/settings/AIAssistancePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package io.github.jeddict.ai.settings;

import io.github.jeddict.ai.scanner.ProjectClassScanner;
import static io.github.jeddict.ai.settings.GenAIModel.models;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.JOptionPane;
import static io.github.jeddict.ai.settings.GenAIModel.MODELS;

final class AIAssistancePanel extends javax.swing.JPanel {

Expand Down Expand Up @@ -287,7 +287,9 @@ private void showDescriptionCheckBoxActionPerformed(java.awt.event.ActionEvent e

private void providerComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_providerComboBoxActionPerformed
GenAIProvider selectedProvider = (GenAIProvider) providerComboBox.getSelectedItem();
if (selectedProvider == GenAIProvider.GOOGLE || selectedProvider == GenAIProvider.OPEN_AI) {
if (selectedProvider == GenAIProvider.GOOGLE
|| selectedProvider == GenAIProvider.OPEN_AI
|| selectedProvider == GenAIProvider.ANTHROPIC) {
providerKeyLabel.setText("API Key:");
providerLocationField.setText("");
providerLocationField.setVisible(false);
Expand Down Expand Up @@ -323,14 +325,14 @@ private List<String> getModelList(GenAIProvider selectedProvider) {
return names;
}
}
return models.values().stream()
return MODELS.values().stream()
.filter(model -> model.getProvider().equals(selectedProvider))
.map(GenAIModel::getName)
.collect(Collectors.toList());
}

private GenAIModel getModel(String modelName) {
return models.get(modelName);
return MODELS.get(modelName);
}

private final PreferencesManager preferencesManager = PreferencesManager.getInstance();
Expand All @@ -350,10 +352,12 @@ void load() {
}

providerComboBox.setSelectedItem(preferencesManager.getProvider());
modelComboBox.setSelectedItem(preferencesManager.getModel().getName());
modelComboBox.setSelectedItem(preferencesManager.getModel());
showDescriptionCheckBox.setSelected(preferencesManager.isDescriptionEnabled());
GenAIProvider selectedProvider = (GenAIProvider) providerComboBox.getSelectedItem();
if (selectedProvider == GenAIProvider.GOOGLE || selectedProvider == GenAIProvider.OPEN_AI) {
if (selectedProvider == GenAIProvider.GOOGLE
|| selectedProvider == GenAIProvider.OPEN_AI
|| selectedProvider == GenAIProvider.ANTHROPIC) {
apiKeyField.setText(preferencesManager.getApiKey(true));
} else if (selectedProvider == GenAIProvider.OLLAMA) {
providerLocationField.setText(preferencesManager.getProviderLocation());
Expand All @@ -370,7 +374,9 @@ void store() {
preferencesManager.setDescriptionEnabled(showDescriptionCheckBox.isSelected());

GenAIProvider selectedProvider = (GenAIProvider) providerComboBox.getSelectedItem();
if (selectedProvider == GenAIProvider.GOOGLE || selectedProvider == GenAIProvider.OPEN_AI) {
if (selectedProvider == GenAIProvider.GOOGLE
|| selectedProvider == GenAIProvider.OPEN_AI
|| selectedProvider == GenAIProvider.ANTHROPIC) {
preferencesManager.setApiKey(new String(apiKeyField.getPassword()));
} else if (selectedProvider == GenAIProvider.OLLAMA) {
preferencesManager.setProviderLocation(providerLocationField.getText());
Expand Down
60 changes: 38 additions & 22 deletions src/main/java/io/github/jeddict/ai/settings/GenAIModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package io.github.jeddict.ai.settings;

import static io.github.jeddict.ai.settings.GenAIProvider.ANTHROPIC;
import static io.github.jeddict.ai.settings.GenAIProvider.GOOGLE;
import static io.github.jeddict.ai.settings.GenAIProvider.OLLAMA;
import static io.github.jeddict.ai.settings.GenAIProvider.OPEN_AI;
Expand All @@ -32,37 +33,44 @@
public class GenAIModel {

public static String DEFAULT_MODEL = "gpt-4o-mini";
public static Map<String, GenAIModel> models = new HashMap<>();
public static Map<String, GenAIModel> MODELS = new HashMap<>();

static {
models.put("gemini-1.5-flash", new GenAIModel(GOOGLE, "gemini-1.5-flash", "A fast and cost-effective model for rapid assessments. Highly recommended."));

models.put("llama3.1:latest", new GenAIModel(OLLAMA, "llama3.1:latest", "Optimized for lightweight tasks that require quick responses."));

models.put("llama3.2:latest", new GenAIModel(OLLAMA, "llama3.2:latest", "Efficient for tasks needing fast turnaround without heavy resources."));

models.put("gemma2:latest", new GenAIModel(OLLAMA, "gemma2:latest", "Designed for speed and quality in lightweight applications."));

models.put("qwen2.5:latest", new GenAIModel(OLLAMA, "qwen2.5:latest", "Compact and efficient, ideal for responsive tasks."));

models.put("deepseek-coder-v2:latest", new GenAIModel(OLLAMA, "deepseek-coder-v2:latest",
"Specialized for coding tasks, offering quick assistance."));

models.put("gpt-4o-mini", new GenAIModel(OPEN_AI, "gpt-4o-mini", "Highly recommended for its excellent balance of performance and cost."));

models.put("gpt-4-turbo", new GenAIModel(OPEN_AI, "gpt-4-turbo", "An advanced model providing great speed and reliability for diverse tasks."));

models.put("gpt-4o", new GenAIModel(OPEN_AI, "gpt-4o", "The premium choice for complex tasks requiring deep analysis and understanding."));
}
MODELS.put("gemini-1.5-flash", new GenAIModel(GOOGLE, "gemini-1.5-flash", "A fast and cost-effective model for rapid assessments. Highly recommended.",
0.075, 0.30));
MODELS.put("gemini-1.5-pro", new GenAIModel(GOOGLE, "gemini-1.5-pro", "A professional version of the Gemini model with enhanced capabilities.",
1.25, 5.00));

MODELS.put("gpt-4o-mini", new GenAIModel(OPEN_AI, "gpt-4o-mini", "Highly recommended for its excellent balance of performance and cost.",
0.150, 0.600));
MODELS.put("o1-mini", new GenAIModel(OPEN_AI, "o1-mini", "A compact model for diverse tasks.",
3.00, 12.00));
MODELS.put("chatgpt-4o-latest", new GenAIModel(OPEN_AI, "chatgpt-4o-latest", "Latest ChatGPT model offering powerful capabilities.",
5.00, 15.00));
MODELS.put("gpt-4o", new GenAIModel(OPEN_AI, "gpt-4o", "The premium choice for complex tasks requiring deep analysis and understanding.",
2.50, 10.00));

MODELS.put("claude-3-5-sonnet-20240620", new GenAIModel(ANTHROPIC, "claude-3-5-sonnet-20240620", "A sonnet model offering refined conversational capabilities.", 3.00, 15.00));
MODELS.put("claude-3-haiku-20240307", new GenAIModel(ANTHROPIC, "claude-3-haiku-20240307", "A haiku model designed for concise and creative expression.", 0.25, 1.25));

MODELS.put("llama3.1:latest", new GenAIModel(OLLAMA, "llama3.1:latest", "Optimized for lightweight tasks that require quick responses.", 0.0, 0.0));
MODELS.put("llama3.2:latest", new GenAIModel(OLLAMA, "llama3.2:latest", "Efficient for tasks needing fast turnaround without heavy resources.", 0.0, 0.0));
MODELS.put("gemma2:latest", new GenAIModel(OLLAMA, "gemma2:latest", "Designed for speed and quality in lightweight applications.", 0.0, 0.0));
MODELS.put("qwen2.5:latest", new GenAIModel(OLLAMA, "qwen2.5:latest", "Compact and efficient, ideal for responsive tasks.", 0.0, 0.0));
MODELS.put("deepseek-coder-v2:latest", new GenAIModel(OLLAMA, "deepseek-coder-v2:latest", "Specialized for coding tasks, offering quick assistance.", 0.0, 0.0));
}

private final GenAIProvider provider;
private final String name;
private final String description;
private final double inputPrice, outputPrice;

public GenAIModel(GenAIProvider provider, String name, String description) {
public GenAIModel(GenAIProvider provider, String name, String description, double inputPrice, double outputPrice) {
this.provider = provider;
this.name = name;
this.description = description;
this.inputPrice = inputPrice;
this.outputPrice = outputPrice;
}

public GenAIProvider getProvider() {
Expand All @@ -74,13 +82,21 @@ public String getName() {
}

public static GenAIModel findByName(String name) {
return models.get(name);
return MODELS.get(name);
}

public String getDescription() {
return description;
}

public double getInputPrice() {
return inputPrice;
}

public double getOutputPrice() {
return outputPrice;
}

public String getFormattedInfo() {
return String.format("%s: %s", name, description);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
public enum GenAIProvider {
OPEN_AI,
GOOGLE,
OLLAMA;
OLLAMA,
ANTHROPIC;

}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public String getModelName() {
}
if (modelName == null || modelName.isEmpty()) {
// Fallback to default model name
modelName = getModel().getName();
modelName = getModel();
}
return modelName;
}
Expand Down Expand Up @@ -151,16 +151,8 @@ public void setClassContext(AIClassContext context) {
preferences.put("classContext", context != null ? context.name() : null);
}

public GenAIModel getModel() {
String gptModel = preferences.get(MODEL_PREFERENCE, null);
if (gptModel != null) {
try {
return GenAIModel.findByName(gptModel);
} catch (IllegalArgumentException iae) {
// .. skip
}
}
return GenAIModel.findByName(DEFAULT_MODEL);
public String getModel() {
return preferences.get(MODEL_PREFERENCE, DEFAULT_MODEL);
}

public void setModel(String model) {
Expand Down

0 comments on commit a02b93a

Please sign in to comment.