Skip to content

Commit efeb143

Browse files
committed
feat: init check for openai hosts
1 parent f62be13 commit efeb143

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/azure/AzureOpenAIProvider.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
5353
private val autoDevSettingsState = AutoDevSettingsState.getInstance()
5454
private val url: String
5555
get() {
56-
val customOpenAiHost = autoDevSettingsState.customOpenAiHost
57-
if (!customOpenAiHost.endsWith("/")) {
58-
return "$customOpenAiHost/"
59-
}
60-
return customOpenAiHost
56+
return tryFixHostUrl(autoDevSettingsState.customOpenAiHost)
6157
}
58+
6259
private var customPromptConfig: CustomPromptConfig? = null
6360
private val timeout = Duration.ofSeconds(600)
6461
private var client = OkHttpClient().newBuilder().readTimeout(timeout).build()
@@ -193,4 +190,23 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
193190
close()
194191
}
195192
}
193+
194+
companion object {
195+
fun isUrlWithPath(input: String): Boolean {
196+
val urlPattern = Regex("^https?://[a-zA-Z0-9-]+(\\\\.[a-zA-Z]{2,})+(/[a-zA-Z0-9-._~:/?#[\\\\]@!\$&'()*+,;=%]*)?\\\$")
197+
return urlPattern.matches(input)
198+
}
199+
200+
fun tryFixHostUrl(customOpenAiHost: String): String {
201+
if (isUrlWithPath(customOpenAiHost)) {
202+
return customOpenAiHost
203+
}
204+
205+
if (!customOpenAiHost.endsWith("/")) {
206+
return "$customOpenAiHost/"
207+
}
208+
209+
return customOpenAiHost
210+
}
211+
}
196212
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.unitmesh.devti.llms.azure
2+
3+
import junit.framework.TestCase.assertEquals
4+
import org.junit.Test
5+
6+
class AzureOpenAIProviderTest {
7+
@Test
8+
fun should_check_host_is_endpoint() {
9+
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com"), "https://api.openai.com/")
10+
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/"), "https://api.openai.com/")
11+
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/v1"), "https://api.openai.com/v1/")
12+
}
13+
}

0 commit comments

Comments
 (0)