Ignore SSE comments to support openrouter streaming (#2432)

This commit is contained in:
johnpccd 2024-02-16 03:00:10 +01:00 committed by GitHub
parent c8ca3ff404
commit b2ee738bb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -367,13 +367,16 @@ class OAIAPICompatLargeLanguageModel(_CommonOAI_API_Compat, LargeLanguageModel):
for chunk in response.iter_lines(decode_unicode=True, delimiter=delimiter):
if chunk:
#ignore sse comments
if chunk.startswith(':'):
continue
decoded_chunk = chunk.strip().lstrip('data: ').lstrip()
chunk_json = None
try:
chunk_json = json.loads(decoded_chunk)
# stream ended
except json.JSONDecodeError as e:
logger.error(f"decoded_chunk error,delimiter={delimiter},decoded_chunk={decoded_chunk}")
logger.error(f"decoded_chunk error: {e}, delimiter={delimiter}, decoded_chunk={decoded_chunk}")
yield create_final_llm_result_chunk(
index=chunk_index + 1,
message=AssistantPromptMessage(content=""),