Skip to content

Commit

Permalink
feat: init check for openai hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Mar 1, 2024
1 parent f62be13 commit efeb143
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Expand Up @@ -53,12 +53,9 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
private val autoDevSettingsState = AutoDevSettingsState.getInstance()
private val url: String
get() {
val customOpenAiHost = autoDevSettingsState.customOpenAiHost
if (!customOpenAiHost.endsWith("/")) {
return "$customOpenAiHost/"
}
return customOpenAiHost
return tryFixHostUrl(autoDevSettingsState.customOpenAiHost)
}

private var customPromptConfig: CustomPromptConfig? = null
private val timeout = Duration.ofSeconds(600)
private var client = OkHttpClient().newBuilder().readTimeout(timeout).build()
Expand Down Expand Up @@ -193,4 +190,23 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
close()
}
}

companion object {
fun isUrlWithPath(input: String): Boolean {
val urlPattern = Regex("^https?://[a-zA-Z0-9-]+(\\\\.[a-zA-Z]{2,})+(/[a-zA-Z0-9-._~:/?#[\\\\]@!\$&'()*+,;=%]*)?\\\$")
return urlPattern.matches(input)
}

fun tryFixHostUrl(customOpenAiHost: String): String {
if (isUrlWithPath(customOpenAiHost)) {
return customOpenAiHost
}

if (!customOpenAiHost.endsWith("/")) {
return "$customOpenAiHost/"
}

return customOpenAiHost
}
}
}
@@ -0,0 +1,13 @@
package cc.unitmesh.devti.llms.azure

import junit.framework.TestCase.assertEquals
import org.junit.Test

class AzureOpenAIProviderTest {
@Test
fun should_check_host_is_endpoint() {
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com"), "https://api.openai.com/")
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/"), "https://api.openai.com/")
assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/v1"), "https://api.openai.com/v1/")
}
}

0 comments on commit efeb143

Please sign in to comment.