MCPcopy Create free account
hub / github.com/InferCore/InferCore / Retrieve

Method Retrieve

internal/retrieval/filekb.go:72–106  ·  view source on GitHub ↗

Retrieve scores chunks by token overlap with query (v1 demo).

(_ context.Context, query string, _ map[string]any)

Source from the content-addressed store, hash-verified

70
71// Retrieve scores chunks by token overlap with query (v1 demo).
72func (f *FileKB) Retrieve(_ context.Context, query string, _ map[string]any) (types.RetrievalResult, error) {
73 qTokens := tokenize(query)
74 if len(qTokens) == 0 {
75 return types.RetrievalResult{}, nil
76 }
77 type scored struct {
78 ch types.RetrievalChunk
79 s float64
80 }
81 var out []scored
82 for _, d := range f.docs {
83 dt := tokenize(d.text)
84 if len(dt) == 0 {
85 continue
86 }
87 s := overlapScore(qTokens, dt)
88 if s <= 0 {
89 continue
90 }
91 out = append(out, scored{
92 ch: types.RetrievalChunk{Text: d.text, Source: d.source, Score: s},
93 s: s,
94 })
95 }
96 sort.Slice(out, func(i, j int) bool { return out[i].s > out[j].s })
97 top := 5
98 if len(out) < top {
99 top = len(out)
100 }
101 chunks := make([]types.RetrievalChunk, 0, top)
102 for i := 0; i < top; i++ {
103 chunks = append(chunks, out[i].ch)
104 }
105 return types.RetrievalResult{Chunks: chunks}, nil
106}
107
108func tokenize(s string) []string {
109 s = strings.ToLower(s)

Callers

nothing calls this directly

Calls 2

tokenizeFunction · 0.85
overlapScoreFunction · 0.85

Tested by

no test coverage detected