Spaces:
Paused
Paused
lanny xu
commited on
Commit
·
152b32b
1
Parent(s):
25e7f71
delete files
Browse files- local_llm_rag.py +6 -5
- setup_and_run_multimodal.py +111 -0
local_llm_rag.py
CHANGED
|
@@ -168,12 +168,13 @@ class GraphState(TypedDict):
|
|
| 168 |
documents: List[str]
|
| 169 |
|
| 170 |
try:
|
| 171 |
-
|
| 172 |
-
except ImportError:
|
|
|
|
| 173 |
try:
|
| 174 |
-
|
| 175 |
-
except ImportError:
|
| 176 |
-
|
| 177 |
|
| 178 |
|
| 179 |
def retrieve(state):
|
|
|
|
| 168 |
documents: List[str]
|
| 169 |
|
| 170 |
try:
|
| 171 |
+
from langchain_core.documents import Document
|
| 172 |
+
except ImportError:
|
| 173 |
+
print("langchain_core.documents not found, using langchain.schema")
|
| 174 |
try:
|
| 175 |
+
from langchain_core.documents import Document
|
| 176 |
+
except ImportError:
|
| 177 |
+
from langchain.schema import Document
|
| 178 |
|
| 179 |
|
| 180 |
def retrieve(state):
|
setup_and_run_multimodal.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
环境配置和运行脚本 (自适应RAG多模态版)
|
| 4 |
+
只负责配置环境和运行 main.py (包含多模态检索功能)
|
| 5 |
+
|
| 6 |
+
使用方法:
|
| 7 |
+
python setup_and_run_multimodal.py
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import sys
|
| 12 |
+
import subprocess
|
| 13 |
+
|
| 14 |
+
print("="*60)
|
| 15 |
+
print("🚀 自适应RAG (多模态) 环境配置和运行")
|
| 16 |
+
print("="*60)
|
| 17 |
+
|
| 18 |
+
# ============================================================
|
| 19 |
+
# 1. 配置环境
|
| 20 |
+
# ============================================================
|
| 21 |
+
def setup_environment():
|
| 22 |
+
"""配置环境变量"""
|
| 23 |
+
print("\n⚙️ 步骤 1/2: 配置环境变量...")
|
| 24 |
+
|
| 25 |
+
# 检查.env文件
|
| 26 |
+
if os.path.exists(".env"):
|
| 27 |
+
print(" ✅ 发现 .env 文件,加载配置...")
|
| 28 |
+
try:
|
| 29 |
+
from dotenv import load_dotenv
|
| 30 |
+
load_dotenv()
|
| 31 |
+
print(" ✅ 环境变量已加载")
|
| 32 |
+
except ImportError:
|
| 33 |
+
print(" ⚠️ python-dotenv 未安装,跳过 .env 加载")
|
| 34 |
+
else:
|
| 35 |
+
print(" ℹ️ 未找到 .env 文件")
|
| 36 |
+
|
| 37 |
+
# 显示环境变量状态
|
| 38 |
+
print("\n 📋 环境变量状态:")
|
| 39 |
+
print(f" • TAVILY_API_KEY: {'✅ 已设置' if os.environ.get('TAVILY_API_KEY') else '⚠️ 未设置'}")
|
| 40 |
+
print(f" • NOMIC_API_KEY: {'✅ 已设置' if os.environ.get('NOMIC_API_KEY') else '⚠️ 未设置'}")
|
| 41 |
+
|
| 42 |
+
# 添加当前目录到 Python 路径
|
| 43 |
+
current_dir = os.getcwd()
|
| 44 |
+
if current_dir not in sys.path:
|
| 45 |
+
sys.path.insert(0, current_dir)
|
| 46 |
+
print(f"\n ✅ 已添加到 Python 路径: {current_dir}")
|
| 47 |
+
|
| 48 |
+
print("\n 💡 注意: 多模态功能需要Pillow库,请确保已安装")
|
| 49 |
+
|
| 50 |
+
# ============================================================
|
| 51 |
+
# 2. 运行 main.py
|
| 52 |
+
# ============================================================
|
| 53 |
+
def run_main_multimodal():
|
| 54 |
+
"""运行 main.py"""
|
| 55 |
+
print("\n🚀 步骤 2/2: 运行 main.py (多模态自适应检索)...")
|
| 56 |
+
print("="*60)
|
| 57 |
+
|
| 58 |
+
# 检查文件是否存在
|
| 59 |
+
if not os.path.exists("main.py"):
|
| 60 |
+
print("\n❌ 错误: 未找到 main.py 文件")
|
| 61 |
+
print(" 请确保在正确的目录中运行此脚本")
|
| 62 |
+
return False
|
| 63 |
+
|
| 64 |
+
print("\n🔄 启动自适应RAG系统...\n")
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# 运行 main.py
|
| 68 |
+
result = subprocess.run(
|
| 69 |
+
[sys.executable, "main.py"],
|
| 70 |
+
capture_output=False, # 实时显示输出
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if result.returncode == 0:
|
| 74 |
+
print("\n" + "="*60)
|
| 75 |
+
print("✅ 运行成功!")
|
| 76 |
+
print("="*60)
|
| 77 |
+
return True
|
| 78 |
+
else:
|
| 79 |
+
print("\n" + "="*60)
|
| 80 |
+
print(f"❌ 运行失败 (返回码: {result.returncode})")
|
| 81 |
+
print("="*60)
|
| 82 |
+
return False
|
| 83 |
+
|
| 84 |
+
except KeyboardInterrupt:
|
| 85 |
+
print("\n\n⚠️ 用户中断执行")
|
| 86 |
+
return False
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"\n❌ 运行时错误: {e}")
|
| 89 |
+
return False
|
| 90 |
+
|
| 91 |
+
# ============================================================
|
| 92 |
+
# 主函数
|
| 93 |
+
# ============================================================
|
| 94 |
+
def main():
|
| 95 |
+
"""主执行流程"""
|
| 96 |
+
try:
|
| 97 |
+
# 1. 配置环境
|
| 98 |
+
setup_environment()
|
| 99 |
+
|
| 100 |
+
# 2. 运行 main.py
|
| 101 |
+
run_main_multimodal()
|
| 102 |
+
|
| 103 |
+
except KeyboardInterrupt:
|
| 104 |
+
print("\n\n⚠️ 用户中断执行")
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"\n❌ 执行过程中出错: {e}")
|
| 107 |
+
import traceback
|
| 108 |
+
traceback.print_exc()
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
main()
|