-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
250 lines (211 loc) · 7.67 KB
/
cli.py
File metadata and controls
250 lines (211 loc) · 7.67 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""One-shot CLI for deep research without the HTTP server.
Usage examples::
# Basic research (outputs JSON by default)
python cli.py "quantum computing advances"
# DOCX report with auto-generated filename
python cli.py "climate change mitigation" --format docx
# Markdown to specific file, shallow preset
python cli.py "rust vs go" --format md --output comparison.md --preset shallow
# Write to stdout
python cli.py "LLM architectures" --format md --output -
"""
import asyncio
import argparse
import re
import sys
from datetime import date
from dotenv import load_dotenv
load_dotenv()
from artemis.writers import write_json, write_markdown, md_to_docx # noqa: E402
_FORMATS = ("json", "md", "docx")
_PRESETS = ("deep", "shallow")
def _slugify(text: str, max_len: int = 48) -> str:
"""Convert a query string into a filename-safe slug."""
slug = text.lower().strip()
slug = re.sub(r"[^\w\s-]", "", slug)
slug = re.sub(r"[\s_]+", "-", slug)
slug = re.sub(r"-+", "-", slug).strip("-")
return slug[:max_len]
def _default_output_path(query: str, fmt: str) -> str:
"""Generate an output filename from query and date."""
ext = {"json": "json", "md": "md", "docx": "docx"}[fmt]
slug = _slugify(query)
today = date.today().isoformat()
return f"{slug}-{today}.{ext}"
def _progress_callback(quiet: bool):
"""Return a progress callback (or None if quiet)."""
if quiet:
return None
icons = {
"start": "🎯",
"outline": "📋",
"pass": "🔄",
"search": "🔍",
"synthesis": "✍️",
"complete": "✅",
}
def show_progress(stage: str, message: str) -> None:
icon = icons.get(stage, "•")
print(f"{icon} {message}", file=sys.stderr)
return show_progress
async def run_outline(query: str, stages: int | None, preset: str) -> None:
import json
from artemis.config import get_settings
from artemis.researcher import generate_outline
settings = get_settings()
if preset == "shallow":
num_sections = stages or settings.shallow_research_stages
else:
num_sections = stages or settings.deep_research_stages
outline, _ = await generate_outline(query, num_sections=num_sections)
print(json.dumps(outline, indent=2))
async def run_research(
query: str,
fmt: str,
output: str | None,
preset: str,
stages: int | None,
passes: int | None,
quiet: bool,
) -> None:
from artemis.config import get_settings
from artemis.researcher import deep_research
settings = get_settings()
# Resolve preset defaults
if preset == "shallow":
stages = stages or settings.shallow_research_stages
passes = passes or settings.shallow_research_passes
sub_queries = settings.shallow_research_subqueries
results_per_query = settings.shallow_research_results_per_query
max_tokens = settings.shallow_research_max_tokens
content_extraction = settings.shallow_research_content_extraction
pages_per_section = settings.shallow_research_pages_per_section
content_max_chars = settings.shallow_research_content_max_chars
else:
stages = stages or settings.deep_research_stages
passes = passes or settings.deep_research_passes
sub_queries = settings.deep_research_subqueries
results_per_query = settings.deep_research_results_per_query
max_tokens = settings.deep_research_max_tokens
content_extraction = settings.deep_research_content_extraction
pages_per_section = settings.deep_research_pages_per_section
content_max_chars = settings.deep_research_content_max_chars
stdout = output == "-"
# Resolve output path
if output is None:
output = _default_output_path(query, fmt)
elif stdout and fmt == "docx":
print("Error: DOCX format cannot be written to stdout.", file=sys.stderr)
sys.exit(1)
progress = _progress_callback(quiet or stdout)
if not quiet and not stdout:
print(f"Research: {query}", file=sys.stderr)
print(f"Preset: {preset} | Stages: {stages} | Passes: {passes}", file=sys.stderr)
print("-" * 50, file=sys.stderr)
try:
result = await deep_research(
query=query,
stages=stages,
passes=passes,
sub_queries_per_stage=sub_queries,
results_per_query=results_per_query,
max_tokens=max_tokens,
content_extraction=content_extraction,
pages_per_section=pages_per_section,
content_max_chars=content_max_chars,
progress_callback=progress,
)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
# Write output
usage_dict = result.usage.model_dump() if result.usage else None
if fmt == "json":
write_json(output, query, result.essay, result.results, usage_dict, stdout=stdout)
elif fmt == "md":
write_markdown(output, result.essay, result.results, stdout=stdout)
elif fmt == "docx":
md_to_docx(output, result.essay, title=query, results=result.results)
if not stdout and not quiet:
print(f"\n✅ Saved to {output}", file=sys.stderr)
print(
f" {len(result.essay)} chars | {len(result.results)} sources",
file=sys.stderr,
)
if result.usage:
print(
f" Tokens: {result.usage.total_tokens} "
f"(in={result.usage.input_tokens} out={result.usage.output_tokens})",
file=sys.stderr,
)
def main() -> None:
parser = argparse.ArgumentParser(
description="Artemis — one-shot deep research from the command line",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=(
"examples:\n"
' %(prog)s "quantum computing"\n'
' %(prog)s "climate change" --format docx\n'
' %(prog)s "rust vs go" --format md -o comparison.md --preset shallow\n'
' %(prog)s "LLM architectures" --format md -o - # stdout\n'
),
)
parser.add_argument("query", help="Research query or question")
parser.add_argument(
"--format", "-f",
choices=_FORMATS,
default="json",
help="Output format (default: json)",
)
parser.add_argument(
"--output", "-o",
metavar="PATH",
default=None,
help='Output file path (default: auto-generated). Use "-" for stdout.',
)
parser.add_argument(
"--preset",
choices=_PRESETS,
default="deep",
help="Research preset (default: deep)",
)
parser.add_argument(
"--stages",
type=int,
default=None,
help="Number of outline sections (overrides preset)",
)
parser.add_argument(
"--passes",
type=int,
default=None,
help="Number of research passes (overrides preset)",
)
parser.add_argument(
"--quiet", "-q",
action="store_true",
help="Suppress progress output",
)
parser.add_argument(
"--outline-only",
action="store_true",
help="Generate and print the research outline as JSON, then exit",
)
args = parser.parse_args()
if args.outline_only:
asyncio.run(run_outline(query=args.query, stages=args.stages, preset=args.preset))
return
asyncio.run(
run_research(
query=args.query,
fmt=args.format,
output=args.output,
preset=args.preset,
stages=args.stages,
passes=args.passes,
quiet=args.quiet,
)
)
if __name__ == "__main__":
main()