| 2763 | // ============================================================================= |
| 2764 | |
| 2765 | int rcli_switch_llm(RCLIHandle handle, const char* model_id) { |
| 2766 | if (!handle || !model_id) return -1; |
| 2767 | auto* engine = static_cast<RCLIEngine*>(handle); |
| 2768 | if (!engine->initialized) return -1; |
| 2769 | |
| 2770 | std::lock_guard<std::mutex> lock(engine->mutex); |
| 2771 | |
| 2772 | auto models = rcli::all_models(); |
| 2773 | const auto* m = rcli::find_model_by_id(model_id, models); |
| 2774 | if (!m) { |
| 2775 | LOG_ERROR("RCLI", "Unknown model id: %s", model_id); |
| 2776 | return -1; |
| 2777 | } |
| 2778 | |
| 2779 | std::string model_path = engine->models_dir + "/" + m->filename; |
| 2780 | FILE* f = fopen(model_path.c_str(), "r"); |
| 2781 | if (!f) { |
| 2782 | LOG_ERROR("RCLI", "Model file not found: %s", model_path.c_str()); |
| 2783 | return -1; |
| 2784 | } |
| 2785 | fclose(f); |
| 2786 | |
| 2787 | LlmConfig new_config; |
| 2788 | new_config.model_path = model_path; |
| 2789 | new_config.n_gpu_layers = (engine->config_gpu_layers >= 0) ? engine->config_gpu_layers : 99; |
| 2790 | new_config.n_ctx = (engine->config_ctx_size > 0) ? engine->config_ctx_size : 4096; |
| 2791 | new_config.n_batch = 512; |
| 2792 | new_config.n_threads = 1; |
| 2793 | new_config.n_threads_batch = 8; |
| 2794 | new_config.temperature = 0.7f; |
| 2795 | new_config.max_tokens = 2048; |
| 2796 | new_config.flash_attn = true; |
| 2797 | new_config.type_k = 8; |
| 2798 | new_config.type_v = 8; |
| 2799 | |
| 2800 | LOG_INFO("RCLI", "Switching LLM to %s (%s)", m->name.c_str(), m->filename.c_str()); |
| 2801 | |
| 2802 | if (!engine->pipeline.reload_llm(new_config)) { |
| 2803 | LOG_ERROR("RCLI", "Failed to hot-swap LLM to %s", m->name.c_str()); |
| 2804 | return -1; |
| 2805 | } |
| 2806 | |
| 2807 | engine->llm_model_name = m->name; |
| 2808 | rcli::write_selected_model_id(model_id); |
| 2809 | |
| 2810 | engine->pipeline.tools().set_external_tool_definitions( |
| 2811 | engine->actions.get_definitions_json()); |
| 2812 | engine->pipeline.set_system_prompt(rastack::apply_personality( |
| 2813 | effective_base_prompt(engine), engine->personality_key)); |
| 2814 | engine->pipeline.recache_system_prompt(); |
| 2815 | |
| 2816 | LOG_INFO("RCLI", "LLM switched to %s (profile: %s)", |
| 2817 | m->name.c_str(), engine->pipeline.llm().profile().family_name.c_str()); |
| 2818 | return 0; |
| 2819 | } |
| 2820 | |
| 2821 | // ============================================================================= |
| 2822 | // Callbacks |
no test coverage detected