dieya_risk_prediction_backend/tests/test_llm_protocol.py
2026-09-04 10:20:40 +08:00

58 lines
1.9 KiB
Python

import json
import httpx
import pytest
from app.core.config import get_settings
from app.services.llm_client import LLMClient
@pytest.mark.asyncio
async def test_openai_compatible_semantic_and_stream_protocol(monkeypatch: pytest.MonkeyPatch) -> None:
get_settings.cache_clear()
monkeypatch.setenv("RISK_LLM_API_KEY", "test-key")
monkeypatch.setenv("RISK_LLM_BASE_URL", "https://llm.test/v1")
monkeypatch.setenv("RISK_LLM_MODEL", "test-model")
calls = 0
async def handler(request: httpx.Request) -> httpx.Response:
nonlocal calls
calls += 1
if calls == 1:
return httpx.Response(
200,
json={
"choices": [
{
"message": {
"content": json.dumps(
{"intent": "assessment", "category": "洗发水", "confidence": 0.92},
ensure_ascii=False,
)
}
}
]
},
)
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
content=(
'data: {"choices":[{"delta":{"content":"第一段"}}]}\n\n'
'data: {"choices":[{"delta":{"content":"第二段"}}]}\n\n'
"data: [DONE]\n\n"
).encode(),
)
client = LLMClient(transport=httpx.MockTransport(handler))
semantic = await client.extract_semantics("评估洗发水新品")
chunks = [
chunk
async for chunk in client.stream_reply([{"role": "user", "content": "继续分析"}])
]
assert semantic["intent"] == "assessment"
assert semantic["confidence"] == 0.92
assert "".join(chunks) == "第一段第二段"
assert calls == 2
get_settings.cache_clear()