Skip to content

Commit ffb07be

Browse files
committed
feat(custom-llm): Add response format validation and success request tracking
- Introduce a new field `hasSuccessRequest` to track if a successful request was made. - Add a check to ensure the response format matches the expected pattern before processing. - Update logging to use info level when a successful request has been made, and error level otherwise.
1 parent d434d8f commit ffb07be

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ data class CustomRequest(val messages: List<Message>)
4040

4141
@Service(Service.Level.PROJECT)
4242
class CustomLLMProvider(val project: Project) : LLMProvider {
43+
private var hasSuccessRequest: Boolean = true
4344
private val autoDevSettingsState = AutoDevSettingsState.getInstance()
4445
private val url
4546
get() = autoDevSettingsState.customEngineServer
@@ -126,8 +127,12 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
126127
}
127128
.blockingForEach { sse ->
128129
if (responseFormat.isNotEmpty()) {
130+
// {"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}}]}
131+
// in some case, the response maybe not equal to our response format, so we need to ignore it
129132
val chunk: String = JsonPath.parse(sse!!.data)?.read(responseFormat)
130133
?: throw Exception("Failed to parse chunk: ${sse.data}")
134+
135+
hasSuccessRequest = true
131136
trySend(chunk)
132137
} else {
133138
val result: ChatCompletionResult =
@@ -145,7 +150,12 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
145150
awaitClose()
146151
}
147152
} catch (e: Exception) {
148-
logger.error("Failed to stream", e)
153+
if (hasSuccessRequest) {
154+
logger.info("Failed to stream", e)
155+
} else {
156+
logger.error("Failed to stream", e)
157+
}
158+
149159
return callbackFlow {
150160
close()
151161
}

0 commit comments

Comments
 (0)