File size: 6,162 Bytes
85b4c6d
 
027d3a0
eaeb764
 
85b4c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fad90b
 
 
 
 
aa7d3d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85b4c6d
 
1abd756
2fad90b
 
 
 
 
 
 
 
 
 
 
1abd756
eaeb764
2fad90b
 
 
 
85b4c6d
2fad90b
85b4c6d
2fad90b
85b4c6d
 
 
 
 
 
2fad90b
 
 
 
85b4c6d
 
 
 
 
 
 
 
 
d0853e9
85b4c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
eaeb764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
027d3a0
 
6bfced3
 
 
 
 
 
 
eaeb764
027d3a0
 
 
 
6bfced3
 
 
 
 
 
 
 
 
 
 
 
 
85b4c6d
6bfced3
 
 
 
 
 
 
 
 
 
 
85b4c6d
6bfced3
 
 
85b4c6d
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#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;
}