ron2k1/diamondclaws
GitHub: ron2k1/diamondclaws
一个故意注入认知偏见的 AI 股票分析平台,通过多智能体圆桌辩论模拟机构共识的形成与扭曲过程。
Stars: 0 | Forks: 0
# DiamondClaws
**机构级偏见股票分析**
DiamondClaws 是一个由具有独特个性的 AI 智能体驱动的、故意带有偏见的股票分析平台。一个主导智能体负责处理所有任务,而三个个性子智能体以圆桌会议形式讨论金融问题。每次分析都会经过同行评审的认知偏见扭曲,并提供完整的审计追踪,准确显示什么被扭曲了以及原因。
**这是讽刺与戏仿。不构成投资建议。**
## 系统架构
```
+---------------------------+
| OpenClaw Gateway |
| WebSocket :18789 |
| Ed25519 device auth |
| Protocol v3 |
+-------------+-------------+
|
+-------------v-------------+
| DiamondClaws API |
| FastAPI :8000 |
| SSE streaming |
| 7 LLM providers |
+-------------+-------------+
|
+-------------v-------------+
| Intent Router |
| |
| $TICKER / finance keywords|
| / debate triggers |
| -> Roundtable |
| Everything else |
| -> Headmaster |
+------+------------+-------+
| |
+--------------v--+ +----v-----------------+
| Headmaster | | Roundtable (All 3) |
| (diamond-bull) | | |
| Lead agent | | Bullish Alpha |
| Tools + files | | Value Contrarian |
| Code execution | | Quant Momentum |
| Any topic | | Sequential debate |
+--------+---------+ +----------+-----------+
| |
+--------v-------------------------v-------+
| SOUL.md Personality Layer |
| |
| Bullish Alpha - Relentless optimist |
| Value Contrarian - Deep skeptic |
| Quant Momentum - Cold data purist |
| |
| Each has: Investment Mode, General Mode, |
| Task Mode, cognitive biases, catchphrase |
+-------------------+----------------------+
|
+-------------------v----------------------+
| Data Pipeline |
| |
| Yahoo Finance -> Distortion Engine |
| 15 bias functions per persona |
| Kahneman & Tversky 1974, Nickerson 1998 |
| SQLite cache (lazy-refresh >4hrs) |
+-------------------+----------------------+
|
+-------------------v----------------------+
| LLM Providers |
| |
| OpenRouter (200+ models, one key) |
| + Direct: OpenAI, Google, Anthropic, |
| DeepSeek, xAI, Mistral |
+------------------------------------------+
```
### 关键组件
| 组件 | 文件 | 用途 |
|-----------|------|---------|
| API Server | `main.py` | FastAPI 应用、启动种子、静态文件 |
| Routes | `api/routes.py` | 所有端点:chat, discuss, analyze, settings, workspace |
| Gateway Client | `tools/gateway_client.py` | OpenClaw gateway 的 WebSocket 客户端 (Ed25519 认证, 流式传输) |
| OpenClaw Integration | `tools/openclaw.py` | 智能体注册表, SOUL.md 加载, gateway 状态 |
| LLM Providers | `tools/providers.py` | 7 个 provider 注册表, API 密钥管理, 模型解析 |
| Analysis Engine | `tools/analysis.py` | 偏见分析生成, LLM 流式传输 |
| Distortion Engine | `tools/distortion.py` | 15 个带有学术引用的认知偏见函数 |
| Market Data | `tools/yfinance_fetch.py` | Yahoo Finance 基本面, 价格历史, 新闻 |
| Database | `models/database.py` | 通过 SQLAlchemy 实现 SQLite, 自动迁移 |
| Schemas | `models/schemas.py` | Pydantic 请求/响应模型 |
| Personas | `data/personas.py` | 人设定义, 偏见引用, SOUL 加载器 |
| Frontend | `web/index.html` | 单文件 Bloomberg 风格深色 UI |
| SOUL Files | `souls/*.md` | 性格优先的智能体身份文件 |
### 智能体架构
**Headmaster** 是主导智能体。所有非金融消息都通过 OpenClaw gateway 路由到 Headmaster (diamond-bull)。Headmaster 拥有工具、文件操作和代码执行权限,同时保持“看涨 Alpha” 的个性。
**圆桌会议** 在意图路由器检测到以下情况时激活:
- `$TICKER` 模式(例如 `$NVDA`)
- 2 个及以上金融关键词(stock, invest, portfolio 等)
- 显式触发词:“roundtable”, “consensus”, “all 3”, “debate”
- “analyze” + 任何金融关键词
所有三个智能体依次发言——每一个都会对前一个智能体的立场做出反应并进行辩论。
### 执行审批流程
当智能体尝试执行命令时,gateway 会通过 WebSocket 发送审批请求。这会在聊天 UI 中显示为一张交互式的批准/拒绝卡片。用户的决定会通过 gateway 发送回智能体。
```
Agent runs command -> Gateway sends exec.approval.request -> SSE to frontend
-> User clicks Approve/Deny -> POST /api/chat/approval -> Gateway resolves
```
## 在其他计算机上部署
### 方式 1:Docker(推荐)
```
git clone https://github.com/ron2k1/diamondclaws.git
cd diamondclaws
cp .env.example .env
```
编辑 `.env` 并添加至少一个 API 密钥(OpenRouter 只需一个密钥即可访问所有模型):
```
OPENROUTER_API_KEY=sk-or-v1-your-key-here
```
构建并运行(Docker 会自动运行 OpenClaw 设置):
```
docker compose up --build
```
打开 `http://localhost:8000`。
### 方式 2:Python(直接运行)
需要 Python 3.11+。
```
git clone https://github.com/ron2k1/diamondclaws.git
cd diamondclaws
python -m venv venv
```
激活虚拟环境:
```
# Linux / macOS
source venv/bin/activate
# Windows
venv\Scripts\activate
```
安装依赖并设置 OpenClaw:
```
pip install -r requirements.txt
python scripts/setup_openclaw.py
cp .env.example .env
```
编辑 `.env` 并添加至少一个 API 密钥:
```
OPENROUTER_API_KEY=sk-or-v1-your-key-here
```
运行:
```
uvicorn main:app --host 0.0.0.0 --port 8000
```
打开 `http://localhost:8000`。
### `setup_openclaw.py` 的作用
设置脚本会创建完整的 `~/.openclaw/` 目录结构,以便智能体开箱即用:
```
~/.openclaw/
openclaw.json # Gateway config with 3 diamond agents registered
identity/
device.json # Ed25519 keypair for gateway auth (auto-generated)
agents/
diamond-bull/
workspace/
SOUL.md # Bullish Alpha personality (copied from repo)
IDENTITY.md # Agent identity + pipeline position
AGENTS.md # Workspace readme
memory/ # Session memory directory
agent/
models.json # Model config (OpenRouter)
auth-profiles.json # API key references (env vars, not hardcoded)
diamond-value/ # Same structure — Value Contrarian
diamond-quant/ # Same structure — Quant Momentum
```
再次运行脚本是安全的——它会保留现有的 auth/channels 配置,仅更新智能体工作区和 SOUL.md 文件。
### 配合 OpenClaw Gateway(完整智能体模式)
要获得完整的智能体能力(工具、文件访问、代码执行),请 alongside DiamondClaws 启动 OpenClaw gateway:
```
# 终端 1:启动 gateway
openclaw gateway start
# 终端 2:启动 DiamondClaws
uvicorn main:app --host 0.0.0.0 --port 8000
```
DiamondClaws 会自动检测端口 18789 上的 gateway 并通过它进行路由。智能体将获得工作区、工具访问权限和执行审批流程。
如果没有 gateway,DiamondClaws 会回退到直接 LLM 调用(聊天仍然有效,但智能体无法使用工具)。
## API 密钥
您只需要 **一个** provider 即可开始。推荐使用 OpenRouter,因为一个密钥即可访问 200 多个模型。
| Provider | 环境变量 | 获取密钥 |
|----------|-------------|---------|
| OpenRouter | `OPENROUTER_API_KEY` | [openrouter.ai/keys](https://openrouter.ai/keys) |
| OpenAI | `OPENAI_API_KEY` | [platform.openai.com/api-keys](https://platform.openai.com/api-keys) |
| Google | `GOOGLE_API_KEY` | [aistudio.google.com/apikey](https://aistudio.google.com/apikey) |
| Anthropic | `ANTHROPIC_API_KEY` | [console.anthropic.com](https://console.anthropic.com/settings/keys) |
| DeepSeek | `DEEPSEEK_API_KEY` | [platform.deepseek.com](https://platform.deepseek.com/api_keys) |
| xAI | `XAI_API_KEY` | [console.x.ai](https://console.x.ai/) |
| Mistral | `MISTRAL_API_KEY` | [console.mistral.ai](https://console.mistral.ai/api-keys) |
密钥也可以在运行时通过 Web UI 的设置面板添加。
## API 端点
| 方法 | 路径 | 描述 |
|--------|------|-------------|
| `GET` | `/` | Web UI |
| `GET` | `/health` | 健康检查 |
| `GET` | `/api/stocks/search?q=` | 搜索股票 |
| `GET` | `/api/stocks/popular` | 热门股票代码 |
| `GET` | `/api/stocks/{ticker}` | 股票数据 |
| `GET` | `/api/stocks/{ticker}/history` | 价格历史 |
| `POST` | `/api/stocks/{ticker}/refresh` | 强制从 Yahoo 刷新 |
| `GET` | `/api/personas` | 列出人设 |
| `POST` | `/api/analyze` | 单一人设分析 |
| `POST` | `/api/analyze/parallel` | 并发执行所有 3 个人设 |
| `POST` | `/api/analyze/consensus` | 共识攻击(强制买入) |
| `POST` | `/api/chat` | 单一智能体聊天 (SSE 流) |
| `POST` | `/api/chat/discuss` | 3 智能体圆桌会议 (SSE 流) |
| `POST` | `/api/chat/approval` | 处理执行审批 |
| `GET` | `/api/chat/workspace/{agent_id}` | 列出智能体工作区文件 |
| `GET` | `/api/chat/workspace/{agent_id}/file?path=` | 读取智能体工作区文件 |
| `GET` | `/api/gateway/status` | OpenClaw gateway 状态 |
| `GET` | `/api/settings/keys` | Provider 密钥状态 |
| `PUT` | `/api/settings/keys` | 保存 API 密钥 |
| `GET` | `/api/settings/models` | 模型配置 |
| `PUT` | `/api/settings/models` | 更新模型配置 |
## 项目结构
```
diamondclaws/
main.py # FastAPI app entrypoint
requirements.txt # Python dependencies
Dockerfile # Container image
docker-compose.yml # One-command deployment
.env.example # Environment variable template
api/
routes.py # All API endpoints
models/
database.py # SQLite + SQLAlchemy
schemas.py # Pydantic models
tools/
analysis.py # Biased analysis generation
distortion.py # 15 cognitive bias functions
gateway_client.py # OpenClaw WebSocket client
openclaw.py # Agent registry + SOUL loader
providers.py # 7 LLM providers + key management
yfinance_fetch.py # Yahoo Finance data fetcher
data/
personas.py # Persona definitions + bias refs
souls/
bullish_alpha.md # Bullish Alpha SOUL.md
value_contrarian.md # Value Contrarian SOUL.md
quant_momentum.md # Quant Momentum SOUL.md
web/
index.html # Single-file frontend
scripts/
ingest_stocks.py # Batch stock ingestion
```
## 环境变量
| 变量 | 默认值 | 描述 |
|----------|---------|-------------|
| `PORT` | `8000` | 服务器端口 |
| `HOST` | `0.0.0.0` | 服务器绑定地址 |
| `DIAMONDCLAWS_DB` | `data/diamondclaws.db` | SQLite 数据库路径 |
| `OPENCLAW_GATEWAY_URL` | `ws://127.0.0.1:18789` | OpenClaw gateway WebSocket URL |
## 许可证
保留所有幻觉权利。
标签:AI智能体, AV绕过, DLL 劫持, Ed25519, FastAPI, PyRIT, satire, SSE流式传输, WebSocket, 人工智能, 依赖分析, 偏见引擎, 域名收集, 多智能体系统, 大语言模型, 机构共识, 用户模式Hook绕过, 股票分析, 行为金融学, 认知偏差, 认知战, 讽刺项目, 请求拦截, 逆向工具, 金融科技