Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
9b75bde
1
Parent(s):
116d9c5
delete vectara
Browse files- check_ollama.py +118 -0
- main.py +29 -0
check_ollama.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Ollama服务快速检查工具
|
| 3 |
+
在启动RAG系统前检查Ollama是否运行
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import requests
|
| 7 |
+
import subprocess
|
| 8 |
+
import sys
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def check_ollama_service():
|
| 12 |
+
"""检查Ollama服务是否运行"""
|
| 13 |
+
try:
|
| 14 |
+
response = requests.get('http://localhost:11434/api/tags', timeout=2)
|
| 15 |
+
if response.status_code == 200:
|
| 16 |
+
print("✅ Ollama服务运行正常")
|
| 17 |
+
|
| 18 |
+
# 显示已下载的模型
|
| 19 |
+
try:
|
| 20 |
+
models = response.json().get('models', [])
|
| 21 |
+
if models:
|
| 22 |
+
print(f"\n📦 已下载的模型 ({len(models)}个):")
|
| 23 |
+
for model in models:
|
| 24 |
+
print(f" - {model['name']}")
|
| 25 |
+
else:
|
| 26 |
+
print("\n⚠️ 没有已下载的模型")
|
| 27 |
+
print("请运行: ollama pull mistral")
|
| 28 |
+
except:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
return True
|
| 32 |
+
else:
|
| 33 |
+
return False
|
| 34 |
+
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def start_ollama_service():
|
| 39 |
+
"""尝试启动Ollama服务"""
|
| 40 |
+
print("🔧 正在尝试启动Ollama服务...")
|
| 41 |
+
try:
|
| 42 |
+
# 后台启动Ollama
|
| 43 |
+
process = subprocess.Popen(
|
| 44 |
+
['ollama', 'serve'],
|
| 45 |
+
stdout=subprocess.DEVNULL,
|
| 46 |
+
stderr=subprocess.DEVNULL
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 等待几秒让服务启动
|
| 50 |
+
import time
|
| 51 |
+
time.sleep(3)
|
| 52 |
+
|
| 53 |
+
# 再次检查
|
| 54 |
+
if check_ollama_service():
|
| 55 |
+
print("✅ Ollama服务已成功启动")
|
| 56 |
+
return True
|
| 57 |
+
else:
|
| 58 |
+
print("❌ Ollama服务启动失败")
|
| 59 |
+
return False
|
| 60 |
+
except FileNotFoundError:
|
| 61 |
+
print("❌ 未找到ollama命令,请先安装Ollama")
|
| 62 |
+
print(" 安装命令: curl -fsSL https://ollama.com/install.sh | sh")
|
| 63 |
+
return False
|
| 64 |
+
except Exception as e:
|
| 65 |
+
print(f"❌ 启动失败: {e}")
|
| 66 |
+
return False
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def main():
|
| 70 |
+
"""主函数"""
|
| 71 |
+
print("=" * 60)
|
| 72 |
+
print("🔍 Ollama服务检查工具")
|
| 73 |
+
print("=" * 60)
|
| 74 |
+
|
| 75 |
+
if check_ollama_service():
|
| 76 |
+
print("\n✅ 检查通过,可以启动RAG系统")
|
| 77 |
+
return 0
|
| 78 |
+
else:
|
| 79 |
+
print("\n❌ Ollama服务未运行")
|
| 80 |
+
|
| 81 |
+
# 询问是否自动启动
|
| 82 |
+
print("\n是否尝试自动启动Ollama服务?")
|
| 83 |
+
print("1. 是(推荐)")
|
| 84 |
+
print("2. 否,手动启动")
|
| 85 |
+
|
| 86 |
+
choice = input("\n请选择 [1/2]: ").strip()
|
| 87 |
+
|
| 88 |
+
if choice == "1":
|
| 89 |
+
if start_ollama_service():
|
| 90 |
+
print("\n✅ 现在可以启动RAG系统了")
|
| 91 |
+
return 0
|
| 92 |
+
else:
|
| 93 |
+
print("\n❌ 自动启动失败,请手动启动")
|
| 94 |
+
print_manual_instructions()
|
| 95 |
+
return 1
|
| 96 |
+
else:
|
| 97 |
+
print_manual_instructions()
|
| 98 |
+
return 1
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def print_manual_instructions():
|
| 102 |
+
"""打印手动启动说明"""
|
| 103 |
+
print("\n" + "=" * 60)
|
| 104 |
+
print("📖 手动启动Ollama服务")
|
| 105 |
+
print("=" * 60)
|
| 106 |
+
print("\n方式1: 在终端运行")
|
| 107 |
+
print(" $ ollama serve")
|
| 108 |
+
print("\n方式2: 在Python中运行")
|
| 109 |
+
print(" import subprocess")
|
| 110 |
+
print(" subprocess.Popen(['ollama', 'serve'])")
|
| 111 |
+
print("\n方式3: 在Kaggle Notebook中运行")
|
| 112 |
+
print(" %run KAGGLE_LOAD_OLLAMA.py")
|
| 113 |
+
print("\n启动后请重新运行RAG系统")
|
| 114 |
+
print("=" * 60)
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
sys.exit(main())
|
main.py
CHANGED
|
@@ -27,6 +27,25 @@ class AdaptiveRAGSystem:
|
|
| 27 |
print(f"❌ {e}")
|
| 28 |
raise
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# 初始化文档处理器
|
| 31 |
print("设置文档处理器...")
|
| 32 |
self.doc_processor, self.vectorstore, self.retriever, self.doc_splits = initialize_document_processor()
|
|
@@ -45,6 +64,16 @@ class AdaptiveRAGSystem:
|
|
| 45 |
|
| 46 |
print("✅ 自适应RAG系统初始化完成!")
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def _build_workflow(self):
|
| 49 |
"""构建工作流图"""
|
| 50 |
# 创建工作流节点实例,传递DocumentProcessor实例和retriever
|
|
|
|
| 27 |
print(f"❌ {e}")
|
| 28 |
raise
|
| 29 |
|
| 30 |
+
# 检查 Ollama 服务是否运行
|
| 31 |
+
print("🔍 检查 Ollama 服务状态...")
|
| 32 |
+
if not self._check_ollama_service():
|
| 33 |
+
print("\n" + "="*60)
|
| 34 |
+
print("❌ Ollama 服务未启动!")
|
| 35 |
+
print("="*60)
|
| 36 |
+
print("\n请先启动 Ollama 服务:")
|
| 37 |
+
print("\n方法1: 在终端运行")
|
| 38 |
+
print(" $ ollama serve")
|
| 39 |
+
print("\n方法2: 在 Kaggle Notebook 中运行")
|
| 40 |
+
print(" import subprocess")
|
| 41 |
+
print(" subprocess.Popen(['ollama', 'serve'])")
|
| 42 |
+
print("\n方法3: 使用快捷脚本")
|
| 43 |
+
print(" %run KAGGLE_LOAD_OLLAMA.py")
|
| 44 |
+
print("="*60)
|
| 45 |
+
raise ConnectionError("Ollama 服务未运行,请先启动服务")
|
| 46 |
+
|
| 47 |
+
print("✅ Ollama 服务运行正常")
|
| 48 |
+
|
| 49 |
# 初始化文档处理器
|
| 50 |
print("设置文档处理器...")
|
| 51 |
self.doc_processor, self.vectorstore, self.retriever, self.doc_splits = initialize_document_processor()
|
|
|
|
| 64 |
|
| 65 |
print("✅ 自适应RAG系统初始化完成!")
|
| 66 |
|
| 67 |
+
def _check_ollama_service(self) -> bool:
|
| 68 |
+
"""检查 Ollama 服务是否运行"""
|
| 69 |
+
import requests
|
| 70 |
+
try:
|
| 71 |
+
# 尝试连接 Ollama API
|
| 72 |
+
response = requests.get('http://localhost:11434/api/tags', timeout=2)
|
| 73 |
+
return response.status_code == 200
|
| 74 |
+
except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
|
| 75 |
+
return False
|
| 76 |
+
|
| 77 |
def _build_workflow(self):
|
| 78 |
"""构建工作流图"""
|
| 79 |
# 创建工作流节点实例,传递DocumentProcessor实例和retriever
|