test_keyword_extraction_drivers.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. from types import SimpleNamespace
  2. from unittest.mock import AsyncMock, patch
  3. import pytest
  4. from lightrag.llm.lmdeploy import lmdeploy_model_if_cache
  5. from lightrag.llm.lollms import lollms_model_complete, lollms_model_if_cache
  6. from lightrag.llm.ollama import _ollama_model_if_cache, ollama_model_complete
  7. @pytest.mark.offline
  8. @pytest.mark.asyncio
  9. async def test_ollama_response_format_forwards_to_inner():
  10. hashing_kv = SimpleNamespace(global_config={"llm_model_name": "ollama-model"})
  11. with patch(
  12. "lightrag.llm.ollama._ollama_model_if_cache",
  13. AsyncMock(return_value="{}"),
  14. ) as mocked_complete:
  15. await ollama_model_complete(
  16. prompt="hello",
  17. hashing_kv=hashing_kv,
  18. response_format={"type": "json_object"},
  19. )
  20. assert mocked_complete.await_args.kwargs["response_format"] == {
  21. "type": "json_object"
  22. }
  23. @pytest.mark.offline
  24. @pytest.mark.asyncio
  25. async def test_ollama_legacy_keyword_extraction_emits_deprecation_warning():
  26. """_ollama_model_if_cache is the canonical emission site for the shim."""
  27. captured_kwargs = {}
  28. class FakeAsyncClient:
  29. def __init__(self, *args, **kwargs):
  30. self._client = SimpleNamespace(aclose=AsyncMock())
  31. async def chat(self, **kwargs):
  32. captured_kwargs.update(kwargs)
  33. return {"message": {"content": "{}"}}
  34. with patch("lightrag.llm.ollama.ollama.AsyncClient", FakeAsyncClient):
  35. with pytest.warns(DeprecationWarning):
  36. await _ollama_model_if_cache(
  37. model="ollama-model",
  38. prompt="hello",
  39. keyword_extraction=True,
  40. )
  41. assert captured_kwargs["format"] == "json"
  42. assert "keyword_extraction" not in captured_kwargs
  43. assert "response_format" not in captured_kwargs
  44. @pytest.mark.offline
  45. @pytest.mark.asyncio
  46. async def test_ollama_complete_forwards_legacy_flag_downstream():
  47. """ollama_model_complete is a pure forwarder; the shim fires inside _if_cache."""
  48. hashing_kv = SimpleNamespace(global_config={"llm_model_name": "ollama-model"})
  49. with patch(
  50. "lightrag.llm.ollama._ollama_model_if_cache",
  51. AsyncMock(return_value="{}"),
  52. ) as mocked_complete:
  53. await ollama_model_complete(
  54. prompt="hello",
  55. hashing_kv=hashing_kv,
  56. keyword_extraction=True,
  57. )
  58. assert mocked_complete.await_args.kwargs.get("keyword_extraction") is True
  59. @pytest.mark.offline
  60. @pytest.mark.asyncio
  61. async def test_ollama_translates_json_object_response_format_to_native_format():
  62. captured_kwargs = {}
  63. class FakeAsyncClient:
  64. def __init__(self, *args, **kwargs):
  65. self._client = SimpleNamespace(aclose=AsyncMock())
  66. async def chat(self, **kwargs):
  67. captured_kwargs.update(kwargs)
  68. return {"message": {"content": "{}"}}
  69. with patch("lightrag.llm.ollama.ollama.AsyncClient", FakeAsyncClient):
  70. result = await _ollama_model_if_cache(
  71. model="ollama-model",
  72. prompt="hello",
  73. response_format={"type": "json_object"},
  74. )
  75. assert result == "{}"
  76. assert captured_kwargs["format"] == "json"
  77. assert "response_format" not in captured_kwargs
  78. @pytest.mark.offline
  79. @pytest.mark.asyncio
  80. async def test_ollama_unwraps_openai_json_schema_response_format():
  81. captured_kwargs = {}
  82. schema = {
  83. "type": "object",
  84. "properties": {"answer": {"type": "string"}},
  85. "required": ["answer"],
  86. }
  87. class FakeAsyncClient:
  88. def __init__(self, *args, **kwargs):
  89. self._client = SimpleNamespace(aclose=AsyncMock())
  90. async def chat(self, **kwargs):
  91. captured_kwargs.update(kwargs)
  92. return {"message": {"content": "{}"}}
  93. with patch("lightrag.llm.ollama.ollama.AsyncClient", FakeAsyncClient):
  94. result = await _ollama_model_if_cache(
  95. model="ollama-model",
  96. prompt="hello",
  97. response_format={
  98. "type": "json_schema",
  99. "json_schema": {"name": "answer_payload", "schema": schema},
  100. },
  101. )
  102. assert result == "{}"
  103. assert captured_kwargs["format"] == schema
  104. assert "response_format" not in captured_kwargs
  105. @pytest.mark.offline
  106. @pytest.mark.asyncio
  107. async def test_lollms_if_cache_strips_response_format_before_request():
  108. """lollms_model_if_cache drops response_format; lollms has no JSON mode."""
  109. captured_requests = []
  110. class FakeResponse:
  111. def __init__(self):
  112. pass
  113. async def __aenter__(self):
  114. return self
  115. async def __aexit__(self, *exc_info):
  116. return False
  117. async def text(self):
  118. return "{}"
  119. class FakeSession:
  120. def __init__(self, *args, **kwargs):
  121. pass
  122. async def __aenter__(self):
  123. return self
  124. async def __aexit__(self, *exc_info):
  125. return False
  126. def post(self, url, json):
  127. captured_requests.append(json)
  128. return FakeResponse()
  129. with patch("lightrag.llm.lollms.aiohttp.ClientSession", FakeSession):
  130. result = await lollms_model_if_cache(
  131. model="lollms-model",
  132. prompt="hello",
  133. response_format={"type": "json_object"},
  134. )
  135. assert result == "{}"
  136. assert captured_requests
  137. assert "response_format" not in captured_requests[0]
  138. @pytest.mark.offline
  139. @pytest.mark.asyncio
  140. async def test_lollms_if_cache_emits_deprecation_warning():
  141. class FakeResponse:
  142. async def __aenter__(self):
  143. return self
  144. async def __aexit__(self, *exc_info):
  145. return False
  146. async def text(self):
  147. return "{}"
  148. class FakeSession:
  149. def __init__(self, *args, **kwargs):
  150. pass
  151. async def __aenter__(self):
  152. return self
  153. async def __aexit__(self, *exc_info):
  154. return False
  155. def post(self, url, json):
  156. return FakeResponse()
  157. with patch("lightrag.llm.lollms.aiohttp.ClientSession", FakeSession):
  158. with pytest.warns(DeprecationWarning):
  159. await lollms_model_if_cache(
  160. model="lollms-model",
  161. prompt="hello",
  162. keyword_extraction=True,
  163. )
  164. @pytest.mark.offline
  165. @pytest.mark.asyncio
  166. async def test_lollms_complete_forwards_legacy_flag_downstream():
  167. hashing_kv = SimpleNamespace(global_config={"llm_model_name": "lollms-model"})
  168. with patch(
  169. "lightrag.llm.lollms.lollms_model_if_cache",
  170. AsyncMock(return_value="{}"),
  171. ) as mocked_complete:
  172. await lollms_model_complete(
  173. prompt="hello",
  174. hashing_kv=hashing_kv,
  175. keyword_extraction=True,
  176. )
  177. assert mocked_complete.await_args.kwargs.get("keyword_extraction") is True
  178. @pytest.mark.offline
  179. @pytest.mark.asyncio
  180. async def test_lmdeploy_strips_response_format_before_generation_config(monkeypatch):
  181. captured_gen_config_kwargs = {}
  182. class FakeGenerationConfig:
  183. def __init__(self, **kwargs):
  184. captured_gen_config_kwargs.update(kwargs)
  185. class FakeVersion:
  186. def __lt__(self, other):
  187. return False
  188. async def fake_generate(*_args, **_kwargs):
  189. yield SimpleNamespace(response="{}")
  190. monkeypatch.setattr(
  191. "lightrag.llm.lmdeploy.initialize_lmdeploy_pipeline",
  192. lambda **_kwargs: SimpleNamespace(generate=fake_generate),
  193. )
  194. import sys
  195. sys.modules["lmdeploy"] = SimpleNamespace(
  196. __version__="0.6.0",
  197. version_info=FakeVersion(),
  198. GenerationConfig=FakeGenerationConfig,
  199. )
  200. result = await lmdeploy_model_if_cache(
  201. model="lmdeploy-model",
  202. prompt="hello",
  203. response_format={"type": "json_object"},
  204. )
  205. assert result == "{}"
  206. assert "response_format" not in captured_gen_config_kwargs
  207. assert "keyword_extraction" not in captured_gen_config_kwargs