Skip to content

Commit 013b4a6

Browse files
authored
fix rerank top n (unit tests) (run-llama#19691)
1 parent ba8e4fb commit 013b4a6

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

llama-index-integrations/indices/llama-index-indices-managed-llama-cloud/tests/test_indices_managed_llama_cloud.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,40 @@ async def test_composite_retriever(index_name: str):
424424
assert any(n.node.metadata["pipeline_id"] == index1.id for n in nodes)
425425
assert any(n.node.metadata["pipeline_id"] == index1.id for n in nodes)
426426

427+
# Test additional rerank_top_n configurations to cover the injection logic
428+
429+
# Test retriever with only rerank_top_n=1 (no existing rerank_config)
430+
retriever_with_rerank_top_n = LlamaCloudCompositeRetriever(
431+
name="composite_retriever_test_2",
432+
project_name=project_name,
433+
api_key=api_key,
434+
base_url=base_url,
435+
create_if_not_exists=True,
436+
mode=CompositeRetrievalMode.FULL,
437+
rerank_top_n=1,
438+
)
439+
retriever_with_rerank_top_n.add_index(index1)
440+
retriever_with_rerank_top_n.add_index(index2)
441+
nodes = retriever_with_rerank_top_n.retrieve("Hello world.")
442+
assert len(nodes) <= 1 # Should be limited to 1 result by rerank_top_n
443+
444+
# Test retriever with both rerank_top_n and custom rerank_config
445+
custom_config = ReRankConfig(top_n=10, model="test-model")
446+
retriever_with_both = LlamaCloudCompositeRetriever(
447+
name="composite_retriever_test_3",
448+
project_name=project_name,
449+
api_key=api_key,
450+
base_url=base_url,
451+
create_if_not_exists=True,
452+
mode=CompositeRetrievalMode.FULL,
453+
rerank_top_n=2,
454+
rerank_config=custom_config,
455+
)
456+
retriever_with_both.add_index(index1)
457+
retriever_with_both.add_index(index2)
458+
nodes = retriever_with_both.retrieve("Hello world.")
459+
assert len(nodes) >= 2 # Should have results from both indices
460+
427461
# Retrieve nodes using the composite retriever
428462
nodes = await retriever.aretrieve("Hello world.")
429463

@@ -432,6 +466,14 @@ async def test_composite_retriever(index_name: str):
432466
assert any(n.node.metadata["pipeline_id"] == index1.id for n in nodes)
433467
assert any(n.node.metadata["pipeline_id"] == index1.id for n in nodes)
434468

469+
# Test async retrieve with the rerank_top_n only retriever
470+
nodes = await retriever_with_rerank_top_n.aretrieve("Hello world.")
471+
assert len(nodes) >= 1
472+
473+
# Test async retrieve with the both rerank_top_n and rerank_config retriever
474+
nodes = await retriever_with_both.aretrieve("Hello world.")
475+
assert len(nodes) >= 2
476+
435477

436478
@pytest.mark.skipif(
437479
not base_url or not api_key, reason="No platform base url or api key set"

llama-index-integrations/indices/llama-index-indices-managed-llama-cloud/uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)