start_server_direct.py
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""
Debug-friendly LangGraph server launcher (single process).
Run this file in PyCharm Debug to hit breakpoints (e.g., pre_model_hook).
"""
import os
import sys
import json
from pathlib import Path
def setup_environment():
# Ensure project root on sys.path
root = Path(__file__).parent.resolve()
sys.path.insert(0, str(root))
# Load graphs from langgraph.json
graphs = {}
cfg = root / "langgraph.json"
if cfg.exists():
with open(cfg, "r", encoding="utf-8") as f:
try:
data = json.load(f)
graphs = data.get("graphs", {})
except Exception as e:
print(f"⚠️ 读取 langgraph.json 失败: {e}")
# Baseline env
os.environ.setdefault("LANGGRAPH_API_URL", "http://localhost:2025")
os.environ.setdefault("LANGGRAPH_RUNTIME_EDITION", "inmem")
os.environ.setdefault("LANGGRAPH_DISABLE_FILE_PERSISTENCE", "false")
os.environ.setdefault("LANGGRAPH_ALLOW_BLOCKING", "true")
os.environ.setdefault("ALLOW_PRIVATE_NETWORK", "true")
os.environ.setdefault("LANGSERVE_GRAPHS", json.dumps(graphs))
os.environ.setdefault("N_JOBS_PER_WORKER", "1")
os.environ.setdefault("ATTACH_SAVE_DIR", "uploads")
os.environ.setdefault("DATABASE_URI", ":memory:")
os.environ.setdefault("REDIS_URI", "fake")
os.environ.setdefault("MIGRATIONS_PATH", "__inmem")
# Load .env if present
env_file = root / ".env"
if env_file.exists():
try:
from dotenv import load_dotenv
load_dotenv(env_file)
print(" Loaded .env")
except Exception:
print(" python-dotenv 未安装,跳过 .env 加载")
def main():
print(" Starting LangGraph server (single-process, debug-friendly)...")
setup_environment()
print("\n" + "=" * 60)
print(" Server URL: http://localhost:2025")
print(" API Docs: http://localhost:2025/docs")
print(" Studio UI: http://localhost:2025/ui")
print(" Health: http://localhost:2025/ok")
print("=" * 60)
try:
import uvicorn
uvicorn.run(
"langgraph_api.server:app",
host="0.0.0.0",
port=2025,
reload=False, # disable auto-reload to avoid child processes
access_log=False,
)
except KeyboardInterrupt:
print("\n Server stopped by user")
except Exception as e:
print(f" Failed to start: {e}")
import traceback; traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()