Skip to content

Commit

Permalink
fix(custom-sse-processor): handle non-standard response format and lo…
Browse files Browse the repository at this point in the history
…g 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.
  • Loading branch information
phodal committed Mar 29, 2024
1 parent 92eb05e commit 20dda56
Showing 1 changed file with 4 additions and 4 deletions.
Expand Up @@ -40,7 +40,7 @@ import org.jetbrains.annotations.VisibleForTesting
* @constructor Creates an instance of `CustomSSEProcessor`.
*/
open class CustomSSEProcessor(private val project: Project) {
open var hasSuccessRequest: Boolean = true
open var hasSuccessRequest: Boolean = false
open val requestFormat: String = ""
open val responseFormat: String = ""
private val logger = logger<CustomSSEProcessor>()
Expand Down Expand Up @@ -87,6 +87,7 @@ open class CustomSSEProcessor(private val project: Project) {
if (responseFormat.isNotEmpty()) {
// {"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}}]}
// in some case, the response maybe not equal to our response format, so we need to ignore it
// {"id":"cmpl-ac26a17e","object":"chat.completion.chunk","created":1858403,"model":"yi-34b-chat","choices":[{"delta":{"role":"assistant"},"index":0}],"content":"","lastOne":false}
val chunk: String? = JsonPath.parse(sse!!.data)?.read(responseFormat)

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

// TODO add refresh feature
trySend(errorMsg)
close()
logger.warn(errorMsg)
} else {
logger.info("Failed to parse response.origin response is: ${sse.data}")
logger.warn("Failed to parse response.origin response is: ${sse.data}")
}
return@blockingForEach
}
Expand Down

0 comments on commit 20dda56

Please sign in to comment.