Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Watsonx #679

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/ee/carlrobert/codegpt/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class Icons {
public static final Icon You = IconLoader.getIcon("/icons/you.svg", Icons.class);
public static final Icon YouSmall = IconLoader.getIcon("/icons/you_small.png", Icons.class);
public static final Icon Ollama = IconLoader.getIcon("/icons/ollama.svg", Icons.class);
public static final Icon Watsonx = IconLoader.getIcon("/icons/watsonx.svg", Icons.class);
public static final Icon User = IconLoader.getIcon("/icons/user.svg", Icons.class);
public static final Icon Upload = IconLoader.getIcon("/icons/upload.svg", Icons.class);
public static final Icon GreenCheckmark =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.ollama.OllamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.codegpt.settings.service.watsonx.WatsonxSettings;
import ee.carlrobert.llm.client.anthropic.ClaudeClient;
import ee.carlrobert.llm.client.azure.AzureClient;
import ee.carlrobert.llm.client.azure.AzureCompletionRequestParams;
Expand All @@ -18,6 +19,7 @@
import ee.carlrobert.llm.client.llama.LlamaClient;
import ee.carlrobert.llm.client.ollama.OllamaClient;
import ee.carlrobert.llm.client.openai.OpenAIClient;
import ee.carlrobert.llm.client.watsonx.WatsonxClient;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;
Expand All @@ -32,6 +34,24 @@ public static CodeGPTClient getCodeGPTClient() {
getDefaultClientBuilder());
}

public static WatsonxClient getWatsonxClient() {
String regionCode = switch(WatsonxSettings.getCurrentState().getRegion()) {
case "Dallas" -> "us-south";
case "Frankfurt" -> "eu-de";
case "London" -> "eu-gb";
case "Tokyo" -> "jp-tok";
default -> "us-south";
};
String host = WatsonxSettings.getCurrentState().isOnPrem() ? WatsonxSettings.getCurrentState().getOnPremHost() : "https://" + regionCode + ".ml.cloud.ibm.com";
return new WatsonxClient.Builder(getCredential(CredentialKey.WATSONX_API_KEY))
.setApiVersion(WatsonxSettings.getCurrentState().getApiVersion())
.setIsOnPrem(WatsonxSettings.getCurrentState().isOnPrem())
.setHost(host)
.setUsername(WatsonxSettings.getCurrentState().getUsername())
.setIsZenApiKey(WatsonxSettings.getCurrentState().isZenApiKey())
.build(getDefaultClientBuilder());
}

public static OpenAIClient getOpenAIClient() {
return new OpenAIClient.Builder(getCredential(CredentialKey.OPENAI_API_KEY))
.setOrganization(OpenAISettings.getCurrentState().getOrganization())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.ollama.OllamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.codegpt.settings.service.watsonx.WatsonxSettings;
import ee.carlrobert.codegpt.util.file.FileUtil;
import ee.carlrobert.llm.client.anthropic.completion.ClaudeBase64Source;
import ee.carlrobert.llm.client.anthropic.completion.ClaudeCompletionDetailedMessage;
Expand Down Expand Up @@ -55,6 +56,7 @@
import ee.carlrobert.llm.client.openai.completion.request.OpenAIMessageImageURLContent;
import ee.carlrobert.llm.client.openai.completion.request.OpenAIMessageTextContent;
import ee.carlrobert.llm.client.openai.completion.request.RequestDocumentationDetails;
import ee.carlrobert.llm.client.watsonx.completion.WatsonxCompletionRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -307,6 +309,33 @@ private static Request buildCustomOpenAIChatCompletionRequest(
}
}

public WatsonxCompletionRequest buildWatsonxChatCompletionRequest(
CallParameters callParameters) {
var settings = WatsonxSettings.getCurrentState();
String prompt = PersonaSettings.getSystemPrompt();
prompt += "\n"+callParameters.getMessage().getPrompt();
var builder = new WatsonxCompletionRequest.Builder(prompt);
builder.setDecodingMethod(settings.isGreedyDecoding() ? "greedy" : "sample");
if (settings.getDeploymentId() != null && !settings.getDeploymentId().isEmpty()) {
builder.setDeploymentId(settings.getDeploymentId());
} else {
builder.setModelId(settings.getModel());
builder.setProjectId(settings.getProjectId());
builder.setSpaceId(settings.getSpaceId());
}
builder.setMaxNewTokens(settings.getMaxNewTokens());
builder.setMinNewTokens(settings.getMinNewTokens());
builder.setTemperature(settings.getTemperature());
builder.setStopSequences(settings.getStopSequences().isEmpty() ? null : settings.getStopSequences().split(","));
builder.setTopP(settings.getTopP());
builder.setTopK(settings.getTopK());
builder.setIncludeStopSequence(settings.getIncludeStopSequence());
builder.setRandomSeed(settings.getRandomSeed());
builder.setRepetitionPenalty(settings.getRepetitionPenalty());
builder.setStream(true);
return builder.build();
}

