4. 测试是否配置正确在客户端发送一条消息,观察监控器的输出。你应该能看到类似这样的信息:text📡 POST请求: /api/chat
Code:
python# test_client_config.py
import requests
def test_direct_ollama():
"""测试直接连接Ollama"""
print("🔍 测试直接连接Ollama (11434)...")
try:
response = requests.get("http://localhost:11434/api/tags", timeout=5)
if response.status_code == 200:
print("✅ 直接连接正常")
return True
except Exception as e:
print(f"❌ 直接连接失败: {e}")
return False
def test_proxy():
"""测试代理连接"""
print("\n🔍 测试代理连接 (11435)...")
try:
response = requests.get("http://localhost:11435/api/tags", timeout=5)
if response.status_code == 200:
print("✅ 代理连接正常")
return True
except Exception as e:
print(f"❌ 代理连接失败: {e}")
return False
def test_chat_through_proxy():
"""通过代理发送测试消息"""
print("\n💬 通过代理发送测试消息...")
test_data = {
"model": "deepseek-v3.1:671b-cloud",
"prompt": "请说'代理测试成功'",
"stream": False
}
try:
response = requests.post(
"http://localhost:11435/api/generate",
json=test_data,
timeout=30
)
if response.status_code == 200:
result = response.json()
print(f"✅ 代理聊天成功: {result.get('response', '')}")
return True
else:
print(f"❌ 聊天失败: {response.status_code}")
return False
except Exception as e:
print(f"❌ 聊天测试失败: {e}")
return False
def check_common_clients():
"""检查常见客户端的配置文件"""
print("\n🔧 检查常见客户端配置位置:")
import os
# ChatBox 可能的配置位置
chatbox_paths = [
os.path.expanduser("~/AppData/Roaming/ChatBox"),
os.path.expanduser("~/.config/ChatBox"),
"C:/Program Files/ChatBox"
]
for path in chatbox_paths:
if os.path.exists(path):
print(f"📁 ChatBox配置可能位于: {path}")
print("\n📝 手动检查步骤:")
print("1. 打开你的Ollama客户端")
print("2. 寻找 'Settings' 或 '设置'")
print("3. 寻找 'API'、'Connection' 或 '连接'")
print("4. 修改 'Base URL'、'API URL' 或 'Endpoint'")
print("5. 将值改为: http://localhost:11435")
def main():
print("=" * 50)
print("🛠️ Ollama代理配置诊断工具")
print("=" * 50)
# 测试连接
direct_ok = test_direct_ollama()
proxy_ok = test_proxy()
if not direct_ok:
print("\n❌ Ollama未运行!")
return
if not proxy_ok:
print("\n❌ 代理服务器未运行!")
print("请先运行代理程序:")
print(" python monitor_proxy.py")
print(" 或 python ollama_proxy_simple.py")
return
# 测试聊天
chat_ok = test_chat_through_proxy()
print("\n" + "=" * 50)
print("📋 诊断结果:")
print(f" Ollama服务: {'✅ 正常' if direct_ok else '❌ 异常'}")
print(f" 代理服务器: {'✅ 正常' if proxy_ok else '❌ 异常'}")
print(f" 代理聊天: {'✅ 正常' if chat_ok else '❌ 异常'}")
if direct_ok and proxy_ok and chat_ok:
print("\n🎉 所有基础测试通过!")
print("\n⚠️ 如果你的客户端还是看不到对话,可能是:")
print(" 1. 客户端没有使用代理端口 (11435)")
print(" 2. 客户端缓存了旧配置")
print(" 3. 需要重启客户端")
else:
check_common_clients()
print("\n💡 解决方案:")
print("1. 确保代理程序正在运行")
print("2. 将客户端API地址改为: http://localhost:11435")
print("3. 重启客户端")
print("4. 重新发送消息")
if __name__ == "__main__":
main()