Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
252 lines (238 loc) · 8.31 KB
/
Copy pathmain.go
File metadata and controls
252 lines (238 loc) · 8.31 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Command citations follows named source bytes through indexing and retrieval
// to the exact evidence selected for an answer. Run go run ./rag/citations
// from the examples module. Both model bindings are deterministic and offline.
package main
import (
"context"
jsonv2 "encoding/json/v2"
"errors"
"fmt"
"os"
"strings"
"github.com/Tangerg/scope/core/chat"
"github.com/Tangerg/scope/core/document"
"github.com/Tangerg/scope/core/embedding"
"github.com/Tangerg/scope/core/embeddingclient"
"github.com/Tangerg/scope/core/vectorstore"
"github.com/Tangerg/scope/core/vectorstore/inmemory"
"github.com/Tangerg/scope/etl"
"github.com/Tangerg/scope/etl/text"
"github.com/Tangerg/scope/rag"
ragchat "github.com/Tangerg/scope/rag/chat"
"github.com/Tangerg/scope/rag/index"
"github.com/Tangerg/scope/rag/vector"
)
const (
policySource = "manual/returns"
passageSeparator = "\n\n"
policy = "Return unopened purchases within 30 days."
question = "When can I return an unopened purchase?"
documentEmbeddingModel = "passages"
queryEmbeddingModel = "questions"
embeddingDimensions = 2
searchResultsPerStore = 2
contextTokenBudget = 60
)
var policyFile = policy + "\n\n" + strings.Repeat("Oversized historical return appendix. ", 100)
// This two-coordinate offline fixture counts actual calls at each purpose
// boundary. It is a demonstration of the contract, not an evaluated ranker.
type encoderCalls struct{ documents, queries int }
func (e *encoderCalls) encode(ctx context.Context, r *embedding.Request) (*embedding.Response, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
switch r.Options.Model {
case documentEmbeddingModel:
e.documents++
case queryEmbeddingModel:
e.queries++
default:
return nil, fmt.Errorf("unexpected encoding purpose %q", r.Options.Model)
}
out := make([]*embedding.Output, len(r.Texts))
for i, s := range r.Texts {
v := []float64{0, 1}
if strings.Contains(strings.ToLower(s), "return") {
v = []float64{1, 0}
}
out[i] = &embedding.Output{Embedding: v}
}
return embedding.NewResponse(out, nil)
}
type wordCounter struct{}
func (wordCounter) CountText(ctx context.Context, s string) (int, error) {
return len(strings.Fields(s)), ctx.Err()
}
func ingest(ctx context.Context, encoder *vector.Encoder, source, raw string) (*inmemory.Store, error) {
reader, err := text.NewReader(strings.NewReader(raw), text.ReaderConfig{SourceName: source})
if err != nil {
return nil, err
}
docs, err := reader.Read(ctx)
if err != nil {
return nil, err
}
ids := etl.NewSHA256IDGenerator(nil)
assign, err := etl.NewIDAssigner(etl.IDAssignerConfig{Generator: ids})
if err != nil {
return nil, err
}
docs, err = assign.Assign(ctx, docs)
if err != nil {
return nil, err
}
passages, err := etl.NewSeparatorSplitter(etl.SeparatorSplitterConfig{Separator: passageSeparator})
if err != nil {
return nil, err
}
splitter, err := etl.NewSplitter(etl.SplitterConfig{TextSplitter: passages})
if err != nil {
return nil, err
}
chunks, err := splitter.Split(ctx, docs)
if err != nil {
return nil, err
}
chunks, err = assign.Assign(ctx, chunks)
if err != nil {
return nil, err
}
store, err := inmemory.NewStore(ctx, inmemory.StoreConfig{Space: encoder.Space(), Dimensions: embeddingDimensions})
if err != nil {
return nil, err
}
writer, err := index.NewWriter(index.WriterConfig{Encoder: encoder, Destination: store})
if err != nil {
return nil, err
}
if err = writer.Write(ctx, chunks); err != nil {
return nil, err
}
return store, nil
}
type report struct {
answer, marker string
cited *document.Document
input string
ranked int
citations rag.Citations
calls encoderCalls
}
func run(ctx context.Context) (report, error) {
var result report
model := embedding.ModelFunc(result.calls.encode)
encoder, err := vector.NewEncoder(vector.EncoderConfig{Space: "offline/keyword-v1", Documents: embeddingclient.Config{Model: model, Options: embedding.Options{Model: documentEmbeddingModel}}, Queries: embeddingclient.Config{Model: model, Options: embedding.Options{Model: queryEmbeddingModel}}})
if err != nil {
return result, err
}
// The Host retains originals independently of either vector index.
originals := map[string]string{policySource: policyFile, "support/receipts": "Find your original receipt in order history."}
primary, err := ingest(ctx, encoder, policySource, originals[policySource])
if err != nil {
return result, err
}
notes, err := ingest(ctx, encoder, "support/receipts", originals["support/receipts"])
if err != nil {
return result, err
}
// This host prefers current policy to support notes. It preserves the native
// order within each source instead of comparing scores between distinct lists.
retrieve := rag.RetrieverFunc(func(ctx context.Context, q rag.Query) (rag.Candidates, error) {
coordinates, encodeErr := encoder.EncodeQuery(ctx, q.Text(), vectorstore.SearchOptions{TopK: searchResultsPerStore})
if encodeErr != nil {
return nil, encodeErr
}
var candidates rag.Candidates
for _, store := range []*inmemory.Store{primary, notes} {
res, queryErr := store.Query(ctx, coordinates)
if queryErr != nil {
return nil, queryErr
}
for _, row := range res.Results {
candidates = append(candidates, rag.Candidate{Document: row.Document, Score: rag.Score(row.Score.Float64())})
}
}
return candidates, nil
})
augmenter, err := ragchat.NewContextualAugmenter(ragchat.ContextualAugmenterConfig{MaxContextTokens: contextTokenBudget, TokenCounter: wordCounter{}})
if err != nil {
return result, err
}
preparer, err := ragchat.NewPreparer(ragchat.PreparerConfig{Retriever: retrieve, Augmenter: augmenter})
if err != nil {
return result, err
}
req, err := chat.NewRequest(chat.NewUserMessage(chat.NewTextPart(question)))
if err != nil {
return result, err
}
prepared, err := preparer.Prepare(ctx, req)
if err != nil {
return result, err
}
// A real model is injected at this same boundary. The deterministic answer
// fixture uses only the supplied prompt and fails if its evidence is absent.
response, err := prepared.Call(ctx, chat.ModelFunc(func(_ context.Context, r *chat.Request) (*chat.Response, error) {
result.input = r.Messages[0].Text()
_, body, ok := strings.Cut(result.input, "\n\n---------------------\n")
if !ok {
return nil, errors.New("model input has no evidence block")
}
block, tail, ok := strings.Cut(body, "\n---------------------\n\nQuery: ")
if !ok || !strings.HasPrefix(tail, question+"\n") {
return nil, errors.New("model input lost its question")
}
var rows []struct {
Citation string `json:"citation"`
Content string `json:"content"`
}
if parseErr := jsonv2.Unmarshal([]byte(block), &rows); parseErr != nil {
return nil, parseErr
}
var answer string
for _, row := range rows {
if strings.Contains(row.Content, "purchases within") {
answer = row.Content + " " + row.Citation
break
}
}
if answer == "" {
return nil, errors.New("no admitted passage answers the question")
}
msg := chat.NewAssistantMessage(chat.NewTextPart(answer))
return chat.NewResponse(&chat.Output{Message: &msg, FinishReason: chat.FinishReasonStop}, nil)
}))
if err != nil {
return result, err
}
result.answer = response.Text()
_, marker, ok := strings.Cut(result.answer, " [")
if !ok {
return result, errors.New("answer has no citation")
}
result.marker = "[" + marker
retrieval := prepared.Retrieval()
result.ranked = len(retrieval.Candidates)
result.citations = retrieval.Citations
result.cited, err = result.citations.Resolve(result.marker)
if err != nil {
return result, err
}
p := result.cited.Provenance
raw, available := originals[p.Source]
if !available || document.NewProvenance(p.Source, []byte(raw)).Revision != p.Revision {
return result, errors.New("original revision is unavailable")
}
return result, nil
}
func main() {
r, err := run(context.Background())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
p := r.cited.Provenance
chunk := p.Chunks[len(p.Chunks)-1]
fmt.Printf("%s\n%s → %s @ %s; chunk %d/%d\n", r.answer, r.marker, p.Source, p.Revision, chunk.Index+1, chunk.Total)
fmt.Printf("Embedding calls: %d document batches, %d query for two indexes. Evidence: %d/%d passages admitted.\n", r.calls.documents, r.calls.queries, r.citations.Len(), r.ranked)
}