Repository navigation
Expand file tree
/
Copy patheval.go
More file actions
134 lines (119 loc) · 3.55 KB
/
Copy patheval.go
File metadata and controls
134 lines (119 loc) · 3.55 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package contexting
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
)
type EvalCase struct {
Query string `json:"query"`
ExpectAny []string `json:"expect_any"`
Description string `json:"description,omitempty"`
Category string `json:"category,omitempty"`
}
type EvalSummary struct {
Cases int `json:"cases"`
Top1Hits int `json:"top1_hits"`
Top3Hits int `json:"top3_hits"`
Top5Hits int `json:"top5_hits"`
HitAt1 float64 `json:"hit_at_1"`
HitAt3 float64 `json:"hit_at_3"`
HitAt5 float64 `json:"hit_at_5"`
MRR float64 `json:"mrr"`
FailedCases int `json:"failed_cases"`
ScoredCases int `json:"scored_cases"`
}
type CaseFile struct {
Version int `json:"version"`
Categories map[string]CaseCategory `json:"categories"`
}
type CaseCategory struct {
Description string `json:"description"`
Cases []EvalCase `json:"cases"`
}
type EvalResult struct {
Case EvalCase `json:"case"`
MatchedAt int `json:"matched_at"`
TopPaths []SearchResult `json:"top_paths"`
}
// LoadEvalCases reads the current versioned case format shared by eval and bench.
func LoadEvalCases(path string) ([]EvalCase, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read eval cases: %w", err)
}
var file CaseFile
if err := json.Unmarshal(data, &file); err != nil {
return nil, fmt.Errorf("parse eval cases: expected a version 2 object with categories: %w", err)
}
if file.Version != 2 {
return nil, fmt.Errorf("unsupported case file version: %d (expected 2)", file.Version)
}
categories := make([]string, 0, len(file.Categories))
for name := range file.Categories {
categories = append(categories, name)
}
sort.Strings(categories)
var cases []EvalCase
for _, name := range categories {
for _, entry := range file.Categories[name].Cases {
entry.Category = name
cases = append(cases, entry)
}
}
return cases, nil
}
func EvaluateSearch(index *ContextIndex, cases []EvalCase, opts SearchOptions) (EvalSummary, []EvalResult) {
summary := EvalSummary{Cases: len(cases)}
results := make([]EvalResult, 0, len(cases))
if len(cases) == 0 || index == nil || index.Tree == nil {
return summary, results
}
var reciprocalRankTotal float64
for _, evalCase := range cases {
if strings.TrimSpace(evalCase.Query) == "" || len(evalCase.ExpectAny) == 0 {
summary.FailedCases++
results = append(results, EvalResult{Case: evalCase, MatchedAt: -1})
continue
}
summary.ScoredCases++
searchResults := SearchHintsWithOptions(index, evalCase.Query, opts)
matchedAt := firstMatchRank(searchResults, evalCase.ExpectAny)
if matchedAt == 1 {
summary.Top1Hits++
}
if matchedAt > 0 && matchedAt <= 3 {
summary.Top3Hits++
}
if matchedAt > 0 && matchedAt <= 5 {
summary.Top5Hits++
}
if matchedAt > 0 {
reciprocalRankTotal += 1.0 / float64(matchedAt)
}
results = append(results, EvalResult{
Case: evalCase,
MatchedAt: matchedAt,
TopPaths: searchResults,
})
}
scored := float64(summary.ScoredCases)
if scored > 0 {
summary.HitAt1 = float64(summary.Top1Hits) / scored
summary.HitAt3 = float64(summary.Top3Hits) / scored
summary.HitAt5 = float64(summary.Top5Hits) / scored
summary.MRR = reciprocalRankTotal / scored
}
return summary, results
}
func firstMatchRank(results []SearchResult, expectAny []string) int {
for i, result := range results {
for _, expected := range expectAny {
if pathMatchesExpected(result.Path, expected) {
return i + 1
}
}
}
return -1
}