diff --git a/data/ai_config.json b/data/ai_config.json index 86f10c3..bd9cde8 100644 --- a/data/ai_config.json +++ b/data/ai_config.json @@ -1,5 +1,5 @@ { - "host": "localhost", + "host": "0.0.0.0", "port": 11434, "model": "qwen3:0.6b" } diff --git a/games/ai_chat.py b/games/ai_chat.py index 72e9d18..9f1a9d3 100644 --- a/games/ai_chat.py +++ b/games/ai_chat.py @@ -247,8 +247,27 @@ class AIChatGame(BaseGame): return str(response) except Exception as e: + error_msg = str(e) logger.error(f"生成AI回答错误: {e}", exc_info=True) - return f"❌ 生成回答时出错: {str(e)}" + + # 获取当前配置以便在错误消息中显示 + try: + config = self._load_config() + config_info = f"\n\n当前配置:\n- 地址: {config['host']}\n- 端口: {config['port']}\n- 模型: {config['model']}" + except: + config_info = "" + + # 提供更友好的错误消息 + if "Server disconnected" in error_msg or "RemoteProtocolError" in error_msg: + config = self._load_config() + test_cmd = f"curl -X POST http://{config.get('host', 'localhost')}:{config.get('port', 11434)}/api/generate -d '{{\"model\": \"{config.get('model', 'qwen3:0.6b')}\", \"prompt\": \"你是谁\", \"stream\": false}}'" + return f"❌ AI服务连接失败,请检查:\n1. Ollama服务是否已启动(在笔记本上)\n2. NPS端口转发是否正常工作\n3. 配置的地址是否为服务器IP(不是localhost)\n4. 模型名称是否正确\n\n测试命令(在服务器上执行):\n{test_cmd}{config_info}\n\n使用 `.aiconfig` 命令检查和修改配置" + elif "ConnectionError" in error_msg or "ConnectTimeout" in error_msg or "actively refused" in error_msg.lower() or "Empty reply" in error_msg: + return f"❌ 无法连接到Ollama服务(连接被拒绝)\n\n根据NPS日志,笔记本上的Ollama服务拒绝了连接。\n\n**解决方案**:\n\n1. **检查Ollama是否运行**:\n 在笔记本上运行:`tasklist | findstr ollama` 或 `Get-Process | Where-Object {{$_.ProcessName -like '*ollama*'}}`\n\n2. **设置Ollama监听地址**(重要!):\n 在Windows上,Ollama默认只监听127.0.0.1,需要设置为0.0.0.0才能被NPS访问\n \n 方法A:设置环境变量(推荐)\n 1. 打开系统环境变量设置\n 2. 添加环境变量:`OLLAMA_HOST=0.0.0.0:11434`\n 3. 重启Ollama服务或重启电脑\n \n 方法B:在命令行启动时指定\n ```powershell\n $env:OLLAMA_HOST=\"0.0.0.0:11434\"\n ollama serve\n ```\n\n3. **检查Windows防火墙**:\n 确保允许11434端口的入站连接\n\n4. **验证监听地址**:\n 在笔记本上运行:`netstat -an | findstr 11434`\n 应该看到 `0.0.0.0:11434` 而不是 `127.0.0.1:11434`\n\n5. **测试本地连接**:\n 在笔记本上运行:`curl http://localhost:11434/api/tags`\n 应该返回模型列表\n\n提示:如果使用NPS转发,配置中的host应该是服务器的IP地址,不是localhost!{config_info}\n\n使用 `.aiconfig host=服务器IP` 修改配置" + elif "timeout" in error_msg.lower(): + return f"❌ AI服务响应超时,请稍后重试{config_info}" + else: + return f"❌ 生成回答时出错: {error_msg}{config_info}" def _get_chat_engine(self, chat_id: int) -> Any: """获取或创建ChatEngine实例 @@ -273,9 +292,11 @@ class AIChatGame(BaseGame): from llama_index.core import ChatPromptTemplate, Settings # 创建Ollama LLM实例 + # 添加超时设置以避免长时间等待 llm = Ollama( model=config['model'], - base_url=f"http://{config['host']}:{config['port']}" + base_url=f"http://{config['host']}:{config['port']}", + timeout=120.0 # 120秒超时 ) # 设置全局LLM @@ -440,7 +461,7 @@ class AIChatGame(BaseGame): - `.aiconfig host=xxx port=xxx model=xxx` - 配置Ollama服务地址和模型 ### 配置示例 -\`.aiconfig host=localhost port=11434 model=llama3.1\` +`.aiconfig host=localhost port=11434 model=llama3.1` ### 说明 - 多个用户可以在同一个会话中提问