Summarize streams with real progress: adapters reassemble SSE/NDJSON deltas and report 0-99% as tokens arrive
This commit is contained in:
parent
cd1b08fda0
commit
7fa99e8291
8 changed files with 246 additions and 42 deletions
|
|
@ -129,6 +129,32 @@ async def test_openai_compat_parses_and_sanitizes():
|
|||
assert res.model == "qwen"
|
||||
|
||||
|
||||
async def test_openai_compat_streams_with_progress():
|
||||
"""stream:true replies (SSE deltas) reassemble and tick progress."""
|
||||
import json as _json
|
||||
|
||||
def sse(request: httpx.Request) -> httpx.Response:
|
||||
payload = _json.loads(request.content.decode())
|
||||
assert payload["stream"] is True
|
||||
pieces = ['{"short": "Stand', 'up.", "detailed": "The team met."',
|
||||
', "key_points": ["a"]', "}"]
|
||||
lines = [f"data: {_json.dumps({'choices': [{'delta': {'content': p}}]})}"
|
||||
for p in pieces]
|
||||
lines.append("data: [DONE]")
|
||||
return httpx.Response(
|
||||
200, content=("\n\n".join(lines) + "\n\n").encode(),
|
||||
headers={"content-type": "text/event-stream"})
|
||||
|
||||
p = OpenAICompatProvider("http://llm:8000", model="qwen",
|
||||
http_client=mock_client(sse))
|
||||
seen_pcts = []
|
||||
res = await p.summarize("t", on_progress=seen_pcts.append)
|
||||
assert res.short == "Standup."
|
||||
assert res.key_points == ("a",)
|
||||
assert seen_pcts and seen_pcts == sorted(seen_pcts)
|
||||
assert all(0 <= p <= 99 for p in seen_pcts) # never claims 100 early
|
||||
|
||||
|
||||
async def test_openai_compat_partial_json_gets_defaults():
|
||||
import json as _json
|
||||
|
||||
|
|
@ -165,14 +191,21 @@ async def test_ollama_happy_path():
|
|||
async def ok(request: httpx.Request) -> httpx.Response:
|
||||
assert request.url.path == "/api/chat"
|
||||
payload = _json.loads(request.content.decode())
|
||||
assert payload["format"] == "json" and payload["stream"] is False
|
||||
return httpx.Response(200, json={
|
||||
"message": {"content": _json.dumps({"short": "S.", "detailed": "D."})}
|
||||
})
|
||||
assert payload["format"] == "json" and payload["stream"] is True
|
||||
body = "\n".join([
|
||||
_json.dumps({"message": {"content": "{\"short\": \"S."}}),
|
||||
_json.dumps({"message": {"content": "\", \"detailed\": \"D.\"}"}}),
|
||||
_json.dumps({"done": True}),
|
||||
])
|
||||
return httpx.Response(
|
||||
200, json=None, content=body.encode(),
|
||||
headers={"content-type": "application/x-ndjson"})
|
||||
p = OllamaProvider("http://ollama:11434", model="llama3",
|
||||
http_client=mock_client(ok))
|
||||
res = await p.summarize("meeting notes")
|
||||
seen_pcts = []
|
||||
res = await p.summarize("meeting notes", on_progress=seen_pcts.append)
|
||||
assert res.short == "S." and res.model == "llama3"
|
||||
assert seen_pcts and seen_pcts == sorted(seen_pcts) # monotonic progress
|
||||
|
||||
|
||||
async def test_ollama_404_is_config_error():
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue