版本比较
java_death 的AI(deepseek弱化版)
正文
增加 105 行删除 105 行
23 行未改动242526272828293031323233343536373839404041424343444546475 行未改动535455565757585960616262636465666768696970717273747575767778798081818283848585868788898990919293939495969798991001011021031031041051061071081091101111121121131141141151161171181181191201211211221231241254 行未改动1301311321331341341351361371371381391401411411421431431441451461471481491491501511521531541551551561571571581591601611621621631641651651661671681691691701711721731741751761771781781791801811811821831841841851861871885 行未改动1941951961971981981992002012022032032042052062072072082092102112122122132142142152162172182182192202212222232242252262272282282292302312322332332342352362362372382392404 行未改动2452462472482492492502512522532542552562562572582592592602612622632642642652662672672682692702702712722732732742752762762772782792792802812822822832842852862862872882892902912912922932942942952962972982992993003013023033043053063073073083093093103113123133143143153163173183183193203213213223233243253263273283283293303313323333343343353363373383383393403413423413423433443453453463473483493503503513523533543553553563573583593603613613623633643653663673673683693703713713723733743753763773783793793803813823833843853863873863873883893903903913923933943 行未改动3983994004014024024034044044054064074085 行未改动4144154164174184184194204214214224234244254264274284294304314314324334344352 行未改动4384394404414424424434444454454464474484494504514514524534544554564574584594604604614624634644654664664674684684694704714724734744744754764774784794804814824834844844854864874874884894904914914924934944954964964974984995005015025025035045055065075085095105115115125135145155155165175185192 行未改动5225235245255265265275285295295305315325335345355365375385395395405415425432 行未改动5465475485495505505515525535542 行未改动55755855956056156156256356456456556656756756856957057157157257357457557657657757857958057958058158258358458558658658758858959059159259259359459523 行未改动 vector<pair<string, string>> history; // 用户输入, AI回复 string current_topic; int conversation_length; time_t start_time;
Context() : current_topic(""), conversation_length(0) { start_time = time(0); }
void addInteraction(const string& user_input, const string& ai_response) { if (history.size() >= MEMORY_SIZE) { history.erase(history.begin()); } history.push_back({user_input, ai_response}); conversation_length++; }
string getSummary() const { if (history.empty()) return "新对话";
ostringstream summary; summary << "当前话题: " << current_topic << " | "; summary << "对话轮次: " << conversation_length << " | "; summary << "最后输入: " << (history.empty() ? "" : history.back().first.substr(0, 30));5 行未改动class KnowledgeBase {private: map<string, vector<pair<string, double>>> knowledge; // 主题 -> {信息, 置信度} map<string, vector<string>> patterns; // 模式匹配
public: KnowledgeBase() { initializeKnowledge(); }
void initializeKnowledge() { // 通用知识 addKnowledge("greeting", "你好!我是DeepSeek 0.001,一个简化的AI助手。", 0.95); addKnowledge("greeting", "Hello! I'm a simplified version of an AI assistant.", 0.95); addKnowledge("farewell", "再见!很高兴与你对话。", 0.9); addKnowledge("farewell", "Goodbye! It was nice talking to you.", 0.9);
// 事实性知识 addKnowledge("math", "1+1=2,这是基本的数学知识。", 0.99); addKnowledge("math", "π约等于3.14159。", 0.98); addKnowledge("science", "水在100°C沸腾。", 0.97); addKnowledge("science", "地球绕太阳公转。", 0.99);
// 模式匹配 patterns["time"] = {"现在几点", "what time", "当前时间"}; patterns["weather"] = {"天气", "weather", "下雨"}; patterns["joke"] = {"笑话", "joke", "funny"}; patterns["help"] = {"帮助", "help", "怎么用"};
// 推理规则 addKnowledge("logic", "如果A等于B,B等于C,那么A等于C。", 0.9); }
void addKnowledge(const string& topic, const string& info, double confidence) { knowledge[topic].push_back({info, confidence}); }
vector<pair<string, double>> search(const string& query, double min_confidence = 0.5) { vector<pair<string, double>> results; string lower_query = toLower(query);
for (const auto& topic : knowledge) { for (const auto& info : topic.second) { // 简单的关键词匹配 if (lower_query.find(toLower(topic.first)) != string::npos || lower_query.find(toLower(info.first.substr(0, 20))) != string::npos) { results.push_back(info); } } }
// 模式匹配 for (const auto& pattern : patterns) { for (const string& keyword : pattern.second) { if (lower_query.find(toLower(keyword)) != string::npos) { results.push_back({pattern.first + "相关", 0.7}); } } }
// 按置信度排序 sort(results.begin(), results.end(), sort(results.begin(), results.end(), [](const pair<string, double>& a, const pair<string, double>& b) { return a.second > b.second; });
return results; }
string toLower(const string& str) const { string result = str; transform(result.begin(), result.end(), result.begin(), ::tolower); return result;4 行未改动class ReasoningEngine {private: mt19937 rng; uniform_real_distribution<double> dist;
public: ReasoningEngine() : rng(random_device{}()), dist(0.0, 1.0) {}
// 简单的逻辑推理 string logicalReasoning(const string& query) { string lower_query = toLower(query);
// 数学问题 if (lower_query.find("+") != string::npos || if (lower_query.find("+") != string::npos || lower_query.find("-") != string::npos || lower_query.find("*") != string::npos || lower_query.find("/") != string::npos) { return "这是一个数学问题,我可以进行简单计算。"; }
// 比较问题 if (lower_query.find("大于") != string::npos || lower_query.find(">") != string::npos || lower_query.find("小于") != string::npos || lower_query.find("<") != string::npos) { return "这涉及比较逻辑。"; }
// 因果问题 if (lower_query.find("为什么") != string::npos || if (lower_query.find("为什么") != string::npos || lower_query.find("because") != string::npos || lower_query.find("原因") != string::npos) { return "这个问题需要因果推理。"; }
return ""; }
// 概率性回答 double calculateConfidence(const string& query, const string& response) { double confidence = 0.5;
// 基于关键词匹配增加置信度 vector<string> keywords = {"你好", "hello", "what", "how", "为什么", "who", "where"}; for (const auto& keyword : keywords) { if (toLower(query).find(toLower(keyword)) != string::npos) { confidence += 0.1; break; } }
// 随机扰动 confidence += dist(rng) * 0.2 - 0.1;
return min(max(confidence, 0.0), 1.0); }
string toLower(const string& str) { string result = str; transform(result.begin(), result.end(), result.begin(), ::tolower); return result;5 行未改动private: vector<string> connectors = { "而且", "另外", "同时", "但是", "然而", "因此", "所以", "例如" };
vector<string> starters = { "根据我的理解,", "我认为,", "从我的角度来看,", "我了解到,", "关于这个问题,", "对于这种情况,", "通常来说," };
public: string generateResponse(const string& base_response, const Context& context, double confidence) { ostringstream response;
// 添加开场白(基于置信度) if (confidence > 0.7 && dist(gen) > 0.5) { response << starters[rand() % starters.size()]; }
response << base_response;
// 添加连接词(如果上下文较长) if (context.conversation_length > 2 && confidence > 0.6) { response << " " << connectors[rand() % connectors.size()];
// 有时添加第二句话 if (dist(gen) > 0.7) { vector<string> followups = { "你觉得呢?", "你有什么看法?", "这很有趣。", "我的知识有限,可能有遗漏。", "希望这对你有帮助。" }; response << " " << followups[rand() % followups.size()]; } }
// 添加置信度提示(如果置信度较低) if (confidence < CONFIDENCE_THRESHOLD) { response << "(我对这个回答不太确定)"; }
return response.str(); }
mt19937 gen{random_device{}()}; uniform_real_distribution<double> dist{0.0, 1.0};};
4 行未改动 ReasoningEngine re; LanguageGenerator lg; shared_ptr<Context> context; vector<string> conversation_memory;
public: DeepSeek001() : context(make_shared<Context>()) { cout << "DeepSeek 0.001 初始化完成" << endl; cout << "记忆大小: " << MEMORY_SIZE << " | 置信度阈值: " << CONFIDENCE_THRESHOLD << endl; cout << "========================================" << endl; }
string processInput(const string& input) { if (input.empty()) return "请说些什么...";
// 检查特殊命令 if (handleSpecialCommands(input)) { return ""; }
// 1. 分析输入 string analyzed = analyzeInput(input);
// 2. 知识库搜索 auto knowledge_results = kb.search(input);
// 3. 逻辑推理 string reasoning_result = re.logicalReasoning(input);
// 4. 生成基础回应 string base_response = generateBaseResponse(input, knowledge_results, reasoning_result);
// 5. 计算置信度 double confidence = re.calculateConfidence(input, base_response);
// 6. 生成完整回应 string final_response = lg.generateResponse(base_response, *context, confidence);
// 7. 更新上下文 context->addInteraction(input, final_response); updateTopic(input);
// 8. 添加记忆 if (confidence > 0.6) { rememberImportantInfo(input, final_response); }
return final_response; }
private: string analyzeInput(const string& input) { // 简单的输入分析 string lower_input = toLower(input);
// 检测语言 bool has_chinese = false; bool has_english = false; for (char c : input) { if (c >= 0x4E00 && c <= 0x9FFF) has_chinese = true; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) has_english = true; }
// 检测问题类型 bool is_question = lower_input.find('?') != string::npos || bool is_question = lower_input.find('?') != string::npos || lower_input.find("吗") != string::npos || lower_input.find("what") != string::npos || lower_input.find("how") != string::npos || lower_input.find("why") != string::npos;
// 检测情感 vector<string> positive_words = {"开心", "高兴", "good", "great", "happy", "love"}; vector<string> negative_words = {"难过", "生气", "bad", "sad", "angry", "hate"};
bool is_positive = false; bool is_negative = false;
for (const auto& word : positive_words) { if (lower_input.find(word) != string::npos) is_positive = true; } for (const auto& word : negative_words) { if (lower_input.find(word) != string::npos) is_negative = true; }
ostringstream analysis; analysis << "分析: "; if (has_chinese && has_english) analysis << "中英混合 "; else if (has_chinese) analysis << "中文 "; else if (has_english) analysis << "英文 ";
if (is_question) analysis << "问题 "; if (is_positive) analysis << "积极 "; if (is_negative) analysis << "消极 ";
return analysis.str(); } string generateBaseResponse(const string& input,
string generateBaseResponse(const string& input, const vector<pair<string, double>>& knowledge, const string& reasoning) {
// 优先使用知识库结果 if (!knowledge.empty() && knowledge[0].second > CONFIDENCE_THRESHOLD) { return knowledge[0].first; }
// 使用推理结果 if (!reasoning.empty()) { return reasoning; }
// 生成基于上下文的回应 if (!context->history.empty()) { string last_user_input = context->history.back().first; string lower_last = toLower(last_user_input); string lower_input = toLower(input);
// 检查是否在延续话题 if (hasCommonWords(lower_last, lower_input)) { return continueConversation(input); } }
// 默认回应 return generateDefaultResponse(input); }
string continueConversation(const string& input) { vector<string> continuations = { "继续刚才的话题,", "关于这一点,", "进一步来说,", "另外我还想到," };
vector<string> responses = { "能告诉我更多细节吗?", "这是一个很好的讨论点。", "我对这个话题很感兴趣。", "让我们深入探讨一下。" }; return continuations[rand() % continuations.size()] +
return continuations[rand() % continuations.size()] + responses[rand() % responses.size()]; }
string generateDefaultResponse(const string& input) { vector<string> defaults = { "我明白了。", "有趣的观点。",3 行未改动 "根据我的理解,这很有意义。", "让我思考一下这个。", "谢谢分享这个观点。" };
string lower_input = toLower(input);
// 检测是否为问题 if (lower_input.find('?') != string::npos || lower_input.find("吗") != string::npos || lower_input.find("什么") != string::npos ||5 行未改动 "我不完全确定,但根据我的知识," }; return question_responses[rand() % question_responses.size()]; }
return defaults[rand() % defaults.size()]; }
void updateTopic(const string& input) { // 简单的主题检测 vector<pair<string, vector<string>>> topic_keywords = { {"数学", {"数学", "计算", "数字", "加", "减"}}, {"科学", {"科学", "物理", "化学", "生物"}}, {"技术", {"技术", "电脑", "程序", "AI", "人工智能"}}, {"生活", {"生活", "日常", "天气", "食物", "旅行"}}, {"哲学", {"哲学", "人生", "意义", "思考"}} };
string lower_input = toLower(input); for (const auto& topic : topic_keywords) { for (const string& keyword : topic.second) { if (lower_input.find(toLower(keyword)) != string::npos) {2 行未改动 } } } }
bool handleSpecialCommands(const string& input) { string lower_input = toLower(input);
if (lower_input == "reset" || lower_input == "重置") { context = make_shared<Context>(); cout << "对话已重置" << endl; return true; }
if (lower_input == "memory" || lower_input == "记忆") { cout << "\n=== 对话记忆 ===" << endl; for (const auto& memory : conversation_memory) { cout << "- " << memory << endl; } cout << "=================" << endl; return true; }
if (lower_input == "context" || lower_input == "上下文") { cout << "\n=== 当前上下文 ===" << endl; cout << context->getSummary() << endl; cout << "对话历史:" << endl; for (const auto& interaction : context->history) { cout << "用户: " << interaction.first.substr(0, 40) cout << "用户: " << interaction.first.substr(0, 40) << "..." << endl; cout << "AI: " << interaction.second.substr(0, 40) cout << "AI: " << interaction.second.substr(0, 40) << "..." << endl; } cout << "==================" << endl; return true; }
if (lower_input == "help" || lower_input == "帮助") { cout << "\n=== 可用命令 ===" << endl; cout << "reset / 重置 - 重置对话" << endl; cout << "memory / 记忆 - 查看记忆" << endl; cout << "context / 上下文 - 查看对话上下文" << endl; cout << "exit / 退出 - 结束程序" << endl; cout << "=================" << endl; return true; }
return false; }
void rememberImportantInfo(const string& input, const string& response) { // 记住重要信息(简化版本) string lower_input = toLower(input);
vector<string> important_keywords = { "名字", "name", "年龄", "age", "喜欢", "like", "不喜欢", "dislike", "重要", "important", "记得", "remember" };
for (const auto& keyword : important_keywords) { if (lower_input.find(toLower(keyword)) != string::npos) { ostringstream memory; memory << "用户提到: " << input.substr(0, 50); conversation_memory.push_back(memory.str());
// 限制记忆大小 if (conversation_memory.size() > 10) { conversation_memory.erase(conversation_memory.begin()); } break; } } }
bool hasCommonWords(const string& str1, const string& str2) { vector<string> words1 = splitWords(str1); vector<string> words2 = splitWords(str2);
int common_count = 0; for (const auto& word1 : words1) { if (word1.length() > 2) { // 忽略太短的词 for (const auto& word2 : words2) {2 行未改动 } } } }
return common_count >= 2; }
vector<string> splitWords(const string& str) { vector<string> words; istringstream stream(str); string word; while (stream >> word) { words.push_back(word); } return words; }
string toLower(const string& str) { string result = str; transform(result.begin(), result.end(), result.begin(), ::tolower); return result;2 行未改动
// ============== 主程序 ==============int main() { srand(time(0));
cout << "========================================" << endl; cout << " DeepSeek 0.001 - 简化AI助手" << endl; cout << "========================================" << endl; cout << "特性:" << endl;2 行未改动 cout << "- 知识库搜索" << endl; cout << "- 智能语言生成" << endl; cout << "输入 'help' 查看命令" << endl; cout << "========================================" << endl << endl;
DeepSeek001 ai; string input;
// 初始问候 cout << "AI: " << ai.processInput("hello") << endl;
while (true) { cout << "\n你: "; getline(cin, input);
if (input.empty()) { cout << "AI: 我还在听..." << endl; continue; }
string lower_input = input; transform(lower_input.begin(), lower_input.end(), lower_input.begin(), ::tolower); if (lower_input == "exit" || lower_input == "退出" ||
if (lower_input == "exit" || lower_input == "退出" || lower_input == "quit" || lower_input == "bye") { cout << "AI: " << ai.processInput("goodbye") << endl; cout << "\n=== 对话结束 ===" << endl; break; }
string response = ai.processInput(input); if (!response.empty()) { cout << "AI: " << response << endl; } }
return 0;}```