compute_hit_rate:v0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import weave
from typing import List
from typing import Dict
from typing import Any
@weave.op()
def compute_hit_rate(
model_output: List[Dict[str, Any]], contexts: List[Dict[str, Any]]
) -> float:
"""
Calculate the hit rate (precision) for a single query.
Args:
model_output (List[Dict[str, Any]]): The list of retrieved documents from the model.
Each dictionary contains:
- 'source': A unique identifier for the document.
- 'score': The relevance score of the document.
contexts (List[Dict[str, Any]]): A list of dictionaries representing the relevant contexts.
Each dictionary contains:
- 'source': A unique identifier for the relevant document.
Returns:
float: The hit rate (precision).
The hit rate (precision) measures the proportion of retrieved documents that are relevant.
This metric is useful for assessing the accuracy of the retrieval system by determining the relevance of the