Skip to content

Commit 41f2c72

Browse files
author
ippan
committed
fix(llm): handle null response in CustomSSEProcessor
- Remove unnecessary 'hasSuccessRequest' toggle and related code. - Introduce 'lastParseFailedResponse' to store the failed response. - Log a detailed error message when a null response is encountered. - Add a check at the end of the stream to notify the user if no successful parse occurred.
1 parent 9203ee1 commit 41f2c72

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

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

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import org.jetbrains.annotations.VisibleForTesting
4040
* @constructor Creates an instance of `CustomSSEProcessor`.
4141
*/
4242
open class CustomSSEProcessor(private val project: Project) {
43-
open var hasSuccessRequest: Boolean = true
43+
open var hasSuccessRequest: Boolean = false
44+
private var lastParseFailedResponse: String = ""
4445
open val requestFormat: String = ""
4546
open val responseFormat: String = ""
4647
private val logger = logger<CustomSSEProcessor>()
@@ -91,27 +92,13 @@ open class CustomSSEProcessor(private val project: Project) {
9192

9293
// new JsonPath lib caught the exception, so we need to handle when it is null
9394
if (chunk == null) {
94-
// if first chunk parse failed, notice user to check response format
95-
if(hasSuccessRequest) {
96-
val errorMsg = """
97-
**Failed** to parse response.origin response is:
98-
<code>${sse.data}</code>
99-
please check your response format:
100-
**$responseFormat**\n""".trimIndent()
101-
102-
// TODO add refresh feature
103-
trySend(errorMsg)
104-
close()
105-
} else {
106-
logger.info("Failed to parse response.origin response is: ${sse.data}")
107-
}
108-
return@blockingForEach
95+
lastParseFailedResponse = sse.data
96+
logger.info("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
97+
} else {
98+
hasSuccessRequest = true
99+
output += chunk
100+
trySend(chunk)
109101
}
110-
111-
hasSuccessRequest = true
112-
113-
output += chunk
114-
trySend(chunk)
115102
} else {
116103
val result: ChatCompletionResult =
117104
ObjectMapper().readValue(sse!!.data, ChatCompletionResult::class.java)
@@ -124,6 +111,18 @@ open class CustomSSEProcessor(private val project: Project) {
124111
}
125112
}
126113

114+
// when stream finished, check if any response parsed succeeded
115+
// if not, notice user check response format
116+
if (!hasSuccessRequest) {
117+
val errorMsg = """
118+
**Failed** to parse response.origin response is:
119+
<code>${lastParseFailedResponse}</code>
120+
please check your response format:
121+
**$responseFormat**\n""".trimIndent()
122+
123+
// TODO add refresh feature
124+
trySend(errorMsg)
125+
}
127126
recording.write(RecordingInstruction(promptText, output))
128127
close()
129128
}

0 commit comments

Comments
 (0)