File size: 2,366 Bytes
b630916
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BASE = "/api/rlm-eval";

async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${BASE}${url}`, {
    headers: { "Content-Type": "application/json" },
    ...init,
  });
  if (!res.ok) {
    const body = await res.json().catch(() => ({}));
    throw new Error(body.error || `HTTP ${res.status}`);
  }
  return res.json();
}

const PRESETS_BASE = "/api/presets/rlm-eval";
async function fetchPresetsJson<T>(url: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${PRESETS_BASE}${url}`, {
    headers: { "Content-Type": "application/json" }, ...init,
  });
  if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error || `HTTP ${res.status}`); }
  return res.json();
}

export const api = {
  loadDataset: (repo: string, config?: string, split?: string) =>
    fetchJson<{
      id: string;
      repo: string;
      name: string;
      config: string;
      split: string;
      metadata: Record<string, unknown>;
      n_examples: number;
      n_rows: number;
    }>("/datasets/load", {
      method: "POST",
      body: JSON.stringify({
        repo,
        config: config || "rlm_call_traces",
        split: split || "train",
      }),
    }),

  getOverview: (dsId: string) =>
    fetchJson<Record<string, unknown>>(`/datasets/${dsId}/overview`),

  getExampleDetail: (dsId: string, exampleIdx: number) =>
    fetchJson<Record<string, unknown>>(`/datasets/${dsId}/example/${exampleIdx}`),

  getIterDetail: (dsId: string, exampleIdx: number, rlmIter: number) =>
    fetchJson<Record<string, unknown>>(
      `/datasets/${dsId}/example/${exampleIdx}/iter/${rlmIter}`
    ),

  unloadDataset: (dsId: string) =>
    fetchJson<{ status: string }>(`/datasets/${dsId}`, { method: "DELETE" }),

  listPresets: () => fetchPresetsJson<Record<string, unknown>[]>(""),

  createPreset: (preset: { name: string; repo: string; config: string; split: string }) =>
    fetchPresetsJson<Record<string, unknown>>("", {
      method: "POST",
      body: JSON.stringify(preset),
    }),

  updatePreset: (id: string, data: { name: string }) =>
    fetchPresetsJson<Record<string, unknown>>(`/${id}`, {
      method: "PUT",
      body: JSON.stringify(data),
    }),

  deletePreset: (id: string) =>
    fetchPresetsJson<{ status: string }>(`/${id}`, { method: "DELETE" }),
};