File size: 6,252 Bytes
5db30ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import asyncio
import sys
import unittest
from pathlib import Path

import httpx

ROOT = Path(__file__).resolve().parents[1]
SRC = ROOT / "src"
if str(SRC) not in sys.path:
    sys.path.insert(0, str(SRC))

from elbench.providers.openai_compatible import OpenAICompatibleClient  # noqa: E402
from elbench.schemas.config import ModelConfig, ProviderConfig  # noqa: E402
from elbench.schemas.evaluation import GenerationRequest  # noqa: E402


class OpenAICompatibleStreamingTest(unittest.TestCase):
    def test_streaming_chat_completion_accumulates_reasoning_and_content(self) -> None:
        async def handler(request: httpx.Request) -> httpx.Response:
            self.assertEqual(request.url.path, "/v1/chat/completions")
            body = b"".join(
                [
                    b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                    b'"choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":"think "},'
                    b'"finish_reason":null}]}\n\n',
                    b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                    b'"choices":[{"index":0,"delta":{"content":"","reasoning_content":"steps"},'
                    b'"finish_reason":null}]}\n\n',
                    b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                    b'"choices":[{"index":0,"delta":{"content":"Final answer is \\\\boxed{204}"},'
                    b'"finish_reason":null}]}\n\n',
                    b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                    b'"usage":{"prompt_tokens":10,"completion_tokens":20,'
                    b'"completion_tokens_details":{"reasoning_tokens":12}},'
                    b'"choices":[{"index":0,"delta":{"content":""},"finish_reason":"stop"}]}\n\n',
                    b"data: [DONE]\n\n",
                ]
            )
            return httpx.Response(200, headers={"Content-Type": "text/event-stream"}, content=body)

        client = self._client(handler)
        try:
            response = asyncio.run(
                client.generate(
                    sample=None,
                    request=GenerationRequest(
                        prompt="Solve.",
                        messages=[{"role": "user", "content": "Solve."}],
                        max_tokens=8192,
                        stream=True,
                    ),
                )
            )
        finally:
            asyncio.run(client.aclose())

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.text, "Final answer is \\boxed{204}")
        self.assertEqual(response.usage["completion_tokens_details"]["reasoning_tokens"], 12)
        self.assertEqual(
            response.raw_payload["choices"][0]["message"]["reasoning_content"],
            "think steps",
        )
        self.assertEqual(response.raw_payload["choices"][0]["finish_reason"], "stop")
        self.assertTrue(response.raw_payload["stream"])

    def test_streaming_chat_completion_requires_done_marker(self) -> None:
        async def handler(request: httpx.Request) -> httpx.Response:
            body = (
                b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                b'"choices":[{"index":0,"delta":{"content":"partial"},"finish_reason":null}]}\n\n'
            )
            return httpx.Response(200, headers={"Content-Type": "text/event-stream"}, content=body)

        client = self._client(handler)
        try:
            with self.assertRaises(RuntimeError):
                asyncio.run(
                    client.generate(
                        sample=None,
                        request=GenerationRequest(
                            prompt="Solve.",
                            messages=[{"role": "user", "content": "Solve."}],
                            max_tokens=8192,
                            stream=True,
                        ),
                    )
                )
        finally:
            asyncio.run(client.aclose())

    def test_streaming_chat_completion_respects_total_timeout(self) -> None:
        class SlowSseStream(httpx.AsyncByteStream):
            async def __aiter__(self):
                yield (
                    b'data: {"id":"stream-1","object":"chat.completion.chunk","model":"deepseek-r1",'
                    b'"choices":[{"index":0,"delta":{"reasoning_content":"still thinking"},'
                    b'"finish_reason":null}]}\n\n'
                )
                await asyncio.sleep(0.05)
                yield b"data: [DONE]\n\n"

        async def handler(request: httpx.Request) -> httpx.Response:
            return httpx.Response(
                200,
                headers={"Content-Type": "text/event-stream"},
                stream=SlowSseStream(),
            )

        client = self._client(handler, timeout=0.01)
        try:
            with self.assertRaises(httpx.TimeoutException):
                asyncio.run(
                    client.generate(
                        sample=None,
                        request=GenerationRequest(
                            prompt="Solve.",
                            messages=[{"role": "user", "content": "Solve."}],
                            max_tokens=8192,
                            stream=True,
                        ),
                    )
                )
        finally:
            asyncio.run(client.aclose())

    def _client(self, handler, *, timeout: float | None = None) -> OpenAICompatibleClient:
        client = OpenAICompatibleClient(
            ProviderConfig(provider_name="openai_compatible", adapter="openai_compatible"),
            ModelConfig(
                model_id="deepseek-r1-250528",
                provider_name="openai_compatible",
                model_name="deepseek-r1-250528",
                api_base="https://api.innospark.cn/v1",
                api_key_env=None,
                timeout=timeout,
            ),
        )
        client._client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
        return client


if __name__ == "__main__":
    unittest.main()