Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.git
.github
.pytest_cache
.venv
.tmp
.codex
__pycache__
*.pyc
*.pyo
*.pyd
data/*.db
tests
assets
models
app_config.toml
63 changes: 63 additions & 0 deletions DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Deployment

## Production baseline

1. Copy `app_config_example.toml` to `app_config.toml`.
2. Set `THINKING_GRAPH_SECRET_KEY` to a strong random value.
3. Keep `debug = false`, `enable_cors = false`, `allow_runtime_settings_write = false`, and `allow_db_fallback = false`.
4. Mount `./data` as a persistent volume.

## User isolation

The application now stores all nodes, connections, audits, and saved graphs with an `owner_id`.

- Default mode: session isolation. Each browser session gets its own graph space.
- Recommended production mode: proxy identity isolation. Configure your reverse proxy or auth gateway to inject a trusted user header, then set `trusted_identity_header` in `app_config.toml` or `THINKING_GRAPH_TRUSTED_IDENTITY_HEADER`.

Example:

```toml
[auth]
trusted_identity_header = "X-Forwarded-User"
session_cookie_secure = true
```

Important:

- Your reverse proxy must strip any client-supplied `X-Forwarded-User` header and set it itself.
- Runtime settings writes are disabled by default so normal users cannot change shared server-side LLM config.
- `/api/settings` no longer returns API keys in plaintext.

## Docker

Build:

```bash
docker build -t thinking-graph:latest .
```

Run:

```bash
docker run -d \
--name thinking-graph \
-p 5000:5000 \
-e THINKING_GRAPH_SECRET_KEY='replace-with-a-long-random-secret' \
-e THINKING_GRAPH_SESSION_COOKIE_SECURE=true \
-v $(pwd)/data:/app/data \
-v $(pwd)/app_config.toml:/app/app_config.toml:ro \
thinking-graph:latest
```

If you terminate TLS at a reverse proxy, also set:

```bash
-e APP_TRUSTED_PROXY_HOPS=1
```

## Reverse proxy checklist

- Terminate HTTPS before exposing the app publicly.
- Forward `X-Forwarded-Proto`, `X-Forwarded-Host`, and optionally `X-Forwarded-User`.
- If using header-based identity, strip any incoming `X-Forwarded-User` from the public internet first.
- Keep the app behind the proxy; do not expose Flask debug mode.
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
APP_HOST=0.0.0.0 \
APP_PORT=5000

WORKDIR /app

RUN addgroup --system app && adduser --system --ingroup app app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

RUN mkdir -p /app/data && chown -R app:app /app

USER app

EXPOSE 5000

CMD ["sh", "-c", "gunicorn --workers=2 --threads=4 --worker-class=gthread --bind=0.0.0.0:${APP_PORT:-5000} wsgi:app"]
142 changes: 142 additions & 0 deletions QUICK_START_REFACTOR.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Quick Start - Post-Refactoring

## What Changed?

The LLM integration has been completely refactored for better maintainability, testability, and extensibility. **All existing APIs work exactly the same** - no breaking changes!

## Installation (Choose One)

### Option 1: Cloud LLM (Recommended for Most Users)
```bash
pip install -r requirements.txt
```
Includes Flask + OpenAI API client. Use with DeepSeek, OpenAI, Claude, etc.

### Option 2: Local LLM with NPU
```bash
pip install -r requirements-local-llm.txt
```
Includes ONNX Runtime / OpenVINO for local inference.

### Option 3: Development Environment
```bash
pip install -r requirements-dev.txt
```
Adds pytest, httpx for testing.

### Option 4: Everything
```bash
pip install -r requirements/all.txt
```

## Running Tests

```bash
python -m pytest tests/test_llm_refactor.py -v
```

This runs 30+ tests covering:
- Schema validation
- Generation pipeline
- Review pipeline
- Backend adapters

## Using the New Architecture (Optional)

### Old Way (Still Works!)
```python
from backend.services.llm_service import LLMService

service = LLMService()
result = service.generate_graph_from_topic("AI ethics")
# Returns dict with nodes, connections, etc.
```

### New Way (Recommended for New Code)
```python
from config import LLMConfig
from backend.services.llm_backends import create_llm_backend
from backend.services.llm_graph_generation import GraphGenerationPipeline

# Create backend
config = LLMConfig.from_env()
backend = create_llm_backend(
config=config,
backend_type="remote_api",
api_key="your-api-key",
)

# Create pipeline
pipeline = GraphGenerationPipeline(backend)

# Generate graph
result = pipeline.generate(
topic="AI ethics",
max_nodes=15,
language="en"
)

# Access structured data
if result.enabled and result.draft:
for node in result.draft.nodes:
print(f"{node.id}: {node.content}")

for conn in result.draft.connections:
print(f"{conn.source_id} -> {conn.target_id} ({conn.conn_type})")
```

### Benefits of New Approach
- ✅ Type-safe access to nodes/connections
- ✅ Better error handling
- ✅ Direct access to validation results
- ✅ Easier to debug and test

## API Endpoints (Unchanged)

All endpoints work exactly as before:

### POST /api/llm/chat
```json
{
"prompt": "What are the main arguments?",
"language": "en"
}
```

### POST /api/llm/generate-graph
```json
{
"topic": "Climate change solutions",
"language": "en",
"max_nodes": 12
}
```

### POST /api/llm/review-graph
```json
{
"language": "en"
}
```

## Documentation

- **Full Refactoring Details**: `docs/LLM_REFACTORING.md`
- **Summary of Changes**: `REFACTORING_SUMMARY.md`
- **Architecture Overview**: See diagrams in docs

## Need Help?

1. Check `docs/LLM_REFACTORING.md` for detailed architecture
2. Read `tests/test_llm_refactor.py` for usage examples
3. All modules have docstrings explaining their purpose

## What's Next?

Consider these enhancements:
1. Add caching for frequent queries
2. Enable advanced critique in generation pipeline
3. Add monitoring for pipeline stages
4. Implement plugin system for custom backends

See `docs/LLM_REFACTORING.md` section "Future Enhancements" for details.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,31 @@ cd thinking_graph
python -m venv .venv
source .venv/bin/activate # Linux/Mac
# 或 .venv\Scripts\activate # Windows
```

#### 选择安装方式

# 安装依赖
**方式 1:最小运行(API LLM)- 推荐新手**
```bash
pip install -r requirements.txt
# 如果需要本地运行语言模型
# pip install -r ./requirements-local-llm.txt
```
包含 Flask + OpenAI API 客户端,适合使用 DeepSeek/OpenAI/Claude 等云端服务。

**方式 2:本地 LLM/NPU 加速**
```bash
pip install -r requirements-local-llm.txt
```
包含 ONNX Runtime / OpenVINO,适合本地推理和 NPU 加速。

**方式 3:开发测试环境**
```bash
pip install -r requirements-dev.txt
```
包含 pytest、httpx 等开发工具。

**方式 4:完整安装(所有依赖)**
```bash
pip install -r requirements/all.txt
```

### 配置
Expand All @@ -83,6 +103,10 @@ python main.py

打开浏览器访问 `http://localhost:5000`,开始构建你的第一张思维图!

## Production Deployment

For container deployment and multi-user isolation setup, see [DEPLOYMENT.md](DEPLOYMENT.md).

---

## 🎯 核心特性
Expand Down
Loading
Loading