/*
 * kcpp / ICPP - the Internet C++ compiler and interpreter
 *
 * Copyright (c) 2026 Muhammad Anisur Rahman
 * Free to use and distribute under the MIT License.
 *
 * SPDX-License-Identifier: MIT
 * See the LICENSE file at the repository root for full terms.
 */
/* Chatbot <-> ICPP-created-LLM communication: one "training" script builds
   a next-char model (embedding [256,2] + head [2,256] wired so h->i and
   i->h - handcrafted weights keep this test deterministic; the REAL
   trained path is already covered by training_loop_check.c), persists it
   with tensor_save. Then - as a chat session would - the model is loaded
   BACK from disk with tensor_load and bot_llm_generate (typeless function,
   icpp_chatbot.c) produces text from it, decoded with tensor_detokenize.
   Proves the full save -> load -> generate -> text pipeline. */
#include "icpp_chatbot.c"

int main(void)
{
    int emb;
    int w;
    int b;
    int emb2;
    int w2;
    int b2;
    int out_ids;
    int last;

    /* --- "training" script side: build + save the model --- */
    emb = tensor_new(256, 2);
    tensor_set(emb, 104, 0, 1.0);   /* 'h' -> [1,0] */
    tensor_set(emb, 105, 1, 1.0);   /* 'i' -> [0,1] */
    w = tensor_new(2, 256);
    tensor_set(w, 0, 105, 10.0);    /* seeing 'h' -> predict 'i' */
    tensor_set(w, 1, 104, 10.0);    /* seeing 'i' -> predict 'h' */
    b = tensor_new(1, 256);
    tensor_save(emb, "bot_model_emb.t");
    tensor_save(w,   "bot_model_w.t");
    tensor_save(b,   "bot_model_b.t");

    /* --- chat session side: LOAD the ICPP-created model and talk to it --- */
    emb2 = tensor_load("bot_model_emb.t");
    w2 = tensor_load("bot_model_w.t");
    b2 = tensor_load("bot_model_b.t");
    if (emb2 == 0)
    {
        iprint("model load FAILED");
        return 1;
    }
    out_ids = tensor_new(1, 4);
    last = bot_llm_generate(emb2, w2, b2, 104, 4, out_ids);
    iprint("llm chat says=[", tensor_detokenize(out_ids), "] last=", last);
    return 0;
}
