Files
deepset-ai--haystack/docs-website/reference_versioned_docs/version-2.22/integrations-api/pyversity.md
T
wehub-resource-sync c56bef871b
Sync docs with Docusaurus / sync (push) Waiting to run
Tests / Check if changed (push) Waiting to run
Tests / format (push) Blocked by required conditions
Tests / check-imports (push) Blocked by required conditions
Tests / Unit / macos-latest (push) Blocked by required conditions
Tests / Unit / ubuntu-latest (push) Blocked by required conditions
Tests / Unit / windows-latest (push) Blocked by required conditions
Tests / mypy (push) Blocked by required conditions
Tests / Integration / ubuntu-latest (push) Blocked by required conditions
Tests / Integration / macos-latest (push) Blocked by required conditions
Tests / Integration / windows-latest (push) Blocked by required conditions
Tests / notify-slack-on-failure (push) Blocked by required conditions
Tests / Mark tests as completed (push) Blocked by required conditions
Docker image release / Build base image (push) Waiting to run
CodeQL / Analyze (python) (push) Has been cancelled
Update Platform Components Table / update (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:28 +08:00

126 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "pyversity"
id: integrations-pyversity
description: "pyversity integration for Haystack"
slug: "/integrations-pyversity"
---
## haystack_integrations.components.rankers.pyversity.ranker
Haystack integration for `pyversity <https://github.com/Pringled/pyversity>`\_.
Wraps pyversity's diversification algorithms as a Haystack `@component`,
making it easy to drop result diversification into any Haystack pipeline.
### PyversityRanker
Reranks documents using [pyversity](https://github.com/Pringled/pyversity)'s diversification algorithms.
Balances relevance and diversity in a ranked list of documents. Documents
must have both `score` and `embedding` populated (e.g. as returned by
a dense retriever with `return_embedding=True`).
Usage example:
```python
from haystack import Document
from haystack_integrations.components.rankers.pyversity import PyversityRanker
from pyversity import Strategy
ranker = PyversityRanker(top_k=5, strategy=Strategy.MMR, diversity=0.5)
docs = [
Document(content="Paris", score=0.9, embedding=[0.1, 0.2]),
Document(content="Berlin", score=0.8, embedding=[0.3, 0.4]),
]
output = ranker.run(documents=docs)
docs = output["documents"]
```
#### __init__
```python
__init__(
top_k: int | None = None,
*,
strategy: Strategy = Strategy.DPP,
diversity: float = 0.5
) -> None
```
Creates an instance of PyversityRanker.
**Parameters:**
- **top_k** (<code>int | None</code>) Number of documents to return after diversification.
If `None`, all documents are returned in diversified order.
- **strategy** (<code>Strategy</code>) Pyversity diversification strategy (e.g. `Strategy.MMR`). Defaults to `Strategy.DPP`.
- **diversity** (<code>float</code>) Trade-off between relevance and diversity in [0, 1].
`0.0` keeps only the most relevant documents; `1.0` maximises
diversity regardless of relevance. Defaults to `0.5`.
**Raises:**
- <code>ValueError</code> If `top_k` is not a positive integer or `diversity` is not in [0, 1].
#### to_dict
```python
to_dict() -> dict[str, Any]
```
Serializes the component to a dictionary.
**Returns:**
- <code>dict\[str, Any\]</code> Dictionary with serialized data.
#### from_dict
```python
from_dict(data: dict[str, Any]) -> PyversityRanker
```
Deserializes the component from a dictionary.
**Parameters:**
- **data** (<code>dict\[str, Any\]</code>) The dictionary to deserialize from.
**Returns:**
- <code>PyversityRanker</code> The deserialized component instance.
#### run
```python
run(
documents: list[Document],
top_k: int | None = None,
strategy: Strategy | None = None,
diversity: float | None = None,
) -> dict[str, list[Document]]
```
Rerank the list of documents using pyversity's diversification algorithm.
Documents missing `score` or `embedding` are skipped with a warning.
**Parameters:**
- **documents** (<code>list\[Document\]</code>) List of Documents to rerank. Each document must have `score` and `embedding` set.
- **top_k** (<code>int | None</code>) Overrides the initialized `top_k` for this call. `None` falls back to the initialized value.
- **strategy** (<code>Strategy | None</code>) Overrides the initialized `strategy` for this call. `None` falls back to the initialized value.
- **diversity** (<code>float | None</code>) Overrides the initialized `diversity` for this call.
`None` falls back to the initialized value.
**Returns:**
- <code>dict\[str, list\[Document\]\]</code> A dictionary with the following keys:
- `documents`: List of up to `top_k` reranked Documents, ordered by the diversification algorithm.
**Raises:**
- <code>ValueError</code> If `top_k` is not a positive integer or `diversity` is not in [0, 1].