fix: prevent duplicate LLM stream snapshots
This commit is contained in:
+4
-3
@@ -10,6 +10,8 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from llm_stream import OpenAIStreamAccumulator
|
||||
|
||||
|
||||
class MentorAgentError(RuntimeError):
|
||||
pass
|
||||
@@ -211,6 +213,7 @@ def stream_with_mentor(
|
||||
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(":"):
|
||||
@@ -227,9 +230,7 @@ def stream_with_mentor(
|
||||
if not choices:
|
||||
continue
|
||||
choice = choices[0] or {}
|
||||
content = (choice.get("delta") or {}).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