File size: 946 Bytes
43e4cb7
d61fb4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43e4cb7
 
 
 
 
 
d61fb4a
 
 
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
import { error, json } from "@sveltejs/kit";
import {
  defaultTools,
  HfAgent,
  LLMFromEndpoint,
  LLMFromHub,
} from "@huggingface/agents";
import { HF_ACCESS_TOKEN, HF_ENDPOINT } from "$env/static/private";

export async function POST({ request }) {
  const r = await request.json();
  const { prompt, tools: selectedTools, filetypes } = r;
  const tools = defaultTools.filter((el) => selectedTools.includes(el.name));

  let agent;
  if (HF_ENDPOINT !== "") {
    agent = new HfAgent(
      HF_ACCESS_TOKEN,
      LLMFromEndpoint(HF_ACCESS_TOKEN, HF_ENDPOINT),
      tools
    );
  } else {
    agent = new HfAgent(HF_ACCESS_TOKEN, LLMFromHub(HF_ACCESS_TOKEN), tools);
  }

  const files = filetypes
    ? filetypes.map((el: string) => ({
        type: el,
      }))
    : undefined;

  let code = "";
  try {
    code = await agent.generateCode(prompt, files);
  } catch (e) {
    throw error(500, e as Error);
  }

  return json(code);
}