Skip to content

Commit

Permalink
fix: append slash to openAI custom host #98
Browse files Browse the repository at this point in the history
  • Loading branch information
iptton committed Mar 8, 2024
1 parent b28ad10 commit 510948e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Expand Up @@ -31,6 +31,7 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import java.net.URL
import java.time.Duration


Expand Down Expand Up @@ -192,13 +193,14 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
}

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)
}

/**
* 如果用户输入的 host 未带 "/" 结尾,自动补全
* 如果用户输入的 host 带有 query 或者 fragment,则不做干涉
*/
fun tryFixHostUrl(customOpenAiHost: String): String {
if (isUrlWithPath(customOpenAiHost)) {
val url = URL(customOpenAiHost)
if (url.query != null || url.toURI().fragment != null) {
return customOpenAiHost
}

Expand Down
Expand Up @@ -9,5 +9,11 @@ class AzureOpenAIProviderTest {
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/")

assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/v1"), "https://api.openai.com/v1/")

assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/v1?d=3"), "https://api.openai.com/v1?d=3")

assertEquals(AzureOpenAIProvider.tryFixHostUrl("https://api.openai.com/v1#2"), "https://api.openai.com/v1#2")
}
}
}

0 comments on commit 510948e

Please sign in to comment.