-
-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
215 additions
and
122 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/AzureServiceVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@file:Suppress("unused") | ||
|
||
package cn.yiiguxing.plugin.translate.trans.openai | ||
|
||
enum class AzureServiceVersion(val value: String) { | ||
V2023_05_15("2023-05-15"), | ||
V2023_12_01_PREVIEW("2023-12-01-preview"); | ||
} |
26 changes: 0 additions & 26 deletions
26
src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAI.kt
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAICredential.kt
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAICredentials.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.yiiguxing.plugin.translate.trans.openai | ||
|
||
import cn.yiiguxing.plugin.translate.TranslationPlugin | ||
import cn.yiiguxing.plugin.translate.util.credential.SimpleStringCredentialManager | ||
import cn.yiiguxing.plugin.translate.util.credential.StringCredentialManager | ||
import com.intellij.credentialStore.generateServiceName | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.components.service | ||
|
||
@Service | ||
internal class OpenAICredentials private constructor() { | ||
|
||
val openAi = SimpleStringCredentialManager(OPEN_AI_SERVICE_NAME) | ||
val azure = SimpleStringCredentialManager(AZURE_SERVICE_NAME) | ||
|
||
companion object { | ||
private val OPEN_AI_SERVICE_NAME = | ||
generateServiceName("OpenAI Credentials", "${TranslationPlugin.PLUGIN_ID}.OPENAI_API_KEY") | ||
private val AZURE_SERVICE_NAME = | ||
generateServiceName("OpenAI Credentials", "${TranslationPlugin.PLUGIN_ID}.AZURE_OPENAI_API_KEY") | ||
|
||
private val service: OpenAICredentials get() = service() | ||
|
||
fun manager(provider: ServiceProvider): StringCredentialManager = when (provider) { | ||
ServiceProvider.OpenAI -> service.openAi | ||
ServiceProvider.Azure -> service.azure | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAIService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cn.yiiguxing.plugin.translate.trans.openai | ||
|
||
import cn.yiiguxing.plugin.translate.trans.openai.chat.ChatCompletion | ||
import cn.yiiguxing.plugin.translate.trans.openai.chat.ChatMessage | ||
import cn.yiiguxing.plugin.translate.trans.openai.chat.chatCompletionRequest | ||
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread | ||
import com.intellij.util.io.RequestBuilder | ||
|
||
const val DEFAULT_OPEN_AI_API_ENDPOINT = "https://api.openai.com" | ||
const val OPEN_AI_API_PATH = "/v1/chat/completions" | ||
|
||
private const val AZURE_OPEN_AI_API_PATH = "/openai/deployments/%s/chat/completions" | ||
|
||
interface OpenAIService { | ||
|
||
@RequiresBackgroundThread | ||
fun chatCompletion(messages: List<ChatMessage>): ChatCompletion | ||
|
||
interface Options { | ||
val model: OpenAIModel | ||
val endpoint: String? | ||
} | ||
|
||
interface AzureOptions : Options { | ||
val apiVersion: AzureServiceVersion | ||
} | ||
|
||
companion object { | ||
fun get(settings: OpenAISettings): OpenAIService { | ||
return when (settings.provider) { | ||
ServiceProvider.OpenAI -> OpenAI(settings.openAi) | ||
ServiceProvider.Azure -> Azure(settings.azure) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
class OpenAI(private val options: OpenAIService.Options) : OpenAIService { | ||
private val apiUrl: String | ||
get() = (options.endpoint ?: DEFAULT_OPEN_AI_API_ENDPOINT).trimEnd('/') + OPEN_AI_API_PATH | ||
|
||
private fun RequestBuilder.auth() { | ||
val apiKey = OpenAICredentials.manager(ServiceProvider.OpenAI).credential | ||
tuner { it.setRequestProperty("Authorization", "Bearer $apiKey") } | ||
} | ||
|
||
override fun chatCompletion(messages: List<ChatMessage>): ChatCompletion { | ||
val request = chatCompletionRequest { | ||
model = options.model.value | ||
this.messages = messages | ||
} | ||
return OpenAIHttp.post<ChatCompletion>(apiUrl, request) { auth() } | ||
} | ||
} | ||
|
||
class Azure(options: OpenAIService.AzureOptions) : OpenAIService { | ||
|
||
private val apiUrl: String = requireNotNull(options.endpoint) { "Azure OpenAI API endpoint is required" } + | ||
AZURE_OPEN_AI_API_PATH.format(options.model.value) + | ||
"?api-version=${options.apiVersion.value}" | ||
|
||
private fun RequestBuilder.auth() { | ||
val apiKey = OpenAICredentials.manager(ServiceProvider.Azure).credential | ||
tuner { it.setRequestProperty("api-key", apiKey) } | ||
} | ||
|
||
override fun chatCompletion(messages: List<ChatMessage>): ChatCompletion { | ||
val request = chatCompletionRequest(false) { | ||
this.messages = messages | ||
} | ||
return OpenAIHttp.post<ChatCompletion>(apiUrl, request) { auth() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.