Skip to content

Commit 20dda56

Browse files
committed
fix(custom-sse-processor): handle non-standard response format and log parsing errors
The `CustomSSEProcessor` now accounts for responses that do not conform to the expected format, avoiding crashes. It also logs parsing errors to provide better debugging capabilities.
1 parent 92eb05e commit 20dda56

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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
4444
open val requestFormat: String = ""
4545
open val responseFormat: String = ""
4646
private val logger = logger<CustomSSEProcessor>()
@@ -87,6 +87,7 @@ open class CustomSSEProcessor(private val project: Project) {
8787
if (responseFormat.isNotEmpty()) {
8888
// {"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}}]}
8989
// in some case, the response maybe not equal to our response format, so we need to ignore it
90+
// {"id":"cmpl-ac26a17e","object":"chat.completion.chunk","created":1858403,"model":"yi-34b-chat","choices":[{"delta":{"role":"assistant"},"index":0}],"content":"","lastOne":false}
9091
val chunk: String? = JsonPath.parse(sse!!.data)?.read(responseFormat)
9192

9293
// new JsonPath lib caught the exception, so we need to handle when it is null
@@ -100,10 +101,9 @@ open class CustomSSEProcessor(private val project: Project) {
100101
**$responseFormat**\n""".trimIndent()
101102

102103
// TODO add refresh feature
103-
trySend(errorMsg)
104-
close()
104+
logger.warn(errorMsg)
105105
} else {
106-
logger.info("Failed to parse response.origin response is: ${sse.data}")
106+
logger.warn("Failed to parse response.origin response is: ${sse.data}")
107107
}
108108
return@blockingForEach
109109
}

0 commit comments

Comments
 (0)