Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
214 lines (188 loc) · 8.22 KB
/
Copy pathcli.py
File metadata and controls
214 lines (188 loc) · 8.22 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""
OPENDEV Enhanced CLI for Quant Autoresearch
"""
import typer
import sys
import os
import asyncio
from typing import Optional
from pathlib import Path
# Add src to path
sys.path.append(str(Path(__file__).parent / "src"))
from core.engine import QuantAutoresearchEngine
from safety.guard import SafetyGuard, SafetyLevel, ApprovalMode
from context.compactor import ContextCompactor
from models.router import model_router
from utils.iteration_tracker import iteration_tracker
from memory.playbook import Playbook
from tools.registry import LazyToolRegistry, SubagentType
from data.connector import DataConnector
from data.preprocessor import prepare_data as prepare_all_data
app = typer.Typer(help="Quant Autoresearch - OPENDEV Autonomous Engine")
@app.command()
def run(
iterations: int = typer.Option(10, "--iterations", "-i", help="Max research iterations"),
safety: str = typer.Option("high", "--safety", "-s", help="Safety level: low, medium, high, critical"),
max_context: int = typer.Option(95, "--max-context", "-c", help="Max context percentage usage"),
thinking_model: str = typer.Option("llama-3.1-8b-instant", "--think", help="Model for thinking phase"),
reasoning_model: str = typer.Option("llama-3.3-70b-versatile", "--reason", help="Model for reasoning phase"),
lazy_tools: bool = typer.Option(True, "--lazy/--no-lazy", help="Enable lazy tool discovery"),
approval_mode: str = typer.Option("semi", "--approval", help="Approval mode: auto, semi, manual"),
db: str = typer.Option("experiments/database/playbook.db", "--db", help="Path to playbook database")
):
"""Run the autonomous research loop"""
typer.echo(f"🚀 Initializing OPENDEV Session...")
engine = QuantAutoresearchEngine(
safety_level=SafetyLevel(safety.lower()),
max_context_percent=max_context,
thinking_model=thinking_model,
reasoning_model=reasoning_model,
lazy_tools=lazy_tools,
approval_mode=ApprovalMode(approval_mode.lower()),
db_path=db
)
asyncio.run(engine.run(max_iterations=iterations))
@app.command()
def status():
"""Show OPENDEV system status and metrics"""
typer.echo("📊 Quant Autoresearch Status")
typer.echo("=" * 50)
# Context status
compactor = ContextCompactor()
context_status = compactor.get_context_status()
typer.echo(f"🧠 Context Usage: {context_status['usage_percent']:.1f}%")
typer.echo(f"📝 Context History: {len(context_status['context_history'])} operations")
# Safety status
guard = SafetyGuard()
safety_summary = guard.get_safety_summary()
typer.echo(f"🛡️ Safety Level: {safety_summary['safety_level']}")
typer.echo(f"🚫 Operations Blocked: {safety_summary['blocked_operations']}")
# Model router status
model_stats = model_router.get_usage_stats()
typer.echo(f"🤖 Model Router:")
typer.echo(f" - Total calls: {model_stats['calls']['thinking_calls'] + model_stats['calls']['reasoning_calls']}")
typer.echo(f" - Total cost: ${model_stats['total_cost']:.4f}")
# Playbook status
playbook = Playbook()
playbook_stats = playbook.get_playbook_statistics()
typer.echo(f"📚 Playbook Patterns: {playbook_stats['total_patterns']}")
typer.echo("System is ready.")
@app.command()
def report():
"""Generate detailed performance and safety report"""
typer.echo("📄 Generating OPENDEV Analysis Report...")
typer.echo("=" * 60)
# Session statistics
session_stats = iteration_tracker.get_session_stats()
if "total_iterations" in session_stats and session_stats["total_iterations"] > 0:
typer.echo(f"📊 SESSION OVERVIEW")
typer.echo(f"Total Iterations: {session_stats['total_iterations']}")
typer.echo(f"Success Rate: {session_stats['success_rate']:.1%}")
typer.echo(f"Best Score: {session_stats['best_score']:.3f}")
# ACC Metrics
compactor = ContextCompactor()
acc_stats = compactor.get_acc_statistics()
typer.echo(f"\n🧠 ACC Metrics:")
typer.echo(f" - Stage: {acc_stats['current_status']['acc_stage']}")
typer.echo(f" - Compression: {acc_stats['compression_ratio']:.2f}")
# Model stats
model_stats = model_router.get_usage_stats()
typer.echo(f"\n🤖 Model Usage:")
typer.echo(f" - Thinking: {model_stats['calls']['thinking_calls']}")
typer.echo(f" - Reasoning: {model_stats['calls']['reasoning_calls']}")
typer.echo(f" - Total Cost: ${model_stats['total_cost']:.4f}")
else:
typer.echo("No data available. Run the engine first!")
@app.command()
def fetch(symbol: str, start: str = "2020-01-01"):
"""Fetch market data for a specific symbol"""
typer.echo(f"📥 Fetching data for {symbol} starting from {start}...")
connector = DataConnector()
if connector.fetch_and_cache(symbol, start):
typer.echo(f"✅ Successfully cached {symbol}")
else:
typer.echo(f"❌ Failed to fetch data for {symbol}")
@app.command()
def ingest(file: str, symbol: str):
"""Ingest custom CSV data into the research cache"""
typer.echo(f"📁 Ingesting {file} as {symbol}...")
connector = DataConnector()
if connector.ingest_custom_csv(file, symbol):
typer.echo(f"✅ Successfully ingested {symbol} from {file}")
else:
typer.echo(f"❌ Failed to ingest {file}")
@app.command()
def setup_data():
"""Download default market data symbols (SPY, BTC, etc.)"""
typer.echo("📊 Preparing default research dataset...")
prepare_all_data()
@app.command()
def test():
"""Test OPENDEV components"""
typer.echo("🧪 Testing OPENDEV Components")
typer.echo("=" * 40)
# 1. Router
typer.echo("1. Testing Model Router...")
try:
res = model_router.thinking_phase({"current_goal": "Test"})
typer.echo(f" ✅ Thinking: {res['success']}")
except Exception as e:
typer.echo(f" ❌ Error: {e}")
# 2. Safety
typer.echo("\n2. Testing Safety Guard...")
try:
guard = SafetyGuard()
res = guard.defense_in_depth_check({"tool_name": "test"}, SubagentType.PLANNER)
typer.echo(f" ✅ Safety: {res['safe']}")
except Exception as e:
typer.echo(f" ❌ Error: {e}")
# 3. Search
typer.echo("\n3. Testing Search Index...")
try:
from core.research import local_bm25_search
res = local_bm25_search("momentum")
typer.echo(f" ✅ BM25: Found {len(res)} papers")
except Exception as e:
typer.echo(f" ❌ Error: {e}")
@app.command()
def research(
query: str = typer.Option(..., "--query", "-q", help="Research query to investigate"),
web: bool = typer.Option(True, "--web/--no-web", help="Include web search"),
academic: bool = typer.Option(True, "--academic/--no-academic", help="Include academic search"),
max_results: int = typer.Option(5, "--max", "-m", help="Max results per source")
):
"""Run research on a topic using web and/or academic sources"""
typer.echo(f"🔍 Researching: {query}")
typer.echo("=" * 60)
from core.research import (
get_research_context,
search_web,
get_comprehensive_research_context
)
if web and academic:
typer.echo("📚 Fetching both academic and web sources...\n")
context = get_comprehensive_research_context(
query,
max_papers=max_results,
max_web_results=max_results,
include_web=True
)
typer.echo(context)
elif web:
typer.echo("🌐 Fetching web sources...\n")
results = search_web(query, num_results=max_results)
if results:
for i, r in enumerate(results, 1):
typer.echo(f"**Result {i}**: {r.get('title', 'N/A')}")
typer.echo(f" URL: {r.get('url', 'N/A')}")
typer.echo(f" Summary: {r.get('snippet', 'N/A')[:200]}...")
typer.echo()
else:
typer.echo("❌ No web results. Configure EXA_API_KEY or SERPAPI_KEY in .env")
else:
typer.echo("📄 Fetching academic sources...\n")
context = get_research_context(query, max_papers=max_results)
typer.echo(context)
if __name__ == "__main__":
app()