fix: prevent duplicate LLM stream snapshots
This commit is contained in:
+4
-4
@@ -6,6 +6,8 @@ import urllib.request
|
||||
from collections.abc import Iterator
|
||||
from typing import Any
|
||||
|
||||
from llm_stream import OpenAIStreamAccumulator
|
||||
|
||||
|
||||
class ReviewAssistantError(RuntimeError):
|
||||
pass
|
||||
@@ -41,6 +43,7 @@ def stream_review_assistant(
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
yielded = False
|
||||
accumulator = OpenAIStreamAccumulator()
|
||||
for raw_line in response:
|
||||
line = raw_line.decode("utf-8", errors="replace").strip()
|
||||
if not line or line.startswith(":"):
|
||||
@@ -57,10 +60,7 @@ def stream_review_assistant(
|
||||
if not choices:
|
||||
continue
|
||||
choice = choices[0] or {}
|
||||
delta = choice.get("delta") or {}
|
||||
content = delta.get("content")
|
||||
if content is None:
|
||||
content = (choice.get("message") or {}).get("content")
|
||||
content = accumulator.feed(choice)
|
||||
if content:
|
||||
yielded = True
|
||||
yield str(content)
|
||||
|
||||
Reference in New Issue
Block a user