2026-02-23 15:17:36 +08:00
|
|
|
import asyncio
|
|
|
|
|
import importlib
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BACKEND_DIR = Path(__file__).resolve().parents[1]
|
|
|
|
|
if str(BACKEND_DIR) not in sys.path:
|
|
|
|
|
sys.path.insert(0, str(BACKEND_DIR))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
llm = importlib.import_module("llm")
|
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
|
pytest.skip("llm module dependencies are not available", allow_module_level=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_call_ollama_messages_roles_with_system(monkeypatch):
|
|
|
|
|
captured = {}
|
|
|
|
|
|
2026-05-24 23:30:32 +08:00
|
|
|
async def fake_generate(**kwargs):
|
|
|
|
|
captured["kwargs"] = kwargs
|
|
|
|
|
return {"response": "ok"}
|
2026-02-23 15:17:36 +08:00
|
|
|
|
2026-05-24 23:30:32 +08:00
|
|
|
monkeypatch.setattr(llm.client, "generate", fake_generate)
|
2026-02-23 15:17:36 +08:00
|
|
|
|
|
|
|
|
result = asyncio.run(
|
|
|
|
|
llm.call_ollama(
|
|
|
|
|
"user prompt body",
|
|
|
|
|
system_prompt="system prompt body",
|
|
|
|
|
tag="test",
|
|
|
|
|
temperature=0.1,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result["content"] == "ok"
|
2026-05-24 23:30:32 +08:00
|
|
|
assert captured["kwargs"]["prompt"] == "system prompt body\n\nuser prompt body"
|
|
|
|
|
assert captured["kwargs"]["raw"] is True
|
2026-02-23 15:17:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_call_ollama_messages_roles_without_system(monkeypatch):
|
|
|
|
|
captured = {}
|
|
|
|
|
|
2026-05-24 23:30:32 +08:00
|
|
|
async def fake_generate(**kwargs):
|
|
|
|
|
captured["kwargs"] = kwargs
|
|
|
|
|
return {"response": "ok"}
|
2026-02-23 15:17:36 +08:00
|
|
|
|
2026-05-24 23:30:32 +08:00
|
|
|
monkeypatch.setattr(llm.client, "generate", fake_generate)
|
2026-02-23 15:17:36 +08:00
|
|
|
|
|
|
|
|
result = asyncio.run(
|
|
|
|
|
llm.call_ollama(
|
|
|
|
|
"user prompt only",
|
|
|
|
|
system_prompt="",
|
|
|
|
|
tag="test-no-system",
|
|
|
|
|
temperature=0.1,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert result["content"] == "ok"
|
2026-05-24 23:30:32 +08:00
|
|
|
assert captured["kwargs"]["prompt"] == "user prompt only"
|
|
|
|
|
assert captured["kwargs"]["raw"] is True
|