okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petadmin: show what each request's decisions would cost on Jev8af1784

main
7.8 KiB204 linesraw
1package app
2
3import (
4	"context"
5	"encoding/json"
6	"os"
7	"strings"
8	"time"
9)
10
11// decisionCost totals the decision-model calls of each request: tokens the
12// model reported, or about len/4 of the request body where it reported none
13// (the local model), so jevUsd is what the same calls would cost on Jev.
14// The filter keeps the scan to the requests asked about (decisions is indexed by
15// request_id and created_at).
16func decisionCost(filter string) string {
17	return `SELECT request_id, count(*) AS calls,
18 sum(coalesce(nullif(json_extract(response_json,'$.usage.input_tokens'),0), length(request_json)/4)) AS inputTokens,
19 coalesce(sum(json_extract(response_json,'$.usage.output_tokens')),0) AS outputTokens,
20 sum(coalesce(json_extract(response_json,'$.usage.input_tokens'),0) = 0) AS estimatedCalls,
21 group_concat(DISTINCT model) AS models
22 FROM decisions WHERE error IS NULL AND ` + filter + ` GROUP BY request_id`
23}
24
25// jevUsd prices decision input tokens at JEV_PRICE_PER_MILLION; Jev does not bill output.
26func (s *Service) jevUsd(row map[string]any) {
27	tokens, _ := row["inputTokens"].(int64)
28	row["jevUsd"] = float64(tokens) * s.cfg.JevPricePerMillion / 1e6
29}
30
31// attachCosts adds each row's decision calls, tokens and Jev price.
32func (s *Service) attachCosts(ctx context.Context, rows []map[string]any) error {
33	if len(rows) == 0 {
34		return nil
35	}
36	ids := make([]any, len(rows))
37	for i, row := range rows {
38		ids[i] = row["requestId"]
39	}
40	costs, err := s.log.adminRows(ctx, decisionCost("request_id IN (?"+strings.Repeat(",?", len(ids)-1)+")"), ids...)
41	if err != nil {
42		return err
43	}
44	byID := map[any]map[string]any{}
45	for _, c := range costs {
46		byID[c["request_id"]] = c
47	}
48	for _, row := range rows {
49		c := byID[row["requestId"]]
50		if c == nil {
51			c = map[string]any{"calls": int64(0), "inputTokens": int64(0)}
52		}
53		row["decisionCalls"], row["inputTokens"], row["decisionModels"] = c["calls"], c["inputTokens"], c["models"]
54		s.jevUsd(row)
55	}
56	return nil
57}
58
59type adminRequestPage struct {
60	Rows       []map[string]any `json:"rows"`
61	NextBefore int64            `json:"nextBefore,omitempty"`
62}
63
64type adminRequestTrace struct {
65	Requests    []map[string]any `json:"requests"`
66	Completions []map[string]any `json:"completions"`
67	Decisions   []map[string]any `json:"decisions"`
68	Evidence    []map[string]any `json:"evidence"`
69}
70
71func (s *Service) adminOverview(ctx context.Context) (any, error) {
72	since := time.Now().Add(-24 * time.Hour).Unix()
73	requests, err := s.log.adminRows(ctx, `SELECT count(*) AS total, coalesce(sum(outcome != 'ok'),0) AS errors
74	 FROM requests WHERE created_at >= ? AND endpoint IN ('search','contents','answer')`, since)
75	if err != nil {
76		return nil, err
77	}
78	models, err := s.log.adminRows(ctx, `SELECT count(*) AS calls, coalesce(sum(error IS NOT NULL),0) AS errors,
79	 coalesce(avg(latency_ms),0) AS meanLatencyMs FROM (
80	 SELECT error,latency_ms FROM completions WHERE created_at >= ? UNION ALL
81	 SELECT error,latency_ms FROM decisions WHERE created_at >= ?)`, since, since)
82	if err != nil {
83		return nil, err
84	}
85	tokens, err := s.log.adminRows(ctx, `SELECT coalesce(sum(prompt_tokens),0) AS input, coalesce(sum(completion_tokens),0) AS output,
86	 count(*) AS calls, coalesce(sum(prompt_tokens IS NOT NULL AND completion_tokens IS NOT NULL),0) AS reportedCalls
87	 FROM completions WHERE created_at >= ?`, since)
88	if err != nil {
89		return nil, err
90	}
91	decisions, err := s.log.adminRows(ctx, `SELECT count(DISTINCT r.request_id) AS requests, coalesce(sum(d.calls),0) AS calls,
92	 coalesce(sum(d.inputTokens),0) AS inputTokens, coalesce(sum(d.outputTokens),0) AS outputTokens,
93	 coalesce(sum(d.estimatedCalls),0) AS estimatedCalls
94	 FROM (SELECT DISTINCT request_id FROM requests WHERE created_at >= ? AND endpoint IN ('search','contents','answer')) r
95	 LEFT JOIN (`+decisionCost("created_at >= ?")+`) d USING (request_id)`, since, since)
96	if err != nil {
97		return nil, err
98	}
99	s.jevUsd(decisions[0])
100	var dbBytes int64
101	for _, suffix := range []string{"", "-wal", "-shm"} {
102		if info, err := os.Stat(s.cfg.DBPath + suffix); err == nil {
103			dbBytes += info.Size()
104		}
105	}
106	return map[string]any{"since": since, "requests": requests[0], "models": models[0], "tokens": tokens[0], "decisions": decisions[0], "databaseBytes": dbBytes,
107		"config": map[string]any{
108			"jevModel": s.cfg.JevModel, "summarizerModel": s.cfg.LLMModel, "jevPricePerMillion": s.cfg.JevPricePerMillion,
109			"jevConfigured": s.cfg.JevKey != "", "summarizerConfigured": s.cfg.LLMKey != "",
110			"searchConfigured":  s.cfg.KagiSession != "" || s.cfg.KagiToken != "",
111			"browserConfigured": s.cfg.BrowserPath != "", "passageCandidates": s.cfg.Passages,
112			"evidenceCharacters": s.cfg.EvidenceChars, "requestTimeoutSeconds": s.cfg.RequestTimeout.Seconds(),
113		}}, nil
114}
115
116func (s *Service) adminRequests(ctx context.Context, before int64, endpoint string, errorsOnly bool) (adminRequestPage, error) {
117	query := `SELECT id,created_at AS createdAt,request_id AS requestId,endpoint,caller,detail,outcome
118	 FROM requests WHERE endpoint IN ('search','contents','answer')`
119	args := []any{}
120	if before > 0 {
121		query += " AND id < ?"
122		args = append(args, before)
123	}
124	if endpoint != "" {
125		query += " AND endpoint = ?"
126		args = append(args, endpoint)
127	}
128	if errorsOnly {
129		query += " AND outcome != 'ok'"
130	}
131	rows, err := s.log.adminRows(ctx, query+" ORDER BY id DESC LIMIT 31", args...)
132	if err != nil {
133		return adminRequestPage{}, err
134	}
135	if err := s.attachCosts(ctx, rows); err != nil {
136		return adminRequestPage{}, err
137	}
138	page := adminRequestPage{Rows: rows}
139	if len(rows) > 30 {
140		page.Rows = rows[:30]
141		page.NextBefore = rows[29]["id"].(int64)
142	}
143	return page, err
144}
145
146func (l *Log) adminTrace(ctx context.Context, id string) (adminRequestTrace, error) {
147	var trace adminRequestTrace
148	for _, part := range []struct {
149		target *[]map[string]any
150		query  string
151	}{
152		{&trace.Requests, `SELECT id,created_at AS createdAt,endpoint,caller,detail,outcome FROM requests WHERE request_id = ? ORDER BY id LIMIT 100`},
153		{&trace.Completions, `SELECT id,created_at AS createdAt,task,model,request_json AS request,response_text AS response,error,prompt_tokens AS inputTokens,completion_tokens AS outputTokens,latency_ms AS latencyMs FROM completions WHERE request_id = ? ORDER BY id LIMIT 200`},
154		{&trace.Decisions, `SELECT id,created_at AS createdAt,task,model,request_json AS request,response_json AS response,error,latency_ms AS latencyMs FROM decisions WHERE request_id = ? ORDER BY id LIMIT 200`},
155		{&trace.Evidence, `SELECT id,source_id AS sourceId,policy_version AS policy,packet_json AS packet FROM evidence WHERE request_id = ? ORDER BY id LIMIT 100`},
156	} {
157		rows, err := l.adminRows(ctx, part.query, id)
158		if err != nil {
159			return trace, err
160		}
161		*part.target = rows
162	}
163	return trace, nil
164}
165
166// SQL projections are the admin read model; this does not modify training rows.
167// Parse valid JSON cells for inspection, retaining malformed legacy cells as text.
168func (l *Log) adminRows(ctx context.Context, query string, args ...any) ([]map[string]any, error) {
169	rows, err := l.db.QueryContext(ctx, query, args...)
170	if err != nil {
171		return nil, err
172	}
173	defer rows.Close()
174	columns, err := rows.Columns()
175	if err != nil {
176		return nil, err
177	}
178	result := []map[string]any{}
179	for rows.Next() {
180		values, pointers := make([]any, len(columns)), make([]any, len(columns))
181		for i := range values {
182			pointers[i] = &values[i]
183		}
184		if err := rows.Scan(pointers...); err != nil {
185			return nil, err
186		}
187		row := map[string]any{}
188		for i, name := range columns {
189			value := values[i]
190			if b, ok := value.([]byte); ok {
191				value = string(b)
192			}
193			if text, ok := value.(string); ok && (name == "detail" || name == "request" || name == "packet") {
194				var decoded any
195				if json.Unmarshal([]byte(text), &decoded) == nil {
196					value = decoded
197				}
198			}
199			row[name] = value
200		}
201		result = append(result, row)
202	}
203	return result, rows.Err()
204}