File size: 1,936 Bytes
8b41737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { DatasetInfo, QuestionData, DatasetSummary, Preset } from "./types";

const BASE = "/api/model";
const PRESETS_BASE = "/api/presets/model";

async function fetchJSON<T>(url: string, opts?: RequestInit): Promise<T> {
  const res = await fetch(url, {
    headers: { "Content-Type": "application/json" },
    ...opts,
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: res.statusText }));
    throw new Error(err.error || res.statusText);
  }
  return res.json();
}

export const api = {
  loadDataset(repo: string, column?: string, split?: string, promptColumn?: string) {
    return fetchJSON<DatasetInfo & { columns: string[]; question_fingerprint: string }>(`${BASE}/datasets/load`, {
      method: "POST",
      body: JSON.stringify({ repo, column, split, prompt_column: promptColumn }),
    });
  },

  listDatasets() {
    return fetchJSON<DatasetInfo[]>(`${BASE}/datasets/`);
  },

  getQuestion(dsId: string, idx: number) {
    return fetchJSON<QuestionData>(`${BASE}/datasets/${dsId}/question/${idx}`);
  },

  getSummary(dsId: string) {
    return fetchJSON<DatasetSummary>(`${BASE}/datasets/${dsId}/summary`);
  },

  unloadDataset(dsId: string) {
    return fetchJSON<{ status: string }>(`${BASE}/datasets/${dsId}`, { method: "DELETE" });
  },

  listPresets() {
    return fetchJSON<Preset[]>(`${PRESETS_BASE}`);
  },

  createPreset(name: string, repo: string, column: string, split?: string) {
    return fetchJSON<Preset>(`${PRESETS_BASE}`, {
      method: "POST",
      body: JSON.stringify({ name, repo, column, split }),
    });
  },

  updatePreset(id: string, updates: { name?: string; column?: string; split?: string }) {
    return fetchJSON<Preset>(`${PRESETS_BASE}/${id}`, {
      method: "PUT",
      body: JSON.stringify(updates),
    });
  },

  deletePreset(id: string) {
    return fetchJSON<{ status: string }>(`${PRESETS_BASE}/${id}`, { method: "DELETE" });
  },
};