public ClaudeCompletionRequest buildAnthropicChatCompletionRequest(
CallParameters callParameters) {
var configuration = ConfigurationSettings.getState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public EventSource getChatCompletionAsync(
settings.getModel(),
eventListener);
}
case WATSONX -> CompletionClientProvider.getWatsonxClient().getCompletionAsync(
requestProvider.buildWatsonxChatCompletionRequest(callParameters),
eventListener);
};
}

Expand Down Expand Up @@ -285,7 +288,7 @@ public static boolean isRequestAllowed(ServiceType serviceType) {
AzureSettings.getCurrentState().isUseAzureApiKeyAuthentication()
? CredentialKey.AZURE_OPENAI_API_KEY
: CredentialKey.AZURE_ACTIVE_DIRECTORY_TOKEN);
case CODEGPT, CUSTOM_OPENAI, ANTHROPIC, LLAMA_CPP, OLLAMA -> true;
case CODEGPT, CUSTOM_OPENAI, ANTHROPIC, LLAMA_CPP, OLLAMA, WATSONX -> true;
case GOOGLE -> CredentialsStore.INSTANCE.isCredentialSet(CredentialKey.GOOGLE_API_KEY);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.ollama.OllamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.codegpt.settings.service.watsonx.WatsonxSettings;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -210,6 +211,9 @@ private static String getModelForSelectedService(ServiceType serviceType) {
case GOOGLE -> application.getService(GoogleSettings.class)
.getState()
.getModel();
case WATSONX -> application.getService(WatsonxSettings.class)
.getState()
.getModel();
};
}
}
10 changes: 10 additions & 0 deletions src/main/java/ee/carlrobert/codegpt/settings/GeneralSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings;
import ee.carlrobert.codegpt.settings.service.ollama.OllamaSettings;
import ee.carlrobert.codegpt.settings.service.openai.OpenAISettings;
import ee.carlrobert.codegpt.settings.service.watsonx.WatsonxSettings;
import ee.carlrobert.codegpt.util.ApplicationUtil;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -92,6 +93,10 @@ public void sync(Conversation conversation) {
ApplicationManager.getApplication().getService(OllamaSettings.class).getState()
.setModel(conversation.getModel());
break;
case WATSONX:
ApplicationManager.getApplication().getService(WatsonxSettings.class).getState()
.setModel(conversation.getModel());
break;
default:
break;
}
Expand Down Expand Up @@ -144,6 +149,11 @@ public String getModel() {
.getService(GoogleSettings.class)
.getState()
.getModel();
case WATSONX:
return ApplicationManager.getApplication()
.getService(WatsonxSettings.class)
.getState()
.getModel();
default:
return "Unknown";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public enum ServiceType {
AZURE("AZURE", "service.azure.title", "azure.chat.completion"),
GOOGLE("GOOGLE", "service.google.title", "google.chat.completion"),
LLAMA_CPP("LLAMA_CPP", "service.llama.title", "llama.chat.completion"),
OLLAMA("OLLAMA", "service.ollama.title", "ollama.chat.completion");
OLLAMA("OLLAMA", "service.ollama.title", "ollama.chat.completion"),
WATSONX("WATSONX", "service.watsonx.title", "watsonx.chat.completion");


private final String code;
private final String label;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ee.carlrobert.codegpt.settings.service.watsonx;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NotNull;

@State(name = "CodeGPT_WatsonxSettings", storages = @Storage("CodeGPT_WatsonxSettings.xml"))
public class WatsonxSettings implements PersistentStateComponent<WatsonxSettingsState> {

private WatsonxSettingsState state = new WatsonxSettingsState();

public static WatsonxSettingsState getCurrentState() {
return getInstance().getState();
}

public static boolean isCodeCompletionsPossible() {
return getInstance().getState().isCodeCompletionsEnabled();
}

public static ee.carlrobert.codegpt.settings.service.watsonx.WatsonxSettings getInstance() {
return ApplicationManager.getApplication().getService(WatsonxSettings.class);
}

@Override
@NotNull
public WatsonxSettingsState getState() {
return state;
}

@Override
public void loadState(@NotNull WatsonxSettingsState state) {
this.state = state;
}
}
Loading