Skip to content

Commit

Permalink
open ai and anthropic model name fix
Browse files Browse the repository at this point in the history
  • Loading branch information
damiano1996 committed Dec 7, 2024
1 parent 24855ac commit bf99244
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.github.damiano1996.jetbrains.incoder.language.model.client;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.github.damiano1996.jetbrains.incoder.language.model.client.chat.ChatCodingAssistant;
import com.github.damiano1996.jetbrains.incoder.language.model.client.file.FileManagerAssistant;
import com.github.damiano1996.jetbrains.incoder.language.model.client.inline.InlineCodingAssistant;
import com.github.damiano1996.jetbrains.incoder.language.model.client.prompt.PromptClassifier;

public interface LanguageModelClient
extends ChatCodingAssistant,
InlineCodingAssistant,
FileManagerAssistant,
PromptClassifier {}
extends ChatCodingAssistant, InlineCodingAssistant, FileManagerAssistant, PromptClassifier {

/**
* Checks whether the connection is healthy.
*
* @throws LanguageModelException if the connection or settings are unhealthy.
*/
void checkServerConnection() throws LanguageModelException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.damiano1996.jetbrains.incoder.language.model.client;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.github.damiano1996.jetbrains.incoder.language.model.client.chat.ChatCodingAssistant;
import com.github.damiano1996.jetbrains.incoder.language.model.client.file.FileManagerAssistant;
import com.github.damiano1996.jetbrains.incoder.language.model.client.inline.InlineCodingAssistant;
Expand All @@ -15,6 +16,8 @@
@Slf4j
public class LanguageModelClientImpl implements LanguageModelClient {

private final ChatLanguageModel chatLanguageModel;

private final ChatCodingAssistant chatCodingAssistant;
private final InlineCodingAssistant inlineCodingAssistant;
private final FileManagerAssistant fileManagerAssistant;
Expand All @@ -25,6 +28,8 @@ public LanguageModelClientImpl(
StreamingChatLanguageModel streamingChatLanguageModel,
ChatMemory chatMemory) {

this.chatLanguageModel = chatLanguageModel;

chatCodingAssistant =
AiServices.builder(ChatCodingAssistant.class)
.streamingChatLanguageModel(streamingChatLanguageModel)
Expand Down Expand Up @@ -85,4 +90,13 @@ public String createFileName(String fileContent) {
log.debug("Defining file path");
return fileManagerAssistant.createFileName(fileContent).trim();
}

@Override
public void checkServerConnection() throws LanguageModelException {
try {
chatLanguageModel.generate("Hello!");
} catch (Exception e) {
throw new LanguageModelException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
package com.github.damiano1996.jetbrains.incoder.language.model.server;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.util.ThrowableComputable;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseServerConfigurable implements Configurable {

protected abstract ServerFactory getServerFactory();

@Override
public @NlsContexts.ConfigurableName String getDisplayName() {
return getServerFactory().getName();
}

@Override
public final void apply() throws ConfigurationException {
updateState();
verifySettings();
//noinspection DialogTitleCapitalization
ProgressManager.getInstance()
.runProcessWithProgressSynchronously(
(ThrowableComputable<Void, ConfigurationException>)
() -> {
updateState();
verifySettings();
return null;
},
"Verifying %s Settings".formatted(getDisplayName()),
false,
null);
}

/** Updates the server state based on the current configuration. */
Expand All @@ -20,5 +43,12 @@ public final void apply() throws ConfigurationException {
*
* @throws ConfigurationException if the settings are invalid.
*/
protected abstract void verifySettings() throws ConfigurationException;
private void verifySettings() throws ConfigurationException {
try {
log.debug("Creating server and client to verify configurations");
getServerFactory().createServer().createClient().checkServerConnection();
} catch (LanguageModelException e) {
throw new ConfigurationException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String getName() {

@Override
public List<String> getAvailableModels() {
return Arrays.stream(AnthropicChatModelName.values()).map(Enum::name).toList();
return Arrays.stream(AnthropicChatModelName.values()).map(Enum::toString).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.FormBuilder;
import java.awt.*;
import javax.swing.*;
import lombok.Getter;

@Getter
public class AnthropicComponent {

private final JPanel mainPanel;
private final JPasswordField apiKeyField = new JPasswordField();
private final JPasswordField apiKeyField;
private final ComboBox<String> modelNameField;
private final JSpinner temperatureField;

public AnthropicComponent() {
apiKeyField = new JPasswordField();
apiKeyField.setColumns(60);

SpinnerNumberModel temperatureModel = new SpinnerNumberModel(0.5, 0.0, 1.0, 0.1);
temperatureField = new JSpinner(temperatureModel);

Expand All @@ -24,6 +28,7 @@ public AnthropicComponent() {
new AnthropicLanguageModelServer()
.getAvailableModels()
.toArray(new String[0]));
modelNameField.setPreferredSize(new Dimension(300, 30));

mainPanel =
FormBuilder.createFormBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.damiano1996.jetbrains.incoder.language.model.server.anthropic.settings;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.BaseServerConfigurable;
import com.github.damiano1996.jetbrains.incoder.language.model.server.anthropic.AnthropicLanguageModelServer;
import com.intellij.openapi.options.ConfigurationException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.ServerFactory;
import com.github.damiano1996.jetbrains.incoder.language.model.server.anthropic.AnthropicFactory;
import javax.swing.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
Expand All @@ -18,6 +17,12 @@ private static AnthropicSettings.State getState() {
return AnthropicSettings.getInstance().getState();
}

@Contract(value = " -> new", pure = true)
@Override
protected @NotNull ServerFactory getServerFactory() {
return new AnthropicFactory();
}

@Contract(pure = true)
@Nls(capitalization = Nls.Capitalization.Title)
@Override
Expand Down Expand Up @@ -54,15 +59,6 @@ public void updateState() {
state.temperature = (Double) settingsComponent.getTemperatureField().getValue();
}

@Override
protected void verifySettings() throws ConfigurationException {
try {
new AnthropicLanguageModelServer().createClient();
} catch (LanguageModelException e) {
throw new ConfigurationException(e.getMessage());
}
}

@Override
public void reset() {
var state = getState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.damiano1996.jetbrains.incoder.language.model.server.ollama.settings;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.BaseServerConfigurable;
import com.github.damiano1996.jetbrains.incoder.language.model.server.ollama.OllamaLanguageModelServer;
import com.intellij.openapi.options.ConfigurationException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.ServerFactory;
import com.github.damiano1996.jetbrains.incoder.language.model.server.ollama.OllamaFactory;
import javax.swing.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
Expand All @@ -18,6 +17,12 @@ public final class OllamaConfigurable extends BaseServerConfigurable {
return OllamaSettings.getInstance().getState();
}

@Contract(value = " -> new", pure = true)
@Override
protected @NotNull ServerFactory getServerFactory() {
return new OllamaFactory();
}

@Contract(pure = true)
@Nls(capitalization = Nls.Capitalization.Title)
@Override
Expand Down Expand Up @@ -59,15 +64,6 @@ public void updateState() {
state.temperature = (Double) settingsComponent.getTemperatureField().getValue();
}

@Override
protected void verifySettings() throws ConfigurationException {
try {
new OllamaLanguageModelServer().createClient();
} catch (LanguageModelException e) {
throw new ConfigurationException(e.getMessage());
}
}

@Override
public void reset() {
var state = getState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.github.damiano1996.jetbrains.incoder.language.model.server.openai.settings.OpenAiSettings;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiLanguageModelName;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.model.openai.*;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -41,7 +39,7 @@ public String getName() {

@Override
public List<String> getAvailableModels() {
return Arrays.stream(OpenAiLanguageModelName.values()).map(Enum::name).toList();
return Arrays.stream(OpenAiChatModelName.values()).map(Enum::toString).toList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.FormBuilder;
import java.awt.*;
import javax.swing.*;
import lombok.Getter;

@Getter
public class OpenAiComponent {

private final JPanel mainPanel;
private final JPasswordField apiKeyField = new JPasswordField();
private final JPasswordField apiKeyField;
private final ComboBox<String> modelNameField;
private final JSpinner temperatureField;

public OpenAiComponent() {
apiKeyField = new JPasswordField();
apiKeyField.setColumns(60);

SpinnerNumberModel temperatureModel = new SpinnerNumberModel(0.5, 0.0, 1.0, 0.1);
temperatureField = new JSpinner(temperatureModel);

Expand All @@ -24,6 +28,7 @@ public OpenAiComponent() {
new OpenAiLanguageModelServer()
.getAvailableModels()
.toArray(new String[0]));
modelNameField.setPreferredSize(new Dimension(300, 30));

mainPanel =
FormBuilder.createFormBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.damiano1996.jetbrains.incoder.language.model.server.openai.settings;

import com.github.damiano1996.jetbrains.incoder.language.model.LanguageModelException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.BaseServerConfigurable;
import com.github.damiano1996.jetbrains.incoder.language.model.server.openai.OpenAiLanguageModelServer;
import com.intellij.openapi.options.ConfigurationException;
import com.github.damiano1996.jetbrains.incoder.language.model.server.ServerFactory;
import com.github.damiano1996.jetbrains.incoder.language.model.server.openai.OpenAiFactory;
import javax.swing.*;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
Expand All @@ -18,6 +17,12 @@ private static OpenAiSettings.State getState() {
return OpenAiSettings.getInstance().getState();
}

@Contract(value = " -> new", pure = true)
@Override
protected @NotNull ServerFactory getServerFactory() {
return new OpenAiFactory();
}

@Contract(pure = true)
@Nls(capitalization = Nls.Capitalization.Title)
@Override
Expand Down Expand Up @@ -54,15 +59,6 @@ public void updateState() {
state.temperature = (Double) settingsComponent.getTemperatureField().getValue();
}

@Override
protected void verifySettings() throws ConfigurationException {
try {
new OpenAiLanguageModelServer().createClient();
} catch (LanguageModelException e) {
throw new ConfigurationException(e.getMessage());
}
}

@Override
public void reset() {
var state = getState();
Expand Down

0 comments on commit bf99244

Please sign in to comment.