Skip to content

process wrong response format #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomSSEProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.jetbrains.annotations.VisibleForTesting
*/
open class CustomSSEProcessor(private val project: Project) {
open var hasSuccessRequest: Boolean = false
private var parseFailedResponses: MutableList<String> = mutableListOf()
open val requestFormat: String = ""
open val responseFormat: String = ""
private val logger = logger<CustomSSEProcessor>()
Expand Down Expand Up @@ -92,26 +93,13 @@ 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) {
// if first chunk parse failed, notice user to check response format
if(hasSuccessRequest) {
val errorMsg = """
**Failed** to parse response.origin response is:
<code>${sse.data}</code>
please check your response format:
**$responseFormat**\n""".trimIndent()

// TODO add refresh feature
logger.warn(errorMsg)
} else {
logger.warn("Failed to parse response.origin response is: ${sse.data}")
}
return@blockingForEach
parseFailedResponses.add(sse.data)
logger.warn("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
} else {
hasSuccessRequest = true
output += chunk
trySend(chunk)
}

hasSuccessRequest = true

output += chunk
trySend(chunk)
} else {
val result: ChatCompletionResult =
ObjectMapper().readValue(sse!!.data, ChatCompletionResult::class.java)
Expand All @@ -124,6 +112,19 @@ open class CustomSSEProcessor(private val project: Project) {
}
}

// when stream finished, check if any response parsed succeeded
// if not, notice user check response format
if (!hasSuccessRequest) {
val errorMsg = """
|**Failed** to parse response.please check your response format:
|**$responseFormat** origin responses is:
|- ${parseFailedResponses.joinToString("\n- ")}
|""".trimMargin()

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