File size: 4,761 Bytes
4655dd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { API_BASE } from "./utils"

export async function apiFetch(path: string, opts: RequestInit = {}) {
  const res = await fetch(`${API_BASE}${path}`, {
    ...opts,
    headers: {
      "Content-Type": "application/json",
      ...(opts.headers || {}),
    },
  })
  if (!res.ok) {
    const txt = await res.text()
    let msg = txt
    try { msg = JSON.parse(txt).detail || txt } catch {}
    throw new Error(msg)
  }
  // if streaming
  if (res.headers.get("content-type")?.includes("text/event-stream")) return res
  const ct = res.headers.get("content-type")
  if (ct?.includes("application/json")) return res.json()
  return res
}

export async function loadModel(payload: any) {
  return apiFetch("/api/model/load", { method: "POST", body: JSON.stringify(payload) })
}
export async function getModelStatus() { return apiFetch("/api/model/status") }
export async function unloadModel() { return apiFetch("/api/model/unload", { method: "DELETE" }) }
export async function validatePath(path: string) { return apiFetch(`/api/model/validate?path=${encodeURIComponent(path)}`) }
export async function getTelemetry() { return apiFetch("/api/telemetry/") }
export async function getHistory(limit=120) { return apiFetch(`/api/telemetry/history?limit=${limit}`) }
export async function runBenchmark(payload: any) { return apiFetch("/api/benchmark/run", { method: "POST", body: JSON.stringify(payload) }) }
export async function getBenchmarkSuites() { return apiFetch("/api/benchmark/suites") }
export async function getBenchmarkResults() { return apiFetch("/api/benchmark/results") }
export async function scanCustomFolder(folder_path: string) { return apiFetch(`/api/benchmark/custom/scan?folder_path=${encodeURIComponent(folder_path)}`, { method: "POST"}) }
export async function runCustomFromFolder(folder_path: string, judge_mode="regex", temperature=0.2) { return apiFetch(`/api/benchmark/custom/run-from-folder?folder_path=${encodeURIComponent(folder_path)}&judge_mode=${judge_mode}&temperature=${temperature}`, { method: "POST"}) }
export async function uploadCustomDataset(files: FileList, judge_mode="regex") {
  const fd = new FormData()
  Array.from(files).forEach(f=> fd.append("files", f))
  fd.append("judge_mode", judge_mode)
  const res = await fetch(`${API_BASE}/api/benchmark/custom/upload`, { method: "POST", body: fd })
  if (!res.ok) throw new Error(await res.text())
  return res.json()
}
export async function getCustomFormats() { return apiFetch("/api/benchmark/custom/formats") }
export async function createShare(report_id: string) { return apiFetch(`/api/share/${report_id}`, { method: "POST"}) }
export async function getSharedReport(token: string) { return apiFetch(`/api/share/${token}`) }
export async function getPdfPreview() { return apiFetch(`/api/export/pdf/preview`) }
export async function runBenchmarkStream(payload: any, onEvent: (ev:any)=>void) {
  const res = await fetch(`${API_BASE}/api/benchmark/run-stream`, {
    method: "POST",
    headers: {"Content-Type":"application/json"},
    body: JSON.stringify(payload)
  })
  if (!res.ok) {
    const txt = await res.text()
    throw new Error(txt)
  }
  const reader = res.body?.getReader()
  const decoder = new TextDecoder()
  let buffer=""
  if (!reader) throw new Error("No reader")
  while(true){
    const {done, value} = await reader.read()
    if (done) break
    buffer += decoder.decode(value, {stream:true})
    const parts = buffer.split("\n\n")
    buffer = parts.pop() || ""
    for (const part of parts){
      if (part.startsWith("data: ")){
        const data = part.slice(6)
        try{ const j=JSON.parse(data); onEvent(j) }catch{}
      }
    }
  }
}

// Streaming helper
export async function streamGenerate(payload: any, onChunk: (chunk: any)=>void, onDone?: ()=>void, onError?: (e:any)=>void) {
  try {
    const res = await fetch(`${API_BASE}/api/generate`, {
      method: "POST",
      headers: {"Content-Type":"application/json"},
      body: JSON.stringify({...payload, stream: true})
    })
    if (!res.ok) {
      const txt = await res.text()
      throw new Error(txt)
    }
    const reader = res.body?.getReader()
    const decoder = new TextDecoder()
    let buffer = ""
    if (!reader) throw new Error("No reader")
    while (true) {
      const { done, value } = await reader.read()
      if (done) break
      buffer += decoder.decode(value, { stream: true })
      const lines = buffer.split("\n\n")
      buffer = lines.pop() || ""
      for (const line of lines) {
        if (line.startsWith("data: ")) {
          const data = line.slice(6)
          try {
            const json = JSON.parse(data)
            onChunk(json)
          } catch {}
        }
      }
    }
    onDone?.()
  } catch (e) {
    onError?.(e)
  }
}