Skip to content

Commit 2b6a4fe

Browse files
committed
refactor: Add default timeout to LLMProvider and update implementations
- Introduce a default timeout property in the LLMProvider interface to centralize timeout configurations. - Update AzureOpenAIProvider, OpenAIProvider, and CustomLLMProvider to use the new default timeout. - Correct typos in XingHuoProvider (secrectKey to secretKey) and update hmacsha256 key specification to use the corrected secretKey.
1 parent 4652954 commit 2b6a4fe

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

src/main/kotlin/cc/unitmesh/devti/llms/LLMProvider.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import kotlinx.coroutines.flow.Flow
77
import kotlinx.coroutines.flow.callbackFlow
88

99
interface LLMProvider {
10+
val defaultTimeout: Long get() = 600
11+
1012
fun prompt(promptText: String): String
1113

1214
@OptIn(ExperimentalCoroutinesApi::class)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
5757
}
5858

5959
private var customPromptConfig: CustomPromptConfig? = null
60-
private val timeout = Duration.ofSeconds(600)
60+
private val timeout = Duration.ofSeconds(defaultTimeout)
6161
private var client = OkHttpClient().newBuilder().readTimeout(timeout).build()
6262
private val openAiVersion: String
6363
private val maxTokenLength: Int

src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomLLMProvider.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
5252
get() = autoDevSettingsState.customEngineResponseFormat
5353

5454
private var client = OkHttpClient()
55-
private val timeout = Duration.ofSeconds(600)
55+
private val timeout = Duration.ofSeconds(defaultTimeout)
5656
private val messages: MutableList<Message> = mutableListOf()
5757
private val logger = logger<CustomLLMProvider>()
5858

@@ -125,8 +125,20 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
125125
if (responseFormat.isNotEmpty()) {
126126
// {"id":"cmpl-a22a0d78fcf845be98660628fe5d995b","object":"chat.completion.chunk","created":822330,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{},"finish_reason":"stop","usage":{"prompt_tokens":434,"completion_tokens":68,"total_tokens":502}}]}
127127
// in some case, the response maybe not equal to our response format, so we need to ignore it
128-
val chunk: String = JsonPath.parse(sse!!.data)?.read(responseFormat)
129-
?: throw Exception("Failed to parse chunk: ${sse.data}")
128+
val chunk: String = try {
129+
JsonPath.parse(sse!!.data)?.read(responseFormat) ?: ""
130+
} catch (e: Exception) {
131+
if (hasSuccessRequest) {
132+
logger.info("Failed to parse response", e)
133+
} else {
134+
logger.error("Failed to parse response", e)
135+
}
136+
return@blockingForEach
137+
}
138+
139+
if (chunk.isEmpty()) {
140+
return@blockingForEach
141+
}
130142

131143
hasSuccessRequest = true
132144
trySend(chunk)

src/main/kotlin/cc/unitmesh/devti/llms/openai/OpenAIProvider.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@ import java.time.Duration
3232

3333
@Service(Service.Level.PROJECT)
3434
class OpenAIProvider(val project: Project) : LLMProvider {
35+
private val timeout = Duration.ofSeconds(defaultTimeout)
36+
private val openAiVersion: String
37+
get() {
38+
val customModel = AutoDevSettingsState.getInstance().customModel
39+
if(AutoDevSettingsState.getInstance().openAiModel == SELECT_CUSTOM_MODEL) {
40+
AutoDevSettingsState.getInstance().openAiModel = customModel
41+
}
42+
return AutoDevSettingsState.getInstance().openAiModel
43+
}
44+
private val openAiKey: String
45+
get() = AutoDevSettingsState.getInstance().openAiKey
46+
47+
private val maxTokenLength: Int
48+
get() = AutoDevSettingsState.getInstance().fetchMaxTokenLength()
49+
50+
private val messages: MutableList<ChatMessage> = ArrayList()
51+
private var historyMessageLength: Int = 0
52+
3553
private val service: OpenAiService
3654
get() {
3755
if (openAiKey.isEmpty()) {
@@ -62,24 +80,6 @@ class OpenAIProvider(val project: Project) : LLMProvider {
6280
}
6381
}
6482

65-
private val timeout = Duration.ofSeconds(600)
66-
private val openAiVersion: String
67-
get() {
68-
val customModel = AutoDevSettingsState.getInstance().customModel
69-
if(AutoDevSettingsState.getInstance().openAiModel == SELECT_CUSTOM_MODEL) {
70-
AutoDevSettingsState.getInstance().openAiModel = customModel
71-
}
72-
return AutoDevSettingsState.getInstance().openAiModel
73-
}
74-
private val openAiKey: String
75-
get() = AutoDevSettingsState.getInstance().openAiKey
76-
77-
private val maxTokenLength: Int
78-
get() = AutoDevSettingsState.getInstance().fetchMaxTokenLength()
79-
80-
private val messages: MutableList<ChatMessage> = ArrayList()
81-
private var historyMessageLength: Int = 0
82-
8383
private val recording: Recording
8484
get() {
8585
if (project.coderSetting.state.recordingInLocal) {

src/main/kotlin/cc/unitmesh/devti/llms/xianghuo/XingHuoProvider.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import javax.crypto.spec.SecretKeySpec
2424
@Service(Service.Level.PROJECT)
2525
class XingHuoProvider(val project: Project) : LLMProvider {
2626
private val autoDevSettingsState = AutoDevSettingsState.getInstance()
27-
private val secrectKey: String
27+
private val secretKey: String
2828
get() = autoDevSettingsState.xingHuoApiSecrect
2929

3030
private val apiVersion: XingHuoApiVersion
@@ -49,7 +49,7 @@ class XingHuoProvider(val project: Project) : LLMProvider {
4949
private val hmacsha256: Mac
5050
get() {
5151
val hmac = Mac.getInstance(hmacsha256Algorithms)
52-
val keySpec = SecretKeySpec(secrectKey.toByteArray(), hmacsha256Algorithms)
52+
val keySpec = SecretKeySpec(secretKey.toByteArray(), hmacsha256Algorithms)
5353
hmac.init(keySpec)
5454
return hmac
5555
}

0 commit comments

Comments
 (0)