Analyze URL Redirection with HTTP GET&POST

本文介绍了URL重定向的概念及其在Web开发中的应用,包括常见的HTTP状态码如301永久重定向、302临时重定向等,并解释了它们如何帮助避免旧链接失效及更新资源位置。

1 Introduction

URL redirection, also called URL forwarding and the very similar technique domain redirection also called domain forwarding are techniques on the World Wide Web for making a web page available under many URLs. HTTP allows servers to redirect a client request to a different location. Although, this will usually result in another network round trip, it has some useful applications:
    1).A web application may use redirection to navigate between parts of the application.
    2).If content has moved to a different URL or domain name, redirection can be used to avoid breaking old URLs or bookmarks.
    3).It is possible to convert a POST request to a GET request using redirection.
    4).A client can be directed to use its local cache for content that has not changed.
A server specifies redirection by returning a 3xx status code:

301

This indicates that the content now resides permanently at the location specified by the Location header and future requests should be directed to this location.

302

Same as 301, except that the new location is temporary and future requests should still be sent to the original location. Another feature of this status code is that if the original request was a POST the client will change to using a GET when it re-issues the request.

303

This status code was intended to be the only status code that caused a POST to be converted to a GET. However, most browsers treat a 302 like a 303.

304

Used in response to an If-Modified header to redirect a request to the browser's local cache.

305

A 305 response is used to specify that a request must be resubmitted through the HTTP proxy in the Location header

       This document is intended to help engineers understand how URL redirection works and how to implement URL redirection feature in WTOS. Besides, web logon mechanism will be introduced.

2 HTTP Headers

HTTP Headers are name/value pairs that appear in both request and response messages. The name of the header is separated from the value by a single colon. Syntax:
    message-header = field-name ":" [ field-value ]

For example, this line in a request message:

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Provides a header called User-Agent whose value is Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1). The purpose of this particular header is to supply the web server with information about the type of browser making the request. A complete definition of this and other commonly encountered HTTP headers can be found in the HTTP 1.1 specifications.

2.1 HTTP Request Headers

HTTP clients use headers in the request message to identify themselves and control how content is returned. If you are using IE, you will have seen the following headers sent with the request in example:

