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
1.8 KiB47 linesraw
1package app
2
3import (
4	"context"
5	"errors"
6	"math"
7	"strings"
8	"testing"
9	"time"
10)
11
12func TestAdminCostsPerRequest(t *testing.T) {
13	s, _ := fixtureService(t)
14	s.cfg.JevPricePerMillion = 0.042
15	ctx := context.WithValue(context.Background(), contextKey{}, callContext{ID: "costly", Caller: "test"})
16	s.log.request(ctx, "search", map[string]string{"query": "q"}, "ok")
17	s.log.decision(ctx, "rerank", "jev", []byte(`{}`), []byte(`{"answers":{},"usage":{"input_tokens":1000000,"output_tokens":20}}`), nil, time.Millisecond)
18	// the local model reports no usage: priced from the request size (400 bytes, about 100 tokens)
19	s.log.decision(ctx, "passage", "local", []byte(strings.Repeat("x", 400)), []byte(`{"answers":{}}`), nil, time.Millisecond)
20	s.log.decision(ctx, "passage", "jev", []byte(`{}`), nil, errors.New("refused"), time.Millisecond)
21	free := context.WithValue(context.Background(), contextKey{}, callContext{ID: "free", Caller: "test"})
22	s.log.request(free, "contents", map[string]string{"urls": "u"}, "ok")
23
24	page, err := s.adminRequests(context.Background(), 0, "", false)
25	if err != nil {
26		t.Fatal(err)
27	}
28	got := map[string]map[string]any{}
29	for _, row := range page.Rows {
30		got[row["requestId"].(string)] = row
31	}
32	c := got["costly"]
33	if c["decisionCalls"] != int64(2) || c["inputTokens"] != int64(1000100) || math.Abs(c["jevUsd"].(float64)-0.0420042) > 1e-9 {
34		t.Fatalf("costly request: %v", c)
35	}
36	if f := got["free"]; f["decisionCalls"] != int64(0) || f["jevUsd"].(float64) != 0 {
37		t.Fatalf("request without decisions: %v", f)
38	}
39	o, err := s.adminOverview(context.Background())
40	if err != nil {
41		t.Fatal(err)
42	}
43	d := o.(map[string]any)["decisions"].(map[string]any)
44	if d["requests"] != int64(2) || d["calls"] != int64(2) || d["estimatedCalls"] != int64(1) {
45		t.Fatalf("overview decisions: %v", d)
46	}
47}