fix: Use WebDAV for tag creation and add LLM-as-a-judge for RAG tests

- Change create_tag() to use WebDAV POST instead of OCS API which
  returned 404 in some Nextcloud versions
- Add llm_judge() helper that evaluates system output against ground
  truth with simple TRUE/FALSE prompt
- Replace keyword-based assertions in RAG tests with LLM judge for
  more flexible semantic evaluation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2025-11-23 02:24:01 +01:00
co-authored by Claude
parent bf2fdac2d0
commit 2ab8dad6a5
2 changed files with 64 additions and 27 deletions
+17 -13
View File
@@ -1398,7 +1398,7 @@ class WebDAVClient(BaseNextcloudClient):
user_visible: bool = True, user_visible: bool = True,
user_assignable: bool = True, user_assignable: bool = True,
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Create a system tag via OCS API. """Create a system tag via WebDAV.
Args: Args:
name: Name of the tag to create name: Name of the tag to create
@@ -1411,12 +1411,10 @@ class WebDAVClient(BaseNextcloudClient):
Raises: Raises:
HTTPStatusError: If tag creation fails (409 if already exists) HTTPStatusError: If tag creation fails (409 if already exists)
""" """
# Use WebDAV POST with JSON body to create tag
response = await self._client.post( response = await self._client.post(
"/ocs/v2.php/apps/systemtags/api/v1/tags", "/remote.php/dav/systemtags/",
headers={ headers={"Content-Type": "application/json"},
"OCS-APIRequest": "true",
"Content-Type": "application/json",
},
json={ json={
"name": name, "name": name,
"userVisible": user_visible, "userVisible": user_visible,
@@ -1425,15 +1423,21 @@ class WebDAVClient(BaseNextcloudClient):
) )
response.raise_for_status() response.raise_for_status()
# Parse OCS response # Extract tag ID from Content-Location header (e.g., /remote.php/dav/systemtags/42)
data = response.json() content_location = response.headers.get("Content-Location", "")
ocs_data = data.get("ocs", {}).get("data", {}) tag_id = None
if content_location:
# Extract the numeric ID from the path
try:
tag_id = int(content_location.rstrip("/").split("/")[-1])
except (ValueError, IndexError):
pass
tag_info = { tag_info = {
"id": ocs_data.get("id"), "id": tag_id,
"name": ocs_data.get("name", name), "name": name,
"userVisible": ocs_data.get("userVisible", user_visible), "userVisible": user_visible,
"userAssignable": ocs_data.get("userAssignable", user_assignable), "userAssignable": user_assignable,
} }
logger.info(f"Created tag '{name}' with ID {tag_info['id']}") logger.info(f"Created tag '{name}' with ID {tag_info['id']}")
+47 -14
View File
@@ -42,6 +42,34 @@ logger = logging.getLogger(__name__)
# Default path to the Nextcloud User Manual PDF # Default path to the Nextcloud User Manual PDF
DEFAULT_MANUAL_PATH = "Nextcloud Manual.pdf" DEFAULT_MANUAL_PATH = "Nextcloud Manual.pdf"
async def llm_judge(
provider: "OpenAIProvider",
ground_truth: str,
system_output: str,
) -> bool:
"""Use LLM to judge if system output aligns with ground truth.
Args:
provider: OpenAI provider with generation capability
ground_truth: The expected/reference answer
system_output: The system's actual output to evaluate
Returns:
True if output aligns with ground truth, False otherwise
"""
prompt = f"""GROUND TRUTH: {ground_truth}
SYSTEM OUTPUT: {system_output}
Does the system output contain the key facts from the ground truth?
Answer: TRUE or FALSE"""
response = await provider.generate(prompt, max_tokens=10)
return "TRUE" in response.upper()
# Skip all tests if OpenAI API key not configured # Skip all tests if OpenAI API key not configured
pytestmark = [ pytestmark = [
pytest.mark.integration, pytest.mark.integration,
@@ -218,7 +246,7 @@ async def test_openai_embeddings_work(openai_provider: OpenAIProvider):
async def test_semantic_search_retrieval( async def test_semantic_search_retrieval(
nc_mcp_client, ground_truth_qa, indexed_manual_pdf nc_mcp_client, ground_truth_qa, indexed_manual_pdf, openai_generation_provider
): ):
"""Test that semantic search retrieves relevant documents from the manual. """Test that semantic search retrieves relevant documents from the manual.
@@ -228,7 +256,6 @@ async def test_semantic_search_retrieval(
# Use first query from ground truth # Use first query from ground truth
test_case = ground_truth_qa[0] # 2FA question test_case = ground_truth_qa[0] # 2FA question
query = test_case["query"] query = test_case["query"]
expected_topics = test_case["expected_topics"]
# Perform semantic search via MCP tool # Perform semantic search via MCP tool
result = await nc_mcp_client.call_tool( result = await nc_mcp_client.call_tool(
@@ -248,16 +275,21 @@ async def test_semantic_search_retrieval(
assert data["total_found"] > 0, f"No results for query: {query}" assert data["total_found"] > 0, f"No results for query: {query}"
assert len(data["results"]) > 0 assert len(data["results"]) > 0
# Check that at least one result contains expected topic keywords # Use LLM judge to evaluate if excerpts are relevant to ground truth
all_excerpts = " ".join([r["excerpt"].lower() for r in data["results"]]) all_excerpts = " ".join([r["excerpt"] for r in data["results"]])
topic_found = any(topic.lower() in all_excerpts for topic in expected_topics) is_relevant = await llm_judge(
assert topic_found, ( openai_generation_provider,
f"Expected topics {expected_topics} not found in results for query: {query}" test_case["ground_truth"],
all_excerpts,
) )
assert is_relevant, f"LLM judge: excerpts not relevant to query: {query}"
async def test_semantic_search_answer_with_sampling( async def test_semantic_search_answer_with_sampling(
nc_mcp_client_with_sampling, ground_truth_qa, indexed_manual_pdf nc_mcp_client_with_sampling,
ground_truth_qa,
indexed_manual_pdf,
openai_generation_provider,
): ):
"""Test semantic search with MCP sampling for answer generation. """Test semantic search with MCP sampling for answer generation.
@@ -314,12 +346,13 @@ async def test_semantic_search_answer_with_sampling(
assert data["generated_answer"] is not None assert data["generated_answer"] is not None
assert len(data["generated_answer"]) > 50 # Non-trivial answer assert len(data["generated_answer"]) > 50 # Non-trivial answer
# Check answer contains relevant content # Use LLM judge to evaluate answer relevance
answer_lower = data["generated_answer"].lower() is_relevant = await llm_judge(
assert any( openai_generation_provider,
keyword in answer_lower test_case["ground_truth"],
for keyword in ["two-factor", "2fa", "authentication", "password"] data["generated_answer"],
), f"Answer doesn't seem relevant to query: {data['generated_answer'][:200]}" )
assert is_relevant, f"LLM judge: answer not relevant to query: {query}"
@pytest.mark.parametrize( @pytest.mark.parametrize(