fix: CustomService Test connection with correct settings (#531)

This commit is contained in:
Phil 2024-05-07 17:34:35 +02:00 committed by GitHub
parent 13c59cc97b
commit 7f7b35d3be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 67 additions and 17 deletions

View file

@ -125,13 +125,17 @@ public class CompletionRequestProvider {
true);
}
public static Request buildCustomOpenAICompletionRequest(String input) {
public static Request buildCustomOpenAICompletionRequest(String context, String url,
Map<String, String> headers, Map<String, Object> body, String credential) {
var usedSettings = new CustomServiceChatCompletionSettingsState();
usedSettings.setBody(body);
usedSettings.setHeaders(headers);
usedSettings.setUrl(url);
return buildCustomOpenAIChatCompletionRequest(
ApplicationManager.getApplication().getService(CustomServiceSettings.class)
.getState()
.getChatCompletionSettings(),
List.of(new OpenAIChatCompletionStandardMessage("user", input)),
true);
usedSettings,
List.of(new OpenAIChatCompletionStandardMessage("user", context)),
true,
credential);
}
public static Request buildCustomOpenAILookupCompletionRequest(String context) {
@ -226,8 +230,16 @@ public class CompletionRequestProvider {
CustomServiceChatCompletionSettingsState settings,
List<OpenAIChatCompletionMessage> messages,
boolean streamRequest) {
return buildCustomOpenAIChatCompletionRequest(settings, messages, streamRequest,
CredentialsStore.INSTANCE.getCredential(CUSTOM_SERVICE_API_KEY));
}
private static Request buildCustomOpenAIChatCompletionRequest(
CustomServiceChatCompletionSettingsState settings,
List<OpenAIChatCompletionMessage> messages,
boolean streamRequest,
String credential) {
var requestBuilder = new Request.Builder().url(requireNonNull(settings.getUrl()).trim());
var credential = CredentialsStore.INSTANCE.getCredential(CUSTOM_SERVICE_API_KEY);
for (var entry : settings.getHeaders().entrySet()) {
String value = entry.getValue();
if (credential != null && value.contains("$CUSTOM_SERVICE_API_KEY")) {

View file

@ -34,17 +34,36 @@ object CodeCompletionRequestFactory {
@JvmStatic
fun buildCustomRequest(details: InfillRequestDetails): Request {
val settings = service<CustomServiceSettings>().state.codeCompletionSettings
val requestBuilder = Request.Builder().url(settings.url!!)
val credential = getCredential(CredentialKey.CUSTOM_SERVICE_API_KEY)
for (entry in settings.headers.entries) {
return buildCustomRequest(
details,
settings.url!!,
settings.headers,
settings.body,
settings.infillTemplate,
credential
)
}
@JvmStatic
fun buildCustomRequest(
details: InfillRequestDetails,
url: String,
headers: Map<String, String>,
body: Map<String, Any>,
infillTemplate: InfillPromptTemplate,
credential: String?
): Request {
val requestBuilder = Request.Builder().url(url)
for (entry in headers.entries) {
var value = entry.value
if (credential != null && value.contains("\$CUSTOM_SERVICE_API_KEY")) {
value = value.replace("\$CUSTOM_SERVICE_API_KEY", credential)
}
requestBuilder.addHeader(entry.key, value)
}
val transformedBody = settings.body.entries.associate { (key, value) ->
key to transformValue(value, settings.infillTemplate, details)
val transformedBody = body.entries.associate { (key, value) ->
key to transformValue(value, infillTemplate, details)
}
try {

View file

@ -15,7 +15,10 @@ import javax.swing.JButton
import javax.swing.JPanel
import javax.swing.SwingUtilities
class CustomServiceChatCompletionForm(state: CustomServiceChatCompletionSettingsState) {
class CustomServiceChatCompletionForm(
state: CustomServiceChatCompletionSettingsState,
val getApiKey: () -> String?
) {
private val urlField = JBTextField(state.url, 30)
private val tabbedPane = CustomServiceFormTabbedPane(state.headers, state.body)
@ -67,7 +70,13 @@ class CustomServiceChatCompletionForm(state: CustomServiceChatCompletionSettings
private fun testConnection() {
CompletionRequestService.getInstance().getCustomOpenAIChatCompletionAsync(
CompletionRequestProvider.buildCustomOpenAICompletionRequest("Hello!"),
CompletionRequestProvider.buildCustomOpenAICompletionRequest(
"Test",
urlField.text,
tabbedPane.headers,
tabbedPane.body,
getApiKey.invoke()
),
TestConnectionEventListener()
)
}

View file

@ -28,7 +28,10 @@ import javax.swing.JButton
import javax.swing.JPanel
import javax.swing.SwingUtilities
class CustomServiceCodeCompletionForm(state: CustomServiceCodeCompletionSettingsState) {
class CustomServiceCodeCompletionForm(
state: CustomServiceCodeCompletionSettingsState,
val getApiKey: () -> String?
) {
private val featureEnabledCheckBox = JBCheckBox(
CodeGPTBundle.get("codeCompletionsForm.enableFeatureText"),
@ -138,7 +141,14 @@ class CustomServiceCodeCompletionForm(state: CustomServiceCodeCompletionSettings
private fun testConnection() {
CompletionRequestService.getInstance().getCustomOpenAICompletionAsync(
CodeCompletionRequestFactory.buildCustomRequest(InfillRequestDetails("Hello", "!")),
CodeCompletionRequestFactory.buildCustomRequest(
InfillRequestDetails("Hello", "!"),
urlField.text,
tabbedPane.headers,
tabbedPane.body,
promptTemplateComboBox.selectedItem as InfillPromptTemplate,
getApiKey.invoke()
),
TestConnectionEventListener()
)
}

View file

@ -34,8 +34,8 @@ class CustomServiceForm {
init {
val state = service<CustomServiceSettings>().state
chatCompletionsForm = CustomServiceChatCompletionForm(state.chatCompletionSettings)
codeCompletionsForm = CustomServiceCodeCompletionForm(state.codeCompletionSettings)
chatCompletionsForm = CustomServiceChatCompletionForm(state.chatCompletionSettings, this::getApiKey)
codeCompletionsForm = CustomServiceCodeCompletionForm(state.codeCompletionSettings, this::getApiKey)
tabbedPane = JTabbedPane().apply {
add(CodeGPTBundle.get("shared.chatCompletions"), chatCompletionsForm.form)
add(CodeGPTBundle.get("shared.codeCompletions"), codeCompletionsForm.form)