|
| 1 | +"""Phase 11.0 — tests for Together, Mistral, Cohere brain adapters.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from typing import Iterable |
| 5 | + |
| 6 | +import httpx |
| 7 | +import pytest |
| 8 | + |
| 9 | +from zhub.brains.base import ChatChunk |
| 10 | +from zhub.brains.together import TogetherAdapter |
| 11 | +from zhub.brains.mistral import MistralAdapter |
| 12 | +from zhub.brains.cohere import CohereAdapter |
| 13 | + |
| 14 | + |
| 15 | +class _FakeStream: |
| 16 | + def __init__(self, lines: Iterable[str]): |
| 17 | + self._lines = list(lines) |
| 18 | + |
| 19 | + async def aiter_lines(self): |
| 20 | + for line in self._lines: |
| 21 | + yield line |
| 22 | + |
| 23 | + async def __aenter__(self): |
| 24 | + return self |
| 25 | + |
| 26 | + async def __aexit__(self, *exc): |
| 27 | + return None |
| 28 | + |
| 29 | + |
| 30 | +class _FakeAsyncClient: |
| 31 | + def __init__(self, lines: Iterable[str]): |
| 32 | + self._lines = list(lines) |
| 33 | + self.last_call: dict | None = None |
| 34 | + |
| 35 | + def stream(self, method, url, **kw): |
| 36 | + self.last_call = {"method": method, "url": url, **kw} |
| 37 | + return _FakeStream(self._lines) |
| 38 | + |
| 39 | + async def aclose(self): |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +# ---- Together (OpenAI-compat) ------------------------------------------ |
| 44 | + |
| 45 | +def test_together_try_init_none_without_key(monkeypatch): |
| 46 | + monkeypatch.delenv("TOGETHER_API_KEY", raising=False) |
| 47 | + assert TogetherAdapter.try_init() is None |
| 48 | + |
| 49 | + |
| 50 | +def test_together_try_init_returns_adapter(monkeypatch): |
| 51 | + class R: |
| 52 | + status_code = 200 |
| 53 | + monkeypatch.setenv("TOGETHER_API_KEY", "tg_test") |
| 54 | + monkeypatch.setattr(httpx, "get", lambda url, headers=None, timeout=None: R()) |
| 55 | + a = TogetherAdapter.try_init() |
| 56 | + assert a is not None and a.name == "together" |
| 57 | + assert a.api_key == "tg_test" |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_together_stream_uses_openai_compat(): |
| 62 | + lines = [ |
| 63 | + 'data: {"choices":[{"delta":{"content":"hi"}}]}', |
| 64 | + 'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}', |
| 65 | + 'data: [DONE]', |
| 66 | + ] |
| 67 | + fake = _FakeAsyncClient(lines) |
| 68 | + a = TogetherAdapter(api_key="tg_test", model="meta-llama/Llama-3.3-70B-Instruct-Turbo", |
| 69 | + http=fake) |
| 70 | + out = [c async for c in a.stream([{"role": "user", "content": "x"}])] |
| 71 | + assert any(c.delta == "hi" for c in out) |
| 72 | + assert out[-1].done is True |
| 73 | + assert fake.last_call["headers"]["Authorization"] == "Bearer tg_test" |
| 74 | + |
| 75 | + |
| 76 | +# ---- Mistral (OpenAI-compat) ------------------------------------------- |
| 77 | + |
| 78 | +def test_mistral_try_init_none_without_key(monkeypatch): |
| 79 | + monkeypatch.delenv("MISTRAL_API_KEY", raising=False) |
| 80 | + assert MistralAdapter.try_init() is None |
| 81 | + |
| 82 | + |
| 83 | +def test_mistral_try_init_returns_adapter(monkeypatch): |
| 84 | + class R: |
| 85 | + status_code = 200 |
| 86 | + monkeypatch.setenv("MISTRAL_API_KEY", "ms_test") |
| 87 | + monkeypatch.setattr(httpx, "get", lambda url, headers=None, timeout=None: R()) |
| 88 | + a = MistralAdapter.try_init() |
| 89 | + assert a is not None and a.name == "mistral" |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_mistral_stream_uses_openai_compat(): |
| 94 | + lines = [ |
| 95 | + 'data: {"choices":[{"delta":{"content":"bonjour"}}]}', |
| 96 | + 'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}', |
| 97 | + 'data: [DONE]', |
| 98 | + ] |
| 99 | + fake = _FakeAsyncClient(lines) |
| 100 | + a = MistralAdapter(api_key="ms_test", model="mistral-large-latest", http=fake) |
| 101 | + out = [c async for c in a.stream([{"role": "user", "content": "x"}])] |
| 102 | + assert any(c.delta == "bonjour" for c in out) |
| 103 | + assert out[-1].done is True |
| 104 | + |
| 105 | + |
| 106 | +# ---- Cohere (custom v2 shape) ------------------------------------------ |
| 107 | + |
| 108 | +def test_cohere_try_init_none_without_key(monkeypatch): |
| 109 | + monkeypatch.delenv("COHERE_API_KEY", raising=False) |
| 110 | + assert CohereAdapter.try_init() is None |
| 111 | + |
| 112 | + |
| 113 | +def test_cohere_try_init_returns_adapter(monkeypatch): |
| 114 | + class R: |
| 115 | + status_code = 200 |
| 116 | + monkeypatch.setenv("COHERE_API_KEY", "co_test") |
| 117 | + monkeypatch.setattr(httpx, "get", lambda url, headers=None, timeout=None: R()) |
| 118 | + a = CohereAdapter.try_init() |
| 119 | + assert a is not None and a.name == "cohere" |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.asyncio |
| 123 | +async def test_cohere_stream_parses_v2_event_shape(): |
| 124 | + """Cohere v2 streams newline-JSON with type discriminator.""" |
| 125 | + lines = [ |
| 126 | + json.dumps({"type": "message-start", "id": "m_x"}), |
| 127 | + json.dumps({"type": "content-delta", |
| 128 | + "delta": {"message": {"content": {"text": "Howdy "}}}}), |
| 129 | + json.dumps({"type": "content-delta", |
| 130 | + "delta": {"message": {"content": {"text": "partner"}}}}), |
| 131 | + json.dumps({"type": "message-end", |
| 132 | + "delta": {"finish_reason": "complete"}}), |
| 133 | + ] |
| 134 | + fake = _FakeAsyncClient(lines) |
| 135 | + a = CohereAdapter(api_key="co_test", model="command-r-plus-08-2024", |
| 136 | + http=fake) |
| 137 | + out = [c async for c in a.stream([{"role": "user", "content": "x"}])] |
| 138 | + deltas = [c.delta for c in out if c.delta] |
| 139 | + assert "".join(deltas) == "Howdy partner" |
| 140 | + assert out[-1].done is True |
| 141 | + # `complete` normalized to `stop` |
| 142 | + assert out[-1].finish_reason == "stop" |
| 143 | + body = fake.last_call["json"] |
| 144 | + assert body["model"] == "command-r-plus-08-2024" |
| 145 | + assert body["messages"][-1] == {"role": "user", "content": "x"} |
| 146 | + headers = fake.last_call["headers"] |
| 147 | + assert headers["Authorization"] == "Bearer co_test" |
0 commit comments