Skip to content

Commit

Permalink
fix(llm): handle null response in CustomSSEProcessor
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
ippan committed Mar 29, 2024
1 parent 41f2c72 commit 5de1db3
Showing 1 changed file with 10 additions and 7 deletions.
Expand Up @@ -41,7 +41,7 @@ import org.jetbrains.annotations.VisibleForTesting
*/
open class CustomSSEProcessor(private val project: Project) {
open var hasSuccessRequest: Boolean = false
private var lastParseFailedResponse: String = ""
private var parseFailedResponses: MutableList<String> = mutableListOf()
open val requestFormat: String = ""
open val responseFormat: String = ""
private val logger = logger<CustomSSEProcessor>()
Expand Down Expand Up @@ -92,7 +92,7 @@ open class CustomSSEProcessor(private val project: Project) {

// new JsonPath lib caught the exception, so we need to handle when it is null
if (chunk == null) {
lastParseFailedResponse = sse.data
parseFailedResponses.add(sse.data)
logger.info("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
} else {
hasSuccessRequest = true
Expand All @@ -115,13 +115,14 @@ open class CustomSSEProcessor(private val project: Project) {
// if not, notice user check response format
if (!hasSuccessRequest) {
val errorMsg = """
**Failed** to parse response.origin response is:
<code>${lastParseFailedResponse}</code>
please check your response format:
**$responseFormat**\n""".trimIndent()
|**Failed** to parse response.please check your response format:
|**$responseFormat** origin responses is:
|- ${parseFailedResponses.joinToString("\n- ")}
|""".trimMargin()

// TODO add refresh feature
trySend(errorMsg)
// don't use trySend, it may be ignored by 'close()` op
send(errorMsg)
}
recording.write(RecordingInstruction(promptText, output))
close()
Expand All @@ -138,6 +139,8 @@ open class CustomSSEProcessor(private val project: Project) {
return callbackFlow {
close()
}
} finally {
parseFailedResponses.clear()
}
}
}
Expand Down

0 comments on commit 5de1db3

Please sign in to comment.