<script lang="ts">
  import Form from "$lib/components/text-generation/Form.svelte";
  import Response from "$lib/components/text-generation/Response.svelte";

  let loading: boolean = false;
  let data: Record<string, any> = {}
  let bodyRequest: Record<string, any> = {}

  let form: Record<string, any> = {
    model: "meta-llama/Llama-2-7b-chat-hf",
    inputs: null,
    parameters: {
      return_full_text: false,
      do_sample: false,
      max_new_tokens: 20,
      temperature: 0.7,
      top_k: 25,
      stop_sequences: null
    },
  }

  const onchange = (newForm: Record<string, any>) => form = newForm

  const onsubmit = async () => {
    loading = true
    const request = await fetch('/api/text-generation', {
      method: 'POST',
      body: JSON.stringify(form),
      headers: {
        "Content-Type": "application/json"
      }
    })
    const response = await request.clone().json().catch(() => ({}))
    data = response
    bodyRequest = form
    loading = false
  }
</script>

<main class="min-h-screen w-full grid grid-cols-2">
  <div class="p-10 w-full flex flex-col gap-6">
    <Form form={form} onForm={onchange} />
    <div>
      <button
        class="bg-gradient-to-r from-slate-900/50 to-slate-900 border border-slate-800/40 text-white rounded-lg text-base transition-all duration-200 leading-relaxed outline-none relative py-3 px-6"
        disabled={loading}
        on:click={onsubmit}
      >
        Submit
      </button>
    </div>
  </div>
  <div class="border-l border-slate-900 bg-slate-950">
    <Response loading={loading} res={data} body={bodyRequest} form={form} endpoint={form.model} />
    response
  </div>
</main>