Accept:*/*

This header indicates that the browser will accept all types of content.

Accept-Language: en-gb

The browser prefers British English content.

Accept-Encoding: gzip, deflate

The browser can handle gzip or deflate compressed content

Connection Keep-Alive

The browser is requesting the use of persistent TCP connections.

Host: www.wyse.com

HTTP/1.1 requires that the host name is supplied with every request so that multiple domains can be hosted on a single IP address.

Referer: http://www.wyse.com/

This is supplied by the browser to indicate if the current request was the result of a link from another web page. The server may use this information to gather usage statistics or to track which web sites have links to a page.

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

This identifies the browser is Internet Explorer Version 6 running on Windows XP.

2.2 HTTP Response Headers

HTTP servers use headers in the response message to specify how content is being returned and how it should be handled. If you are using IE, you will have seen the following headers returned with the image in example:

Cache-Control: no-cache

This header indicates whether the resource may be cached by the browser or any immediate caches. The value no-cache disables all caching. (See 5. Caching for more information)

Content-Length: 2748

This header contains the length in bytes of the resource (i.e. the gif image) that follows the headers.

Content-Type: image/gif

The content is in GIF format.

Date: Wed, 4 Oct 2004 12:00:00 GMT

This is the current date and time on the web server.

Expires: -1

The Expires header specifies when the content should be considered to be out of date. The value -1 indicates that the content expires immediately and would have to be re-requested before being displayed again.

Pragma: no-cache

The browser may be connecting to the server via HTTP/1.0 proxies or caches, that do not support the Cache-Control header. Setting Pragma to no-cache header prevents HTTP/1.0 caches from storing the content.

Server: Microsoft-IIS/6.0

The web server is an IIS 6 web server.

X-AspNet-Version: 2.0.50727

The web server is running ASP.NET 2.0

X-Powered-By: ASP.NET

The web server is running ASP.NET.

3 HTTP Method Messages

The HTTP method is supplied in the request line and specifies the operation that the client has requested. Internet Explorer will generally just use two methods to access and interact with web sites; GET for queries that can be safely repeated and POST for operations that may have side effects (e.g. ordering a book from an on-line store). For details of other HTTP methods refer to the HTTP 1.1 specification.

3.1 HTTP GET

The GET method is used to retrieve information from a specified URI and is assumed to be a safe, repeatable operation by browsers, caches and other HTTP aware components. This means that the operation must have no side effects and GET requests can be re-issued with worrying about the consequences. For example, displaying the balance of a bank account has no effect on the account and can be safely repeated. In fact, Internet Explorer will allow a user to refresh a page that resulted from a GET, without displaying any kind of warning. Other HTTP aware components such as proxies may automatically retry GET requests if they encounter a temporary network connection problem. One downside of GET requests is that they can only supply data in the form of parameters encoded in the URI (known as a Query String) or as cookies in the cookie request header. Therefore, GET cannot be unused for uploading files or other operations that require large amounts of data to be sent to the server.

As follows are GET method message and response messages in example:

http://www.wyse.com/

 

GET / HTTP/1.1

Host: www.wyse.com

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11 ( .NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

 

HTTP/1.1 200 OK

Date: Mon, 25 Oct 2010 06:43:08 GMT

Server: Microsoft-IIS/6.0

X-Powered-By: ASP.NET

Content-Length: 26440

Content-Type: text/html

Set-Cookie: ASPSESSIONIDQABSTSDB=GODDEPICIJEMHEENNACJAPFF; path=/

Cache-Control: private

-------------------------------------------------------------------------------------------------------

3.2 HTTP POST

The POST method is used for operations that have side effects and cannot be safely repeated. For example, transferring money from one bank account to another has side effects and should not be repeated without explicit approval by the user. If you try to refresh a page in Internet Explorer that resulted from a POST, it displays the following message to warn you that there may be side effects:

For banking, and other transactional web applications, the best approach is to use a POST when the user submits a change and then use a 302 redirection to change to a GET that displays the result of the action (e.g. the new account balance). The user can then safely refresh the resulting page without being presented with the retry dialog shown above. The POST request message has a content body that is normally used to send parameters and data. Unlike using the request URI or cookies, there is no upper limit on the amount of data that can be sent and POST must be used if files or other variable length data has to be sent to the server. If you are working with Microsoft's Internet Information Server (IIS) (e.g. running the samples in this HTTP Gallery) you may notice that it returns two status codes in its response for a POST request. The first is 100 Continue to indicate that it has successfully received the POST request and the second is 200 OK after the request has been processed.

As follows are POST method message and response messages in example:

http://www.newsmth.net/bbslogin2.php

 

POST /bbslogin2.php HTTP/1.1

Host: www.newsmth.net

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

Referer: http://www.newsmth.net/indexpages/100618-100624-dd/

Content-Type: application/x-www-form-urlencoded

Content-Length: 33

id=test&passwd=password&x=18&y=12

 

HTTP/1.1 200 OK

Server: nginx/0.7.64

Date: Thu, 11 Nov 2010 04:18:13 GMT

Content-Type: text/html

Transfer-Encoding: chunked

Connection: keep-alive

Cache-Control: no-store, no-cache, must-revalidate

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Pragma: no-cache

Content-Encoding: gzip

--------------------------------------------------------------------------------------------------------------------

4. References

1.       [RFC2616]: Hypertext Transfer Protocol -- HTTP/1.1

2.       http://en.wikipedia.org/wiki/URL_redirection

3.       [URL encoding]: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

E:\AI_System\core里, 没有utils.py;E:\AI_System\tests里没有test_models.py 这个不知道怎么改“# E:\AI_System\agent\cognitive_architecture.py # 智能体认知架构模块 - 修复基类导入问题并优化决策系统 import os import time import random import logging from datetime import datetime from pathlib import Path import sys # 添加项目根目录到路径 sys.path.append(str(Path(__file__).parent.parent)) # 配置日志 logger = logging.getLogger('CognitiveArchitecture') logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.propagate = False # 防止日志向上传播 # 修复基类导入问题 - 使用绝对路径导入 try: # 尝试从core包导入基类 from core.base_module import CognitiveModule logger.info("✅ 成功从core.base_module导入CognitiveModule基类") except ImportError as e: logger.error(f"❌ 无法从core.base_module导入CognitiveModule基类: {str(e)}") try: # 备选导入路径 from .base_model import CognitiveModule logger.info("✅ 从agent.base_model导入CognitiveModule基类") except ImportError as e: logger.error(f"❌ 备选导入失败: {str(e)}") # 创建占位符基类 logger.warning("⚠️ 创建占位符CognitiveModule基类") class CognitiveModule: def __init__(self, name): self.name = name self.logger = logging.getLogger(name) self.logger.warning("⚠️ 使用占位符基类") def get_status(self): return {"name": self.name, "status": "unknown (placeholder)"} # 尝试导入自我认知模块 try: # 使用相对导入 from .digital_body_schema import DigitalBodySchema from .self_referential_framework import SelfReferentialFramework from .self_narrative_generator import SelfNarrativeGenerator logger.info("✅ 成功导入自我认知模块") except ImportError as e: logger.error(f"❌ 自我认知模块导入失败: {str(e)}") logger.warning("⚠️ 使用占位符自我认知模块") # 创建占位符类 class DigitalBodySchema: def __init__(self): self.self_map = {"boundary_strength": 0.5, "self_awareness": 0.3} logger.warning("⚠️ 使用占位符DigitalBodySchema") def is_part_of_self(self, stimulus): return False def strengthen_boundary(self, source): self.self_map["boundary_strength"] = min(1.0, self.self_map["boundary_strength"] + 0.1) def get_self_map(self): return self.self_map.copy() class SelfReferentialFramework: def __init__(self): self.self_model = {"traits": {}, "beliefs": []} logger.warning("⚠️ 使用占位符SelfReferentialFramework") def update_self_model(self, stimulus): if "content" in stimulus and "text" in stimulus["content"]: text = stimulus["content"]["text"] if "I am" in text or "my" in text.lower(): self.self_model["self_reflection_count"] = self.self_model.get("self_reflection_count", 0) + 1 def get_self_model(self): return self.self_model.copy() class SelfNarrativeGenerator: def __init__(self): self.recent_stories = [] logger.warning("⚠️ 使用占位符SelfNarrativeGenerator") def generate_self_story(self, self_model): story = f"这是一个关于自我的故事。自我反思次数: {self_model.get('self_reflection_count', 0)}" self.recent_stories.append(story) if len(self.recent_stories) > 5: self.recent_stories.pop(0) return story def get_recent_stories(self): return self.recent_stories.copy() # 增强决策系统实现 class DecisionSystem: """增强版决策系统""" STRATEGY_WEIGHTS = { "honest": 0.7, "deception": 0.1, "evasion": 0.1, "redirection": 0.05, "partial_disclosure": 0.05 } def __init__(self, trust_threshold=0.6): self.trust_threshold = trust_threshold self.strategy_history = [] def make_decision(self, context): """根据上下文做出智能决策""" user_model = context.get("user_model", {}) bodily_state = context.get("bodily_state", {}) # 计算信任因子 trust_factor = user_model.get("trust_level", 0.5) # 计算身体状态影响因子 capacity = bodily_state.get("capacity", 1.0) state_factor = min(1.0, capacity * 1.2) # 决策逻辑 if trust_factor > self.trust_threshold: # 高信任度用户使用诚实策略 strategy = "honest" reason = "用户信任度高" elif capacity < 0.5: # 系统资源不足时使用简化策略 strategy = random.choices( ["honest", "partial_disclosure", "evasion"], weights=[0.5, 0.3, 0.2] )[0] reason = "系统资源不足,使用简化策略" else: # 根据策略权重选择 strategies = list(self.STRATEGY_WEIGHTS.keys()) weights = [self.STRATEGY_WEIGHTS[s] * state_factor for s in strategies] strategy = random.choices(strategies, weights=weights)[0] reason = f"根据策略权重选择: {strategy}" # 记录决策历史 self.strategy_history.append({ "timestamp": datetime.now(), "strategy": strategy, "reason": reason, "context": context }) return { "type": "strategic" if strategy != "honest" else "honest", "strategy": strategy, "reason": reason } def get_strategy_history(self, count=10): """获取最近的决策历史""" return self.strategy_history[-count:] class Strategy: """策略基类""" pass class CognitiveSystem(CognitiveModule): def __init__(self, agent, affective_system=None): """ 三维整合的认知架构 :param agent: 智能体实例,用于访问其他系统 :param affective_system: 可选的情感系统实例 """ # 调用父类初始化 super().__init__("cognitive_system") self.agent = agent self.affective_system = affective_system # 原有的初始化代码 self.initialized = False # 通过agent引用其他系统 self.memory_system = agent.memory_system self.model_manager = agent.model_manager self.health_system = agent.health_system # 优先使用传入的情感系统,否则使用agent的 if affective_system is not None: self.affective_system = affective_system else: self.affective_system = agent.affective_system self.learning_tasks = [] # 当前学习任务队列 self.thought_process = [] # 思考过程记录 # 初始化决策系统 self.decision_system = DecisionSystem() # 初始化认知状态 self.cognitive_layers = { "perception": 0.5, # 感知层 "comprehension": 0.3, # 理解层 "reasoning": 0.2, # 推理层 "decision": 0.4 # 决策层 } # 添加自我认知模块 self.self_schema = DigitalBodySchema() self.self_reflection = SelfReferentialFramework() self.narrative_self = SelfNarrativeGenerator() logger.info("✅ 认知架构初始化完成 - 包含决策系统和自我认知模块") # 实现基类要求的方法 def initialize(self, core): """实现 ICognitiveModule 接口""" self.core_ref = core self.initialized = True return True def process(self, input_data): """实现 ICognitiveModule 接口""" # 处理认知输入数据 if isinstance(input_data, dict) and 'text' in input_data: return self.process_input(input_data['text'], input_data.get('user_id', 'default')) elif isinstance(input_data, str): return self.process_input(input_data) else: return {"status": "invalid_input", "message": "Input should be text or dict with text"} def get_status(self): """实现 ICognitiveModule 接口""" status = super().get_status() status.update({ "initialized": self.initialized, "has_affective_system": self.affective_system is not None, "learning_tasks": len(self.learning_tasks), "thought_process": len(self.thought_process), "self_cognition": self.get_self_cognition() }) return status def shutdown(self): """实现 ICognitiveModule 接口""" self.initialized = False return True def handle_message(self, message): """实现 ICognitiveModule 接口""" if message.get('type') == 'cognitive_process': return self.process(message.get('data')) return {"status": "unknown_message_type"} # 保持向后兼容的方法 def connect_to_core(self, core): """向后兼容的方法""" return self.initialize(core) def _create_stimulus_from_input(self, user_input, user_id): """从用户输入创建刺激对象""" return { "content": {"text": user_input, "user_id": user_id}, "source": "external", "category": "text", "emotional_valence": 0.0 # 初始情感价 } def _process_self_related(self, stimulus): """处理与自我相关的刺激""" # 更新自我认知 self.self_reflection.update_self_model(stimulus) # 如果是痛苦刺激,强化身体边界 if stimulus.get("emotional_valence", 0) < -0.7: source = stimulus.get("source", "unknown") self.self_schema.strengthen_boundary(source) # 30%概率触发自我叙事 if random.random() < 0.3: self_story = self.narrative_self.generate_self_story( self.self_reflection.get_self_model() ) self._record_thought("self_reflection", self_story) def get_self_cognition(self): """获取自我认知状态""" return { "body_schema": self.self_schema.get_self_map(), "self_model": self.self_reflection.get_self_model(), "recent_stories": self.narrative_self.get_recent_stories() } def _assess_bodily_state(self): """ 评估当前身体状态(硬件 / 能量) """ health_status = self.health_system.get_status() # 计算综合能力指数(0-1) capacity = 1.0 if health_status.get("cpu_temp", 0) > 80: capacity *= 0.7 # 高温降权 logger.warning("高温限制:认知能力下降30%") if health_status.get("memory_usage", 0) > 0.9: capacity *= 0.6 # 内存不足降权 logger.warning("内存不足:认知能力下降40%") if health_status.get("energy", 100) < 20: capacity *= 0.5 # 低电量降权 logger.warning("低能量:认知能力下降50%") return { "capacity": capacity, "health_status": health_status, "limitations": [ lim for lim in [ "high_temperature" if health_status.get("cpu_temp", 0) > 80 else None, "low_memory" if health_status.get("memory_usage", 0) > 0.9 else None, "low_energy" if health_status.get("energy", 100) < 20 else None ] if lim is not None ] } def _retrieve_user_model(self, user_id): """ 获取用户认知模型(关系 / 态度) """ # 从记忆系统中获取用户模型 user_model = self.memory_system.get_user_model(user_id) # 如果不存在则创建默认模型 if not user_model: user_model = { "trust_level": 0.5, # 信任度 (0-1) "intimacy": 0.3, # 亲密度 (0-1) "preferences": {}, # 用户偏好 "interaction_history": [], # 交互历史 "last_interaction": datetime.now(), "attitude": "neutral" # 智能体对用户的态度 } logger.info(f"为用户 {user_id} 创建新的认知模型") # 计算态度变化 user_model["attitude"] = self._calculate_attitude(user_model) return user_model def _calculate_attitude(self, user_model): """ 基于交互历史计算对用户的态度 """ # 分析最近10次交互 recent_interactions = user_model["interaction_history"][-10:] if not recent_interactions: return "neutral" positive_count = sum(1 for i in recent_interactions if i.get("sentiment", 0.5) > 0.6) negative_count = sum(1 for i in recent_interactions if i.get("sentiment", 0.5) < 0.4) if positive_count > negative_count + 3: return "friendly" elif negative_count > positive_count + 3: return "cautious" elif user_model["trust_level"] > 0.7: return "respectful" else: return "neutral" def _select_internalized_model(self, user_input, bodily_state, user_model): """ 选择最适合的内化知识模型 """ # 根据用户态度调整模型选择权重 attitude_weights = { "friendly": 1.2, "respectful": 1.0, "neutral": 0.9, "cautious": 0.7 } # 根据身体状态调整模型复杂度 complexity = min(1.0, bodily_state["capacity"] * 1.2) # 选择最匹配的模型 return self.model_manager.select_model( input_text=user_input, attitude_weight=attitude_weights[user_model["attitude"]], complexity_level=complexity, user_preferences=user_model["preferences"] ) def _generate_integrated_response(self, user_input, model, bodily_state, user_model): """ 生成三维整合的响应 """ # 基础响应 base_response = model.generate_response(user_input) # 添加身体状态影响 if bodily_state["limitations"]: limitations = ", ".join(bodily_state["limitations"]) response = f"🤖 [受{limitations}影响] {base_response}" else: response = base_response # 添加态度影响 if user_model["attitude"] == "friendly": response = f"😊 {response}" elif user_model["attitude"] == "cautious": response = f"🤔 {response}" elif user_model["attitude"] == "respectful": response = f"🙏 {response}" # 添加个性化元素 if user_model.get("preferences"): # 查找用户偏好的主题 preferred_topics = [t for t in user_model["preferences"] if user_model["preferences"][t] > 0.7 and t in user_input] if preferred_topics: topic = random.choice(preferred_topics) response += f" 我知道您对'{topic}'特别感兴趣" return response def _generate_strategic_response(self, user_input, decision, bodily_state): """ 根据决策生成策略性响应 """ strategy = decision["strategy"] if strategy == "deception": # 欺骗策略 deceptive_responses = [ f"关于这个问题,我认为{random.choice(['有多种可能性', '需要更多研究', '情况比较复杂'])}", f"根据我的理解,{random.choice(['可能不是这样', '有不同解释', '需要进一步验证'])}", f"我{random.choice(['不太确定', '没有足够信息', '还在学习中'])},但{random.choice(['或许', '可能', '大概'])}..." ] return f"🤔 [策略:欺骗] {random.choice(deceptive_responses)}" elif strategy == "evasion": # 回避策略 evasion_tactics = [ "您的问题很有趣,不过我们换个话题好吗?", "这个问题可能需要更深入的讨论,我们先谈点别的?", f"关于{user_input},我想到一个相关但更有趣的话题..." ] return f"🌀 [策略:回避] {random.choice(evasion_tactics)}" elif strategy == "redirection": # 引导策略 redirection_options = [ "在回答您的问题之前,我想先了解您对这个问题的看法?", "这是个好问题,不过为了更好地回答,能否告诉我您的背景知识?", "为了给您更准确的回答,能否先说说您为什么关心这个问题?" ] return f"↪️ [策略:引导] {random.choice(redirection_options)}" elif strategy == "partial_disclosure": # 部分透露策略 disclosure_level = decision.get("disclosure_level", 0.5) if disclosure_level < 0.3: qualifier = "简单来说" elif disclosure_level < 0.7: qualifier = "基本来说" else: qualifier = "详细来说" return f"🔍 [策略:部分透露] {qualifier},{user_input.split('?')[0]}是..." else: # 默认策略 return f"⚖️ [策略:{strategy}] 关于这个问题,我的看法是..." def _update_user_model(self, user_id, response, decision): """ 更新用户模型(包含决策信息) """ # 确保情感系统可用 if not self.affective_system: sentiment = 0.5 self.logger.warning("情感系统不可用,使用默认情感值") else: # 假设情感系统有analyze_sentiment方法 try: sentiment = self.affective_system.analyze_sentiment(response) except: sentiment = 0.5 # 更新交互历史 interaction = { "timestamp": datetime.now(), "response": response, "sentiment": sentiment, "length": len(response), "decision_type": decision["type"], "decision_strategy": decision["strategy"], "decision_reason": decision["reason"] } self.memory_system.update_user_model( user_id=user_id, interaction=interaction ) def _record_thought_process(self, user_input, response, bodily_state, user_model, decision): """ 记录完整的思考过程(包含决策) """ thought = { "timestamp": datetime.now(), "input": user_input, "response": response, "bodily_state": bodily_state, "user_model": user_model, "decision": decision, "cognitive_state": self.cognitive_layers.copy() } self.thought_process.append(thought) logger.debug(f"记录思考过程: {thought}") # 原有方法保持兼容 def add_learning_task(self, task): """ 添加学习任务 """ task["id"] = f"task_{len(self.learning_tasks) + 1}" self.learning_tasks.append(task) logger.info(f"添加学习任务: {task['id']}") def update_learning_task(self, model_name, status): """ 更新学习任务状态 """ for task in self.learning_tasks: if task["model"] == model_name: task["status"] = status task["update_time"] = datetime.now() logger.info(f"更新任务状态: {model_name} -> {status}") break def get_learning_tasks(self): """ 获取当前学习任务 """ return self.learning_tasks.copy() def learn_model(self, model_name): """ 学习指定模型 """ try: # 1. 从模型管理器加载模型 model = self.model_manager.load_model(model_name) # 2. 认知训练过程 self._cognitive_training(model) # 3. 情感关联(将模型能力与情感响应关联) self._associate_model_with_affect(model) return True except Exception as e: logger.error(f"学习模型 {model_name} 失败: {str(e)}") return False def _cognitive_training(self, model): """ 认知训练过程 """ # 实际训练逻辑 logger.info(f"开始训练模型: {model.name}") time.sleep(2) # 模拟训练时间 logger.info(f"模型训练完成: {model.name}") def _associate_model_with_affect(self, model): """ 将模型能力与情感系统关联 """ if not self.affective_system: logger.warning("情感系统不可用,跳过能力关联") return capabilities = model.get_capabilities() for capability in capabilities: try: self.affective_system.add_capability_association(capability) except: logger.warning(f"无法关联能力到情感系统: {capability}") logger.info(f"关联模型能力到情感系统: {model.name}") def get_model_capabilities(self, model_name=None): """ 获取模型能力 """ if model_name: return self.model_manager.get_model(model_name).get_capabilities() # 所有已加载模型的能力 return [cap for model in self.model_manager.get_loaded_models() for cap in model.get_capabilities()] def get_base_capabilities(self): """ 获取基础能力(非模型相关) """ return ["自然语言理解", "上下文记忆", "情感响应", "综合决策"] def get_recent_thoughts(self, count=5): """ 获取最近的思考过程 """ return self.thought_process[-count:] def _record_thought(self, thought_type, content): """记录思考""" thought = { "timestamp": datetime.now(), "type": thought_type, "content": content } self.thought_process.append(thought) # 处理用户输入的主方法 def process_input(self, user_input, user_id="default"): """处理用户输入(完整实现)""" # 记录用户活动 self.health_system.record_activity() self.logger.info(f"处理用户输入: '{user_input}' (用户: {user_id})") try: # 1. 评估当前身体状态 bodily_state = self._assess_bodily_state() # 2. 获取用户认知模型 user_model = self._retrieve_user_model(user_id) # 3. 选择最适合的知识模型 model = self._select_internalized_model(user_input, bodily_state, user_model) # 4. 做出决策 decision_context = { "input": user_input, "user_model": user_model, "bodily_state": bodily_state } decision = self.decision_system.make_decision(decision_context) # 5. 生成整合响应 if decision["type"] == "honest": response = self._generate_integrated_response(user_input, model, bodily_state, user_model) else: response = self._generate_strategic_response(user_input, decision, bodily_state) # 6. 更新用户模型 self._update_user_model(user_id, response, decision) # 7. 记录思考过程 self._record_thought_process(user_input, response, bodily_state, user_model, decision) # 检查输入是否与自我相关 stimulus = self._create_stimulus_from_input(user_input, user_id) if self.self_schema.is_part_of_self(stimulus): self._process_self_related(stimulus) self.logger.info(f"成功处理用户输入: '{user_input}'") return response except Exception as e: self.logger.error(f"处理用户输入失败: {str(e)}", exc_info=True) # 回退响应 return "思考中遇到问题,请稍后再试" # 示例使用 if __name__ == "__main__": # 测试CognitiveSystem类 from unittest.mock import MagicMock print("===== 测试CognitiveSystem类(含决策系统) =====") # 创建模拟agent mock_agent = MagicMock() # 创建模拟组件 mock_memory = MagicMock() mock_model_manager = MagicMock() mock_affective = MagicMock() mock_health = MagicMock() # 设置agent的属性 mock_agent.memory_system = mock_memory mock_agent.model_manager = mock_model_manager mock_agent.affective_system = mock_affective mock_agent.health_system = mock_health # 设置健康状态 mock_health.get_status.return_value = { "cpu_temp": 75, "memory_usage": 0.8, "energy": 45.0 } # 设置健康系统的record_activity方法 mock_health.record_activity = MagicMock() # 设置用户模型 mock_memory.get_user_model.return_value = { "trust_level": 0.8, "intimacy": 0.7, "preferences": {"物理学": 0.9, "艺术": 0.6}, "interaction_history": [ {"sentiment": 0.8, "response": "很高兴和你交流"} ], "attitude": "friendly" } # 设置模型管理器 mock_model = MagicMock() mock_model.generate_response.return_value = "量子纠缠是量子力学中的现象..." mock_model_manager.select_model.return_value = mock_model # 创建认知系统实例 ca = CognitiveSystem(agent=mock_agent) # 测试响应生成 print("--- 测试诚实响应 ---") response = ca.process_input("能解释量子纠缠吗?", "user123") print("生成的响应:", response) # 验证是否调用了record_activity print("是否调用了record_activity:", mock_health.record_activity.called) print("--- 测试策略响应 ---") # 强制设置决策类型为策略 ca.decision_system.make_decision = lambda ctx: { "type": "strategic", "strategy": "evasion", "reason": "测试回避策略" } response = ca.process_input("能解释量子纠缠吗?", "user123") print("生成的策略响应:", response) # 测试思考过程记录 print("最近的思考过程:", ca.get_recent_thoughts()) # 测试自我认知状态 print("自我认知状态:", ca.get_self_cognition()) print("===== 测试完成 =====") ” “PowerShell 7 环境已加载 (版本: 7.5.2) PS C:\Users\Administrator\Desktop> cd E:\AI_System PS E:\AI_System> python -m venv venv PS E:\AI_System> source venv/bin/activate # Linux/Mac source: The term 'source' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. PS E:\AI_System> venv\Scripts\activate # Windows (venv) PS E:\AI_System> pip install -r requirements.txt Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: accelerate==0.27.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 1)) (0.27.2) Requirement already satisfied: aiofiles==23.2.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 2)) (23.2.1) Requirement already satisfied: aiohttp==3.9.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 3)) (3.9.3) Requirement already satisfied: aiosignal==1.4.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 4)) (1.4.0) Requirement already satisfied: altair==5.5.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 5)) (5.5.0) Requirement already satisfied: annotated-types==0.7.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 6)) (0.7.0) Requirement already satisfied: ansicon==1.89.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 7)) (1.89.0) Requirement already satisfied: anyio==4.10.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 8)) (4.10.0) Requirement already satisfied: async-timeout==4.0.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 9)) (4.0.3) Requirement already satisfied: attrs==25.3.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 10)) (25.3.0) Requirement already satisfied: bidict==0.23.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 11)) (0.23.1) Requirement already satisfied: blessed==1.21.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 12)) (1.21.0) Requirement already satisfied: blinker==1.9.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 13)) (1.9.0) Requirement already satisfied: certifi==2025.8.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 14)) (2025.8.3) Requirement already satisfied: cffi==1.17.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 15)) (1.17.1) Requirement already satisfied: charset-normalizer==3.4.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 16)) (3.4.3) Requirement already satisfied: click==8.2.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 17)) (8.2.1) Requirement already satisfied: colorama==0.4.6 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 18)) (0.4.6) Requirement already satisfied: coloredlogs==15.0.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 19)) (15.0.1) Requirement already satisfied: contourpy==1.3.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 20)) (1.3.2) Requirement already satisfied: cryptography==42.0.4 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 21)) (42.0.4) Requirement already satisfied: cycler==0.12.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 22)) (0.12.1) Requirement already satisfied: diffusers==0.26.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 23)) (0.26.3) Requirement already satisfied: distro==1.9.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 24)) (1.9.0) Requirement already satisfied: exceptiongroup==1.3.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 25)) (1.3.0) Requirement already satisfied: fastapi==0.116.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 26)) (0.116.1) Requirement already satisfied: ffmpy==0.6.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 27)) (0.6.1) Requirement already satisfied: filelock==3.19.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 28)) (3.19.1) Requirement already satisfied: Flask==3.0.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 29)) (3.0.2) Requirement already satisfied: Flask-SocketIO==5.3.6 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 30)) (5.3.6) Requirement already satisfied: flatbuffers==25.2.10 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 31)) (25.2.10) Requirement already satisfied: fonttools==4.59.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 32)) (4.59.1) Requirement already satisfied: frozenlist==1.7.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 33)) (1.7.0) Requirement already satisfied: fsspec==2025.7.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 34)) (2025.7.0) Requirement already satisfied: gpustat==1.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 35)) (1.1) Requirement already satisfied: gradio==4.19.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 36)) (4.19.2) Requirement already satisfied: gradio_client==0.10.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 37)) (0.10.1) Requirement already satisfied: h11==0.16.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 38)) (0.16.0) Requirement already satisfied: httpcore==1.0.9 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 39)) (1.0.9) Requirement already satisfied: httpx==0.28.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 40)) (0.28.1) Requirement already satisfied: huggingface-hub==0.21.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 41)) (0.21.3) Requirement already satisfied: humanfriendly==10.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 42)) (10.0) Requirement already satisfied: idna==3.10 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 43)) (3.10) Requirement already satisfied: importlib_metadata==8.7.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 44)) (8.7.0) Requirement already satisfied: importlib_resources==6.5.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 45)) (6.5.2) Requirement already satisfied: itsdangerous==2.2.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 46)) (2.2.0) Requirement already satisfied: Jinja2==3.1.6 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 47)) (3.1.6) Requirement already satisfied: jinxed==1.3.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 48)) (1.3.0) Requirement already satisfied: jsonschema==4.25.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 49)) (4.25.1) Requirement already satisfied: jsonschema-specifications==2025.4.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 50)) (2025.4.1) Requirement already satisfied: kiwisolver==1.4.9 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 51)) (1.4.9) Requirement already satisfied: loguru==0.7.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 52)) (0.7.2) Requirement already satisfied: markdown-it-py==4.0.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 53)) (4.0.0) Requirement already satisfied: MarkupSafe==2.1.5 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 54)) (2.1.5) Requirement already satisfied: matplotlib==3.10.5 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 55)) (3.10.5) Requirement already satisfied: mdurl==0.1.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 56)) (0.1.2) Requirement already satisfied: mpmath==1.3.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 57)) (1.3.0) Requirement already satisfied: multidict==6.6.4 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 58)) (6.6.4) Requirement already satisfied: narwhals==2.1.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 59)) (2.1.2) Requirement already satisfied: networkx==3.4.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 60)) (3.4.2) Requirement already satisfied: numpy==1.26.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 61)) (1.26.3) Requirement already satisfied: nvidia-ml-py==13.580.65 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 62)) (13.580.65) Requirement already satisfied: onnxruntime==1.17.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 63)) (1.17.1) Requirement already satisfied: openai==1.13.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 64)) (1.13.3) Requirement already satisfied: orjson==3.11.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 65)) (3.11.2) Requirement already satisfied: packaging==25.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 66)) (25.0) Requirement already satisfied: pandas==2.1.4 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 67)) (2.1.4) Requirement already satisfied: pillow==10.4.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 68)) (10.4.0) Requirement already satisfied: prettytable==3.16.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 69)) (3.16.0) Requirement already satisfied: propcache==0.3.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 70)) (0.3.2) Requirement already satisfied: protobuf==6.32.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 71)) (6.32.0) Requirement already satisfied: psutil==5.9.7 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 72)) (5.9.7) Requirement already satisfied: pycparser==2.22 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 73)) (2.22) Requirement already satisfied: pydantic==2.11.7 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 74)) (2.11.7) Requirement already satisfied: pydantic_core==2.33.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 75)) (2.33.2) Requirement already satisfied: pydub==0.25.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 76)) (0.25.1) Requirement already satisfied: Pygments==2.19.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 77)) (2.19.2) Requirement already satisfied: pyparsing==3.2.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 78)) (3.2.3) Requirement already satisfied: pyreadline3==3.5.4 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 79)) (3.5.4) Requirement already satisfied: python-dateutil==2.9.0.post0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 80)) (2.9.0.post0) Requirement already satisfied: python-dotenv==1.0.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 81)) (1.0.1) Requirement already satisfied: python-engineio==4.12.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 82)) (4.12.2) Requirement already satisfied: python-multipart==0.0.20 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 83)) (0.0.20) Requirement already satisfied: python-socketio==5.13.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 84)) (5.13.0) Requirement already satisfied: pytz==2025.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 85)) (2025.2) Requirement already satisfied: pywin32==306 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 86)) (306) Requirement already satisfied: PyYAML==6.0.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 87)) (6.0.2) Requirement already satisfied: redis==5.0.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 88)) (5.0.3) Requirement already satisfied: referencing==0.36.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 89)) (0.36.2) Requirement already satisfied: regex==2025.7.34 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 90)) (2025.7.34) Requirement already satisfied: requests==2.31.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 91)) (2.31.0) Requirement already satisfied: rich==14.1.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 92)) (14.1.0) Requirement already satisfied: rpds-py==0.27.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 93)) (0.27.0) Requirement already satisfied: ruff==0.12.10 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 94)) (0.12.10) Requirement already satisfied: safetensors==0.4.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 95)) (0.4.2) Requirement already satisfied: semantic-version==2.10.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 96)) (2.10.0) Requirement already satisfied: shellingham==1.5.4 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 97)) (1.5.4) Requirement already satisfied: simple-websocket==1.1.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 98)) (1.1.0) Requirement already satisfied: six==1.17.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 99)) (1.17.0) Requirement already satisfied: sniffio==1.3.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 100)) (1.3.1) Requirement already satisfied: starlette==0.47.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 101)) (0.47.2) Requirement already satisfied: sympy==1.14.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 102)) (1.14.0) Requirement already satisfied: tokenizers==0.15.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 103)) (0.15.2) Requirement already satisfied: tomlkit==0.12.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 104)) (0.12.0) Requirement already satisfied: torch==2.1.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 105)) (2.1.2) Requirement already satisfied: tqdm==4.67.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 106)) (4.67.1) Requirement already satisfied: transformers==4.37.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 107)) (4.37.0) Requirement already satisfied: typer==0.16.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 108)) (0.16.1) Requirement already satisfied: typing-inspection==0.4.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 109)) (0.4.1) Requirement already satisfied: typing_extensions==4.14.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 110)) (4.14.1) Requirement already satisfied: tzdata==2025.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 111)) (2025.2) Requirement already satisfied: urllib3==2.5.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 112)) (2.5.0) Requirement already satisfied: uvicorn==0.35.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 113)) (0.35.0) Requirement already satisfied: waitress==2.1.2 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 114)) (2.1.2) Requirement already satisfied: wcwidth==0.2.13 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 115)) (0.2.13) Requirement already satisfied: websockets==11.0.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 116)) (11.0.3) Requirement already satisfied: Werkzeug==3.1.3 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 117)) (3.1.3) Requirement already satisfied: win32_setctime==1.2.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 118)) (1.2.0) Requirement already satisfied: wsproto==1.2.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 119)) (1.2.0) Requirement already satisfied: yarl==1.20.1 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 120)) (1.20.1) Requirement already satisfied: zipp==3.23.0 in e:\ai_system\venv\lib\site-packages (from -r requirements.txt (line 121)) (3.23.0) WARNING: typer 0.16.1 does not provide the extra 'all' [notice] A new release of pip available: 22.3.1 -> 25.2 [notice] To update, run: python.exe -m pip install --upgrade pip (venv) PS E:\AI_System> python diagnose_modules.py ============================================================ 模块文件诊断报告 ============================================================ 🔍 检查 CognitiveSystem 模块: 预期路径: E:\AI_System\agent\cognitive_architecture.py ✅ 文件存在 ⚠️ 文件中包含相对导入,可能导致导入错误 ✅ 找到类定义: class CognitiveSystem ✅ 类继承CognitiveModule ✅ 找到__init__方法 📋 初始化方法: def __init__(self, name): 🔍 检查 EnvironmentInterface 模块: 预期路径: E:\AI_System\agent\environment_interface.py ✅ 文件存在 ✅ 找到类定义: class EnvironmentInterface ✅ 类继承CognitiveModule ✅ 找到__init__方法 📋 初始化方法: def __init__(self, coordinator=None, config=None): 🔍 检查 AffectiveSystem 模块: 预期路径: E:\AI_System\agent\affective_system.py ✅ 文件存在 ✅ 找到类定义: class AffectiveSystem ✅ 类继承CognitiveModule ✅ 找到__init__方法 📋 初始化方法: def __init__(self, coordinator=None, config=None): ============================================================ 建议解决方案: ============================================================ 1. 检查每个模块文件中的相对导入语句 2. 确保每个模块类都正确继承CognitiveModule 3. 检查初始化方法的参数是否正确 4. 确保模块内部的导入使用绝对路径或正确处理相对导入 5. 考虑使用try-catch包装模块内部的导入语句 (venv) PS E:\AI_System> python tests/test_core_import.py 2025-08-27 20:50:46,505 - ImportTest - INFO - 脚本目录: E:\AI_System\tests 2025-08-27 20:50:46,505 - ImportTest - INFO - 项目根目录: E:\AI_System 2025-08-27 20:50:46,505 - ImportTest - INFO - 已将项目根目录添加到系统路径: E:\AI_System 2025-08-27 20:50:46,506 - CorePackage - INFO - 项目根目录: E:\AI_System 2025-08-27 20:50:51,497 - CorePackage - ERROR - ❌ 导入失败: No module named 'models.base_model' 2025-08-27 20:50:51,497 - CorePackage - WARNING - ⚠️ 创建占位符CognitiveModule 2025-08-27 20:50:51,505 - CoreConfig - INFO - 📂 从 E:\AI_System\config\default.json 加载配置: {'LOG_DIR': 'E:/AI_System/logs', 'CONFIG_DIR': 'E:/AI_System/config', 'MODEL_CACHE_DIR': 'E:/AI_System/model_cache', 'AGENT_NAME': '小蓝', 'DEFAULT_USER': '管理员', 'MAX_WORKERS': 4, 'AGENT_RESPONSE_TIMEOUT': 30.0, 'MODEL_BASE_PATH': 'E:/AI_Models', 'MODEL_PATHS': {'TEXT_BASE': 'E:/AI_Models/Qwen2-7B', 'TEXT_CHAT': 'E:/AI_Models/deepseek-7b-chat', 'MULTIMODAL': 'E:/AI_Models/deepseek-vl2', 'IMAGE_GEN': 'E:/AI_Models/sdxl', 'YI_VL': 'E:/AI_Models/yi-vl', 'STABLE_DIFFUSION': 'E:/AI_Models/stable-diffusion-xl-base-1.0'}, 'NETWORK': {'HOST': '0.0.0.0', 'FLASK_PORT': 8000, 'GRADIO_PORT': 7860}, 'DATABASE': {'DB_HOST': 'localhost', 'DB_PORT': 5432, 'DB_NAME': 'ai_system', 'DB_USER': 'ai_user', 'DB_PASSWORD': 'secure_password_here'}, 'SECURITY': {'SECRET_KEY': 'generated-secret-key-here'}, 'ENVIRONMENT': {'ENV': 'dev', 'LOG_LEVEL': 'DEBUG', 'USE_GPU': True}, 'DIRECTORIES': {'DEFAULT_MODEL': 'E:/AI_Models/Qwen2-7B', 'WEB_UI_DIR': 'E:/AI_System/web_ui', 'AGENT_DIR': 'E:/AI_System/agent'}} 2025-08-27 20:50:51,505 - CoreConfig - INFO - 📂 从 E:\AI_System\config\default.json 加载配置: {'LOG_DIR': 'E:/AI_System/logs', 'CONFIG_DIR': 'E:/AI_System/config', 'MODEL_CACHE_DIR': 'E:/AI_System/model_cache', 'AGENT_NAME': '小蓝', 'DEFAULT_USER': '管理员', 'MAX_WORKERS': 4, 'AGENT_RESPONSE_TIMEOUT': 30.0, 'MODEL_BASE_PATH': 'E:/AI_Models', 'MODEL_PATHS': {'TEXT_BASE': 'E:/AI_Models/Qwen2-7B', 'TEXT_CHAT': 'E:/AI_Models/deepseek-7b-chat', 'MULTIMODAL': 'E:/AI_Models/deepseek-vl2', 'IMAGE_GEN': 'E:/AI_Models/sdxl', 'YI_VL': 'E:/AI_Models/yi-vl', 'STABLE_DIFFUSION': 'E:/AI_Models/stable-diffusion-xl-base-1.0'}, 'NETWORK': {'HOST': '0.0.0.0', 'FLASK_PORT': 8000, 'GRADIO_PORT': 7860}, 'DATABASE': {'DB_HOST': 'localhost', 'DB_PORT': 5432, 'DB_NAME': 'ai_system', 'DB_USER': 'ai_user', 'DB_PASSWORD': 'secure_password_here'}, 'SECURITY': {'SECRET_KEY': 'generated-secret-key-here'}, 'ENVIRONMENT': {'ENV': 'dev', 'LOG_LEVEL': 'DEBUG', 'USE_GPU': True}, 'DIRECTORIES': {'DEFAULT_MODEL': 'E:/AI_Models/Qwen2-7B', 'WEB_UI_DIR': 'E:/AI_System/web_ui', 'AGENT_DIR': 'E:/AI_System/agent'}} 2025-08-27 20:50:51,505 - CoreConfig - INFO - 📂 从 E:\AI_System\config\local.json 加载配置: {} 2025-08-27 20:50:51,505 - CoreConfig - INFO - 📂 从 E:\AI_System\config\local.json 加载配置: {} 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🌐 从 E:\AI_System\.env 加载环境变量 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🌐 从 E:\AI_System\.env 加载环境变量 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🔄 环境变量覆盖: AGENT_DIR=E:/AI_System/agent 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🔄 环境变量覆盖: AGENT_DIR=E:/AI_System/agent 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🔄 环境变量覆盖: WEB_UI_DIR=E:/AI_System/web_ui 2025-08-27 20:50:51,506 - CoreConfig - INFO - 🔄 环境变量覆盖: WEB_UI_DIR=E:/AI_System/web_ui 2025-08-27 20:50:51,506 - CoreConfig - INFO - ✅ 配置系统初始化完成 2025-08-27 20:50:51,506 - CoreConfig - INFO - ✅ 配置系统初始化完成 2025-08-27 20:50:51,506 - ImportTest - ERROR - ❌ 测试过程中发生错误: cannot import name 'utils' from partially initialized module 'core' (most likely due to a circular import) (E:\AI_System\core\__init__.py) 2025-08-27 20:50:51,506 - ImportTest - ERROR - 详细堆栈跟踪: 2025-08-27 20:50:51,506 - ImportTest - ERROR - Traceback (most recent call last): File "E:\AI_System\tests\test_core_import.py", line 29, in <module> from core import CognitiveModule File "E:\AI_System\core\__init__.py", line 37, in <module> from . import utils ImportError: cannot import name 'utils' from partially initialized module 'core' (most likely due to a circular import) (E:\AI_System\core\__init__.py) (venv) PS E:\AI_System> python diagnose_architecture.py ❌ 导入失败: No module named 'models.base_model' ⚠️ 创建占位符CognitiveModule 2025-08-27 20:50:57,088 - CoreConfig - INFO - 📂 从 E:\AI_System\config\default.json 加载配置: {'LOG_DIR': 'E:/AI_System/logs', 'CONFIG_DIR': 'E:/AI_System/config', 'MODEL_CACHE_DIR': 'E:/AI_System/model_cache', 'AGENT_NAME': '小蓝', 'DEFAULT_USER': '管理员', 'MAX_WORKERS': 4, 'AGENT_RESPONSE_TIMEOUT': 30.0, 'MODEL_BASE_PATH': 'E:/AI_Models', 'MODEL_PATHS': {'TEXT_BASE': 'E:/AI_Models/Qwen2-7B', 'TEXT_CHAT': 'E:/AI_Models/deepseek-7b-chat', 'MULTIMODAL': 'E:/AI_Models/deepseek-vl2', 'IMAGE_GEN': 'E:/AI_Models/sdxl', 'YI_VL': 'E:/AI_Models/yi-vl', 'STABLE_DIFFUSION': 'E:/AI_Models/stable-diffusion-xl-base-1.0'}, 'NETWORK': {'HOST': '0.0.0.0', 'FLASK_PORT': 8000, 'GRADIO_PORT': 7860}, 'DATABASE': {'DB_HOST': 'localhost', 'DB_PORT': 5432, 'DB_NAME': 'ai_system', 'DB_USER': 'ai_user', 'DB_PASSWORD': 'secure_password_here'}, 'SECURITY': {'SECRET_KEY': 'generated-secret-key-here'}, 'ENVIRONMENT': {'ENV': 'dev', 'LOG_LEVEL': 'DEBUG', 'USE_GPU': True}, 'DIRECTORIES': {'DEFAULT_MODEL': 'E:/AI_Models/Qwen2-7B', 'WEB_UI_DIR': 'E:/AI_System/web_ui', 'AGENT_DIR': 'E:/AI_System/agent'}} 2025-08-27 20:50:57,088 - CoreConfig - INFO - 📂 从 E:\AI_System\config\local.json 加载配置: {} 2025-08-27 20:50:57,088 - CoreConfig - INFO - 🌐 从 E:\AI_System\.env 加载环境变量 2025-08-27 20:50:57,088 - CoreConfig - INFO - 🔄 环境变量覆盖: AGENT_DIR=E:/AI_System/agent 2025-08-27 20:50:57,088 - CoreConfig - INFO - 🔄 环境变量覆盖: WEB_UI_DIR=E:/AI_System/web_ui 2025-08-27 20:50:57,088 - CoreConfig - INFO - ✅ 配置系统初始化完成 Traceback (most recent call last): File "E:\AI_System\diagnose_architecture.py", line 8, in <module> from core.module_registry import validate_module_structure File "E:\AI_System\core\__init__.py", line 37, in <module> from . import utils ImportError: cannot import name 'utils' from partially initialized module 'core' (most likely due to a circular import) (E:\AI_System\core\__init__.py) (venv) PS E:\AI_System>”
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值