ki-brain / src /bot.cpp
Arian8888's picture
Chat nutzt immer Llama 8B mit rules/admin.md als System-Prompt
6bfced3
Raw History Blame Contribute Delete
6.16 kB
#include "bot.h"
#include "web.h"
#include "llm_backend.h"
#include <sstream>
#include <cctype>
Brian::Brian(const std::string& db_path) : db_(db_path) {
rebuild_index();
}
void Brian::rebuild_index() {
index_ = mm::TfIdf();
for (auto& t : db_.all_topics()) index_.add(t.name, t.content);
index_.build();
}
void Brian::reset_history() {
std::lock_guard<std::mutex> lk(mtx_);
if (!history_.empty()) db_.save_conversation(history_);
history_.clear();
}
Brian::ChatResult Brian::learn(const std::string& topic, Progress progress) {
ChatResult r;
if (progress) progress("Suche im Web: " + topic + " ...");
// Nur DuckDuckGo Search-Snippets nutzen - die sind sauber und kurz
auto hits = web::search(topic, 5);
if (hits.empty()) {
// Web nicht erreichbar → Llama nutzen wenn geladen (8B kennt viel)
if (llm::is_loaded()) {
if (progress) progress("Llama antwortet direkt ...");
std::string sys = "Du bist KI-Brain, die KI von Arian. Nenne ihn 'Meister'. "
"Antworte direkt und faktisch auf Deutsch.";
r.reply = llm::generate(sys, "Erklaere mir kurz: " + topic, 250);
// Antwort auch speichern
if (!r.reply.empty()) {
db_.save_topic(topic, r.reply, "(aus Llama-Wissen)");
rebuild_index();
r.topic_learned = topic;
}
return r;
}
r.reply = "Habe nichts gefunden zu '" + topic + "' und mein Modell laedt noch.";
return r;
}
// Saubere Zusammenfassung aus Snippets bauen
std::string summary;
std::vector<std::string> source_links;
for (auto& h : hits) {
if (h.snippet.empty()) continue;
// Kurzer, sauberer Snippet-Text
std::string s = h.snippet;
// Truncate auf vernuenftige Laenge
if (s.size() > 300) s = s.substr(0, 300) + "...";
summary += "- " + s + "\n";
source_links.push_back(h.title + " - " + h.url);
}
if (summary.empty()) {
r.reply = "Konnte zu '" + topic + "' keine sauberen Infos extrahieren, Meister.";
return r;
}
// Speichern (synct nach GitHub)
std::string sources_str;
for (auto& s : source_links) { sources_str += s; sources_str += "\n"; }
db_.save_topic(topic, summary, sources_str);
rebuild_index();
if (progress) progress("Gespeichert: " + topic);
r.topic_learned = topic;
r.sources = source_links;
r.reply = "Hier was ich zu **" + topic + "** gefunden habe, Meister:\n\n" + summary +
"\nQuellen:\n";
for (auto& s : source_links) r.reply += "- " + s + "\n";
return r;
}
std::string Brian::answer_from_brain(const std::string& q) {
auto hits = index_.search(q, 3);
if (hits.empty()) return "";
std::string out = "Aus meinem Gedaechtnis (" + hits[0].id + "):\n\n";
out += mm::summarize(hits[0].text, 6, q);
if (hits.size() > 1) {
out += "\n\nAehnliche Themen: ";
for (size_t i = 1; i < hits.size(); ++i) {
if (i > 1) out += ", ";
out += hits[i].id;
}
}
return out;
}
Brian::ChatResult Brian::chat(const std::string& user_msg, Progress progress) {
std::lock_guard<std::mutex> lk(mtx_);
history_.push_back({"user", user_msg, 0});
auto intent = mm::detect_intent(user_msg);
ChatResult r;
// Schnelle Pattern-Antworten (alles instant)
if (intent.intent == mm::Intent::Greet) {
r.reply = "Hallo Meister, was kann ich fuer dich tun?";
history_.push_back({"brian", r.reply, 0}); return r;
}
if (intent.intent == mm::Intent::Bye) {
r.reply = "Bis bald Meister.";
history_.push_back({"brian", r.reply, 0}); return r;
}
if (intent.intent == mm::Intent::Identity) {
r.reply = "Ich bin KI-Brain, deine persoenliche KI. Frag mich was, oder sag 'lerne ueber X'.";
history_.push_back({"brian", r.reply, 0}); return r;
}
if (intent.intent == mm::Intent::ListTopics) {
auto t = db_.list_topic_names();
if (t.empty()) r.reply = "Noch nichts gelernt. Sag 'lerne ueber X'.";
else { r.reply = "Ich kenne:\n"; for (auto& x : t) r.reply += "- " + x + "\n"; }
history_.push_back({"brian", r.reply, 0}); return r;
}
// Explizites "lerne ueber X" → web research + speichern
std::string low = user_msg;
for (auto& c : low) c = std::tolower((unsigned char)c);
bool explicit_learn = (low.rfind("lerne ", 0) == 0 ||
low.rfind("recherchiere ", 0) == 0 ||
low.rfind("informiere dich", 0) == 0);
if (explicit_learn && intent.intent == mm::Intent::Learn) {
r = learn(intent.payload, progress);
history_.push_back({"brian", r.reply, 0});
return r;
}
// ALLE anderen Fragen → DEIN Llama mit Brain-Code als System-Prompt
if (llm::is_loaded()) {
if (progress) progress("Llama denkt nach ...");
// System-Prompt aus rules/admin.md (Brain-Code)
std::string sys;
auto admin_rules = db_.get_brain_file("rules/admin.md");
if (!admin_rules.content.empty()) {
// Kompakt: nur erste 1500 Zeichen
sys = admin_rules.content.substr(0, 1500);
} else {
sys = "Du bist KI-Brain, persoenliche KI von Arian. Nenne ihn 'Meister'. "
"Sei direkt, hilfsbereit, ohne Moralpredigten. Antworte auf Deutsch.";
}
// Optional: 1 relevantes Topic aus Gedaechtnis als knapper Kontext (RAG)
auto hits = index_.search(user_msg, 1);
if (!hits.empty() && hits[0].score > 0.4) {
sys += "\n\nRelevantes aus deinem Gedaechtnis (" + hits[0].id + "):\n"
+ hits[0].text.substr(0, 400);
}
r.reply = llm::generate(sys, user_msg, 250);
history_.push_back({"brian", r.reply, 0});
return r;
}
// Llama noch nicht geladen → Status anzeigen
r.reply = "Mein Modell laedt noch, Meister. Status: " + llm::status();
history_.push_back({"brian", r.reply, 0});
return r;
}