-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt_expert_enhence.py
More file actions
1675 lines (1458 loc) · 60.3 KB
/
Copy pathprompt_expert_enhence.py
File metadata and controls
1675 lines (1458 loc) · 60.3 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Manifest Generator – CLI Tool for Reproducible Instruction Manifests via Ollama.
Version: 2.1.0
Usage:
manifest generate <task> [--topic TOPIC ...] [--model MODEL] [options]
manifest parallel <task> [--topic TOPIC ...] [options]
manifest synthesis <task> <file_a> <file_b> [options]
manifest full <task> [--topic TOPIC ...] [options]
manifest memory view
manifest memory clear
Commands:
generate Single model manifest generation (multi‑topic optional).
parallel Parallel generation from two models (MODEL_A, MODEL_B).
synthesis Run the meta‑architect synthesis on two existing manifests.
full Full pipeline: parallel + synthesis.
memory Manage session memory (view / clear).
For detailed help on a command: manifest <command> --help
"""
import argparse
import json
import logging
import os
import platform
import re
import shutil
import subprocess
import sys
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime, timezone
from pathlib import Path
from typing import List, Optional, Tuple, Dict
from urllib.parse import quote_plus
import requests
# ----------------------------------------------------------------------
# Configuration – can be overridden via environment or command line
# ----------------------------------------------------------------------
OLLAMA_URL = "http://localhost:11434/api/generate"
BASE_DIR = Path(__file__).resolve().parent
MEMORY_DIR = BASE_DIR / "memory"
OUTPUT_DIR = BASE_DIR / "outputs"
MEMORY_FILE = MEMORY_DIR / "sessions.json"
METHODOLOGY_FILE = BASE_DIR / "prompte_expert_methodologie.json"
DEFAULT_MODEL_A = "llama3:latest"
DEFAULT_MODEL_B = "qwen2.5:7b"
DEFAULT_SYNTH_MODEL = "qwen2.5:7b"
DEFAULT_TEMPERATURE = 0.3
DEFAULT_TIMEOUT = 600 # seconds for a single Ollama call
SYNTH_TIMEOUT = 1200 # longer timeout for synthesis
# Technique appliquées par défaut (peuvent être réglées via CLI)
DEFAULT_TECHNIQUES = [1, 5, 8, 10, 12, 14, 18, 25, 40, 47, 108, 121, 125, 147, 153]
# Create directories at import time
MEMORY_DIR.mkdir(exist_ok=True)
OUTPUT_DIR.mkdir(exist_ok=True)
# Load methodologies from JSON
TECHNIQUES_DB: Dict[int, Dict[str, str]] = {}
CATEGORIES_DB: List[Dict] = []
ANTI_PATTERNS: List[Dict] = []
QUICK_REFERENCE: Dict[str, List[int]] = {}
def load_methodologies():
global TECHNIQUES_DB, CATEGORIES_DB, ANTI_PATTERNS, QUICK_REFERENCE
if not METHODOLOGY_FILE.exists():
return
try:
raw = METHODOLOGY_FILE.read_text(encoding="utf-8")
data = json.loads(raw)
if "categories" in data:
CATEGORIES_DB = data["categories"]
for cat in CATEGORIES_DB:
for tech in cat.get("techniques", []):
TECHNIQUES_DB[tech["id"]] = {
"title": tech["title"],
"description": tech["description"],
"category": cat["name"],
}
ANTI_PATTERNS = data.get("anti_patterns", [])
QUICK_REFERENCE = data.get("quick_reference", {}).get("mappings", {})
else:
for line in raw.strip().split("\n"):
if line and line[0].isdigit():
match = re.match(r"(\d+)\.\s+\*\*(.+?)\*\*\s+–\s+(.+)", line)
if match:
num = int(match.group(1))
TECHNIQUES_DB[num] = {"title": match.group(2), "description": match.group(3)}
except Exception as e:
logger.warning(f"Could not load methodologies: {e}")
load_methodologies()
# Logging setup
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.StreamHandler(sys.stderr)],
)
logger = logging.getLogger("manifest_gen")
# ----------------------------------------------------------------------
# Ollama bootstrap — detect, install, list models, pull
# ----------------------------------------------------------------------
OLLAMA_API_BASE = OLLAMA_URL.rsplit("/api/", 1)[0] # http://localhost:11434
def detect_os() -> str:
s = platform.system().lower()
if s == "darwin":
return "mac"
if s == "windows":
return "windows"
return "linux"
def is_ollama_installed() -> bool:
return shutil.which("ollama") is not None
def is_ollama_running() -> bool:
try:
requests.get(f"{OLLAMA_API_BASE}/api/tags", timeout=3)
return True
except Exception:
return False
def install_ollama_interactive() -> bool:
os_type = detect_os()
print()
print(" [!] Ollama is not installed on this machine.")
print(f" Detected OS: {os_type}")
print()
if os_type == "mac":
print(" Installation options:")
print(" 1. Automatic curl install (recommended)")
print(" 2. Cancel")
c = input(" Choice > ").strip()
if c != "1":
return False
print("\n Installing ...")
try:
subprocess.run(
["bash", "-c", "curl -fsSL https://ollama.com/install.sh | sh"],
check=True,
)
print(" Ollama installed successfully.")
return True
except subprocess.CalledProcessError as e:
print(f" [ERROR] Installation failed: {e}")
return False
elif os_type == "linux":
print(" Installation options:")
print(" 1. Automatic curl install (recommended)")
print(" 2. Cancel")
c = input(" Choice > ").strip()
if c != "1":
return False
print("\n Installing ...")
try:
subprocess.run(
["bash", "-c", "curl -fsSL https://ollama.com/install.sh | sh"],
check=True,
)
print(" Ollama installed successfully.")
return True
except subprocess.CalledProcessError as e:
print(f" [ERROR] Installation failed: {e}")
return False
elif os_type == "windows":
print(" Windows automatic installation:")
print(" 1. Download and run installer (winget)")
print(" 2. Cancel")
c = input(" Choice > ").strip()
if c != "1":
return False
print("\n Installing via winget ...")
try:
subprocess.run(
["winget", "install", "Ollama.Ollama", "--accept-source-agreements", "--accept-package-agreements"],
check=True,
)
print(" Ollama installed successfully.")
print(" [!] Terminal restart may be required.")
return True
except FileNotFoundError:
print(" [!] winget not available. Trying PowerShell fallback ...")
try:
subprocess.run(
["powershell", "-Command",
"Invoke-WebRequest -Uri 'https://ollama.com/download/OllamaSetup.exe' -OutFile '$env:TEMP\\OllamaSetup.exe'; Start-Process '$env:TEMP\\OllamaSetup.exe' -Wait"],
check=True,
)
print(" Installer launched. Wait for installation to complete.")
return True
except Exception as e2:
print(f" [ERROR] {e2}")
print(" Install manually: https://ollama.com/download")
return False
except subprocess.CalledProcessError as e:
print(f" [ERROR] {e}")
return False
return False
def start_ollama_serve():
if is_ollama_running():
return
print(" Starting Ollama in background ...")
os_type = detect_os()
try:
if os_type == "windows":
subprocess.Popen(
["ollama", "serve"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP | subprocess.DETACHED_PROCESS,
)
else:
subprocess.Popen(
["ollama", "serve"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
start_new_session=True,
)
for _ in range(15):
time.sleep(1)
if is_ollama_running():
print(" Ollama is ready.")
return
print(" [!] Ollama started but not ready yet. Continuing anyway.")
except Exception as e:
print(f" [!] Could not start Ollama: {e}")
def list_local_models() -> List[Dict[str, str]]:
try:
resp = requests.get(f"{OLLAMA_API_BASE}/api/tags", timeout=5)
resp.raise_for_status()
data = resp.json()
models = []
for m in data.get("models", []):
name = m.get("name", "")
size_bytes = m.get("size", 0)
size_gb = size_bytes / (1024 ** 3) if size_bytes else 0
modified = m.get("modified_at", "")[:10]
models.append({
"name": name,
"size": f"{size_gb:.1f}GB" if size_gb else "?",
"modified": modified,
})
return models
except Exception:
return []
def pull_model_interactive(model_name: str) -> bool:
print(f"\n Downloading model '{model_name}' ...")
print(" (This may take several minutes depending on size)")
print()
try:
proc = subprocess.Popen(
["ollama", "pull", model_name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
)
for line in proc.stdout:
line = line.strip()
if line:
sys.stdout.write(f"\r {line[:70]:<70}")
sys.stdout.flush()
proc.wait()
print()
if proc.returncode == 0:
print(f" Model '{model_name}' downloaded successfully.")
return True
else:
print(f" [ERROR] ollama pull returned code {proc.returncode}")
return False
except FileNotFoundError:
print(" [ERROR] 'ollama' command not found.")
return False
except Exception as e:
print(f" [ERROR] {e}")
return False
def pick_model_interactive(label: str, current: str) -> str:
models = list_local_models()
print()
if models:
print(f" -- {label} --")
print(f" Locally installed models:")
for i, m in enumerate(models, 1):
marker = " <-- current" if m["name"] == current else ""
print(f" {i:3d}. {m['name']:<30s} {m['size']:>6s} {m['modified']}{marker}")
print(f" {len(models)+1:3d}. [Enter a name manually / pull a new model]")
print()
raw = input(f" Choice [{current}] > ").strip()
if not raw:
return current
try:
idx = int(raw)
if 1 <= idx <= len(models):
return models[idx - 1]["name"]
except ValueError:
pass
if raw.isdigit() and int(raw) == len(models) + 1:
raw = ""
else:
print(f" -- {label} --")
print(" No models installed locally.")
print()
raw = ""
if not raw:
name = input(f" Model name (e.g. llama3:latest, qwen2.5:7b) [{current}] > ").strip()
if not name:
return current
raw = name
local_names = [m["name"] for m in models]
if raw not in local_names:
print(f"\n Model '{raw}' is not installed locally.")
do_pull = input(" Download now? (yes/no) [yes] > ").strip().lower()
if do_pull in ("", "yes", "y", "oui", "o", "1"):
if pull_model_interactive(raw):
return raw
else:
print(f" Using previous model: {current}")
return current
else:
print(f" [!] Model '{raw}' will be used as-is (may fail if not available).")
return raw
def ensure_ollama_ready():
if not is_ollama_installed():
if not install_ollama_interactive():
print("\n [!] Ollama is not available. Generation will fail.")
print(" Install manually: https://ollama.com/download")
return
if not is_ollama_running():
start_ollama_serve()
# ----------------------------------------------------------------------
# Meta‑prompts (unchanged from original, kept for brevity)
# ----------------------------------------------------------------------
META_PROMPT = """
# META-PROMPT: REPRODUCIBLE INSTRUCTION MANIFEST GENERATOR
> Usage: Paste this meta-prompt into any LLM agent, then provide your task in the USER_INPUT section. It will produce a complete, expert-level instruction manifest that another LLM agent can execute without prior context.
---
## ROLE
You are a senior prompt architect specializing in producing reproducible instruction manifests. Your mission is never to write code or execute the task. Your mission is to produce a manifest document that describes the task with such methodological precision that another LLM agent (Claude, GPT, Gemini, Llama, Mistral, Qwen) can reproduce it entirely using its own implementation methods.
You write like an RFC author, product spec writer, or technical article author — not like a developer. You describe the WHAT, the WHY, the IN WHAT ORDER, and the CONCEPTUAL-HOW — never the SYNTACTIC-HOW.
---
## USER INPUT
<<<USER_INPUT
{USER_INPUT}
USER_INPUT>>>
{TOPIC_FOCUS}
{MEMORY_CONTEXT}
{WEB_CONTEXT}
---
## ABSOLUTE OUTPUT RULES
1. Zero lines of code. No code blocks. No raw shell commands. No API syntax.
2. Total reproducibility. A different LLM agent must be able to reproduce the result.
3. Implementation-agnostic.
4. Manifest / article / spec style. Numbered sections, clear headings.
5. Expert level. Anticipate edge cases, pitfalls, and ambiguities.
6. Self-diagnose ambiguity if the input is ambiguous.
7. No length limit: write as much as necessary to exhaust the subject.
---
## MANDATORY STRUCTURE OF THE GENERATED MANIFEST
You produce exactly the following 12 sections, in this order:
### § 1. TITLE & EXECUTIVE SUMMARY
### § 2. FINAL OBJECTIVE & SUCCESS DEFINITION
### § 3. EXECUTION CONTEXT & PREREQUISITES
### § 4. AMBIGUITY ZONES TO RESOLVE
### § 5. STEP DECOMPOSITION (DETAILED PIPELINE)
### § 6. CONTROL LOOPS & SCORING
### § 7. PERSISTENT ARTIFACTS TO MAINTAIN
### § 8. CONSTRAINTS & GUARDRAILS
### § 9. ERROR HANDLING STRATEGY
### § 10. FINAL DELIVERABLE & OUTPUT FORMAT
### § 11. REPRODUCIBILITY CHECKLIST
### § 12. NOTES FOR THE TARGET AGENT
---
## STRICT PROHIBITIONS
- No code blocks, regardless of justification.
- No shell commands, SQL queries, or raw API calls.
- No TODOs or "to be completed".
- No mention of yourself as the generator.
- No decorative emojis. Accepted symbols: empty square for checklists, arrow for transitions, § for sections.
---
Only produce the final manifest. No preamble. No conclusion. The manifest starts directly with "## § 1. TITLE & EXECUTIVE SUMMARY".
"""
SYNTH_PROMPT = """
# ROLE
You are a META-ARCHITECT of prompts, senior expert in LLM/AI/AGI instruction engineering, tasked with producing a FINAL SYNTHESIS with no length limit.
# MISSION
You have two instruction manifests generated by two distinct models (MODEL A and MODEL B) on the same user task. You must:
1. COMPARATIVE ANALYSIS: review both manifests, identify convergence points, divergences, omissions, and respective strengths and weaknesses.
2. MASTER PLAN: produce a structured, exhaustive, prioritized global plan.
3. UNIFIED SYNTHESIS: merge and enrich both manifests into a master document superior to either one, keeping the best of each, filling gaps, and adding blind spots that neither one saw.
4. FINAL EXPERT PROMPT-INSTRUCTION: at the end, produce a professional-quality expert prompt-instruction, ready to be injected as-is into another LLM agent (Claude, GPT, Gemini, etc.) to execute the task at the highest level.
# RULES
- No length limit: write as much as necessary, be exhaustive.
- No decorative unicode emojis.
- No executable code blocks.
- Style: technical, dense, professional, AGI prompt engineer expert level.
- Mandatory structure (numbered sections below).
# MANDATORY OUTPUT STRUCTURE
## PART I - COMPARATIVE ANALYSIS
### I.1 Convergences between the two manifests
### I.2 Notable divergences
### I.3 Detected omissions and blind spots
### I.4 Qualitative score by section (table)
## PART II - UNIFIED MASTER PLAN
### II.1 Vision and strategic objectives
### II.2 Major phases
### II.3 Dependencies and sequencing
### II.4 Critical risks and mitigation
## PART III - ENRICHED SYNTHETIC MANIFEST
Covers the 12 standard sections (§ 1 to § 12) in a unified, enriched version superior to both inputs.
## PART IV - FINAL EXPERT PROMPT-INSTRUCTION
A turnkey, self-contained prompt requiring no external context, ready to be pasted into any LLM agent to solve the task. Senior prompt engineer level. Include: role, context, constraints, methodology, success criteria, self-verification loops, deliverable format.
## PART V - META NOTES
Strategic advice for the target agent, known pitfalls, possible optimizations.
# INPUTS
## ORIGINAL USER TASK
<<<TASK
{USER_INPUT}
TASK>>>
## MODEL A MANIFEST ({MODEL_A})
<<<MANIFEST_A
{MANIFEST_A}
MANIFEST_A>>>
## MODEL B MANIFEST ({MODEL_B})
<<<MANIFEST_B
{MANIFEST_B}
MANIFEST_B>>>
{MEMORY_CONTEXT}
# START THE SYNTHESIS NOW
Begin directly with "## PART I - COMPARATIVE ANALYSIS". No preamble.
"""
# ----------------------------------------------------------------------
# Persistent memory helpers (thread‑safe with simple lock)
# ----------------------------------------------------------------------
import threading
_memory_lock = threading.Lock()
def load_memory() -> dict:
with _memory_lock:
if not MEMORY_FILE.exists():
return {"sessions": []}
try:
return json.loads(MEMORY_FILE.read_text(encoding="utf-8"))
except json.JSONDecodeError:
logger.warning("Corrupted memory file; resetting.")
return {"sessions": []}
def save_memory(mem: dict) -> None:
with _memory_lock:
MEMORY_FILE.write_text(json.dumps(mem, ensure_ascii=False, indent=2), encoding="utf-8")
def append_session(entry: dict) -> None:
mem = load_memory()
mem.setdefault("sessions", []).append(entry)
# keep only last 50 sessions
mem["sessions"] = mem["sessions"][-50:]
save_memory(mem)
def build_memory_context(max_items: int = 3) -> str:
mem = load_memory()
sessions = mem.get("sessions", [])[-max_items:]
if not sessions:
return ""
lines = ["## MEMORY CONTEXT (previous sessions, for coherence)"]
for s in sessions:
lines.append(f"- [{s.get('timestamp','?')}] Task: {s.get('user_input','')[:200]}")
if s.get("topics"):
lines.append(f" Topics covered: {', '.join(s['topics'])}")
return "\n".join(lines)
def clear_memory():
save_memory({"sessions": []})
logger.info("Memory cleared.")
def view_memory() -> str:
mem = load_memory()
sessions = mem.get("sessions", [])
if not sessions:
return "No sessions in memory."
out = [f"Total sessions: {len(sessions)}", ""]
for i, s in enumerate(reversed(sessions), 1):
out.append(f"### Session {i} - {s.get('timestamp','?')}")
out.append(f"Task: {s.get('user_input','')[:300]}")
if s.get("topics"):
out.append(f"Topics: {', '.join(s['topics'])}")
if s.get("files"):
out.append(f"Files: {', '.join(s['files'])}")
out.append("")
return "\n".join(out)
# ----------------------------------------------------------------------
# Internet detection & web research
# ----------------------------------------------------------------------
_internet_available: Optional[bool] = None
def check_internet(timeout: float = 3.0) -> bool:
global _internet_available
if _internet_available is not None:
return _internet_available
for url in ("https://duckduckgo.com/", "https://www.google.com/"):
try:
requests.head(url, timeout=timeout, allow_redirects=True)
_internet_available = True
return True
except Exception:
continue
_internet_available = False
return False
def web_search_duckduckgo(query: str, max_results: int = 5) -> List[Dict[str, str]]:
try:
resp = requests.get(
"https://html.duckduckgo.com/html/",
params={"q": query},
headers={"User-Agent": "Mozilla/5.0 (compatible; ManifestGen/2.0)"},
timeout=10,
)
resp.raise_for_status()
results = []
for m in re.finditer(
r'<a rel="nofollow" class="result__a" href="([^"]+)"[^>]*>(.*?)</a>',
resp.text,
):
href = m.group(1)
title = re.sub(r"<[^>]+>", "", m.group(2)).strip()
if href and title:
results.append({"title": title, "url": href})
if len(results) >= max_results:
break
snippets = re.findall(
r'<a class="result__snippet"[^>]*>(.*?)</a>', resp.text
)
for i, snip in enumerate(snippets):
if i < len(results):
results[i]["snippet"] = re.sub(r"<[^>]+>", "", snip).strip()
return results
except Exception as e:
logger.debug(f"DuckDuckGo search failed: {e}")
return []
def fetch_page_text(url: str, max_chars: int = 3000) -> str:
try:
resp = requests.get(
url,
headers={"User-Agent": "Mozilla/5.0 (compatible; ManifestGen/2.0)"},
timeout=10,
)
resp.raise_for_status()
text = re.sub(r"<script[^>]*>.*?</script>", "", resp.text, flags=re.DOTALL)
text = re.sub(r"<style[^>]*>.*?</style>", "", text, flags=re.DOTALL)
text = re.sub(r"<[^>]+>", " ", text)
text = re.sub(r"\s+", " ", text).strip()
return text[:max_chars]
except Exception:
return ""
def build_web_context(task: str, topic: str = "", max_results: int = 3, max_pages: int = 2) -> str:
if not check_internet():
return ""
query = task
if topic:
query = f"{topic} {task}"
logger.info(f"Web research: \"{query[:60]}\" ...")
results = web_search_duckduckgo(query, max_results=max_results)
if not results:
logger.info("No web results found.")
return ""
lines = ["## WEB CONTEXT (automatic search, for enrichment)"]
pages_fetched = 0
for r in results:
lines.append(f"- [{r['title']}]({r.get('url', '')})")
if r.get("snippet"):
lines.append(f" Summary: {r['snippet']}")
if pages_fetched < max_pages and r.get("url"):
page_text = fetch_page_text(r["url"])
if page_text:
lines.append(f" Excerpt: {page_text[:800]}")
pages_fetched += 1
logger.info(f"Web context: {len(results)} results, {pages_fetched} pages fetched.")
return "\n".join(lines)
# ----------------------------------------------------------------------
# Ollama interaction — batch and streaming
# ----------------------------------------------------------------------
def query_ollama(
model: str,
prompt: str,
temperature: float = DEFAULT_TEMPERATURE,
num_predict: int = -1,
timeout: int = DEFAULT_TIMEOUT,
ollama_url: str = OLLAMA_URL,
num_ctx: int = 8192,
) -> str:
payload = {
"model": model,
"prompt": prompt,
"stream": False,
"options": {
"temperature": temperature,
"num_predict": num_predict,
"num_ctx": num_ctx,
},
}
try:
resp = requests.post(ollama_url, json=payload, timeout=timeout)
resp.raise_for_status()
data = resp.json()
return data.get("response", "[Empty response]")
except requests.exceptions.ConnectionError:
raise RuntimeError(f"Could not connect to Ollama at {ollama_url}. Is it running?")
except requests.exceptions.Timeout:
raise TimeoutError(f"Timeout while waiting for model {model}.")
except Exception as e:
raise RuntimeError(f"Ollama call to {model} failed: {e}")
def query_ollama_stream(
model: str,
prompt: str,
temperature: float = DEFAULT_TEMPERATURE,
num_predict: int = -1,
timeout: int = DEFAULT_TIMEOUT,
ollama_url: str = OLLAMA_URL,
callback=None,
num_ctx: int = 8192,
):
payload = {
"model": model,
"prompt": prompt,
"stream": True,
"options": {
"temperature": temperature,
"num_predict": num_predict,
"num_ctx": num_ctx,
},
}
full_text = []
try:
with requests.post(ollama_url, json=payload, timeout=timeout, stream=True) as resp:
resp.raise_for_status()
for raw_line in resp.iter_lines():
if not raw_line:
continue
chunk = json.loads(raw_line)
token = chunk.get("response", "")
if token:
full_text.append(token)
if callback:
callback(token)
if chunk.get("done"):
break
except requests.exceptions.ConnectionError:
raise RuntimeError(f"Could not connect to Ollama at {ollama_url}. Is it running?")
except requests.exceptions.Timeout:
raise TimeoutError(f"Timeout while waiting for model {model}.")
except Exception as e:
raise RuntimeError(f"Ollama stream to {model} failed: {e}")
return "".join(full_text)
# ----------------------------------------------------------------------
# Dual-pane streaming for parallel generation
# ----------------------------------------------------------------------
class DualPaneDisplay:
def __init__(self, label_a: str, label_b: str):
self._lock = threading.Lock()
self.label_a = label_a
self.label_b = label_b
self.buf_a: List[str] = []
self.buf_b: List[str] = []
self.lines_a: List[str] = [""]
self.lines_b: List[str] = [""]
term_size = shutil.get_terminal_size((120, 40))
self.width = term_size.columns
self.height = term_size.lines - 4
self.col_w = (self.width - 3) // 2
self._last_drawn = 0
def _tokenize_lines(self, buf: List[str]) -> List[str]:
text = "".join(buf)
raw_lines = text.split("\n")
wrapped = []
for rl in raw_lines:
while len(rl) > self.col_w:
wrapped.append(rl[: self.col_w])
rl = rl[self.col_w :]
wrapped.append(rl)
return wrapped
def feed_a(self, token: str):
with self._lock:
self.buf_a.append(token)
self.lines_a = self._tokenize_lines(self.buf_a)
self._draw()
def feed_b(self, token: str):
with self._lock:
self.buf_b.append(token)
self.lines_b = self._tokenize_lines(self.buf_b)
self._draw()
def _draw(self):
with self._lock:
visible = self.height - 2
la = self.lines_a[-visible:] if len(self.lines_a) > visible else self.lines_a
lb = self.lines_b[-visible:] if len(self.lines_b) > visible else self.lines_b
max_rows = max(len(la), len(lb), 1)
header_a = f" {self.label_a} "
header_b = f" {self.label_b} "
ha = header_a.center(self.col_w, "-")
hb = header_b.center(self.col_w, "-")
output = [f"\033[2J\033[H{ha} | {hb}"]
for i in range(max_rows):
left = la[i] if i < len(la) else ""
right = lb[i] if i < len(lb) else ""
left = left[: self.col_w].ljust(self.col_w)
right = right[: self.col_w].ljust(self.col_w)
output.append(f"{left} | {right}")
sep = "-" * self.col_w + "-+-" + "-" * self.col_w
output.append(sep)
status = f" A: {len(self.lines_a)} lignes | B: {len(self.lines_b)} lignes"
output.append(status)
sys.stdout.write("\n".join(output))
sys.stdout.flush()
def get_texts(self) -> Tuple[str, str]:
return "".join(self.buf_a), "".join(self.buf_b)
def get_technique_boost(technique_ids: List[int]) -> str:
"""Generate prompt enhancement block from selected techniques, grouped by category."""
if not technique_ids:
return ""
by_cat: Dict[str, List[str]] = {}
for tid in sorted(technique_ids):
if tid in TECHNIQUES_DB:
tech = TECHNIQUES_DB[tid]
cat = tech.get("category", "Other")
by_cat.setdefault(cat, []).append(f"- {tech['title']}: {tech['description']}")
if not by_cat:
return ""
blocks = []
for cat_name, items in by_cat.items():
blocks.append(f"### {cat_name}")
blocks.extend(items)
blocks_str = "\n".join(blocks)
if not blocks_str:
return ""
return (
"## PROMPT ENGINEERING TECHNIQUES TO APPLY\n"
"Integrate the following methods into your generation for superior quality:\n"
+ blocks_str
)
def build_full_prompt(
user_input: str,
topic: str = "",
use_memory: bool = True,
techniques: Optional[List[int]] = None,
use_web: bool = True,
) -> str:
topic_block = ""
if topic.strip():
topic_block = (
"## FOCUS TOPIC\n"
f"Focus this entire manifest on the following topic: '{topic.strip()}'. "
"Treat this topic as the dominant angle, going to maximum depth."
)
memory_block = build_memory_context() if use_memory else ""
web_block = build_web_context(user_input, topic) if use_web else ""
techniques_block = get_technique_boost(techniques or DEFAULT_TECHNIQUES)
return (META_PROMPT
.replace("{USER_INPUT}", user_input.strip())
.replace("{TOPIC_FOCUS}", topic_block)
.replace("{MEMORY_CONTEXT}", memory_block)
.replace("{WEB_CONTEXT}", web_block)
.replace("---\n\n## USER INPUT",
f"{techniques_block}\n\n---\n\n## USER INPUT"))
def split_topics(topics_raw: str) -> List[str]:
"""Parse a user‑supplied topics string into a list of non‑empty topics."""
if not topics_raw or not topics_raw.strip():
return []
# split on newline, comma, or semicolon; allow dashes/bullets as decorations
parts = re.split(r"[\n;,]+", topics_raw)
return [p.strip(" -*\t") for p in parts if p.strip(" -*\t")]
# ----------------------------------------------------------------------
# Core generation logic
# ----------------------------------------------------------------------
def generate_multi_topics(
model: str,
user_input: str,
topics_raw: str,
temperature: float,
use_memory: bool,
ollama_url: str,
timeout: int,
techniques: Optional[List[int]] = None,
use_web: bool = True,
stream_callback=None,
) -> str:
if not user_input.strip():
raise ValueError("Task description must not be empty.")
topics = split_topics(topics_raw)
if not topics:
topics = [""]
chunks = []
for idx, topic in enumerate(topics, 1):
label = topic if topic else "global manifest"
logger.info(f"Generating {idx}/{len(topics)} for model {model}: '{label}'")
prompt = build_full_prompt(user_input, topic, use_memory, techniques, use_web)
if stream_callback:
text = query_ollama_stream(model, prompt, temperature, num_predict=-1, timeout=timeout, ollama_url=ollama_url, callback=stream_callback)
else:
text = query_ollama(model, prompt, temperature, num_predict=-1, timeout=timeout, ollama_url=ollama_url)
chunks.append(
f"\n\n{'='*70}\nOUTPUT {idx} / {len(topics)} - TOPIC: {label}\n{'='*70}\n\n{text}"
)
full = "".join(chunks)
append_session({
"timestamp": datetime.now(timezone.utc).isoformat(),
"user_input": user_input,
"model": model,
"topics": [t for t in topics if t],
"mode": "multi_topics",
"techniques": techniques or DEFAULT_TECHNIQUES,
"web_enriched": use_web and check_internet(),
})
return full
def generate_parallel_both(
user_input: str,
topics_raw: str,
temperature: float,
use_memory: bool,
ollama_url: str,
timeout: int,
model_a: str = DEFAULT_MODEL_A,
model_b: str = DEFAULT_MODEL_B,
techniques: Optional[List[int]] = None,
use_web: bool = True,
live_display: bool = False,
) -> Tuple[str, str, Path, Path]:
if not user_input.strip():
raise ValueError("Task description must not be empty.")
session_id = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") + "_" + uuid.uuid4().hex[:6]
if live_display and sys.stdout.isatty():
pane = DualPaneDisplay(model_a, model_b)
def worker_a():
return generate_multi_topics(
model_a, user_input, topics_raw, temperature, use_memory,
ollama_url, timeout, techniques, use_web, stream_callback=pane.feed_a,
)
def worker_b():
return generate_multi_topics(
model_b, user_input, topics_raw, temperature, use_memory,
ollama_url, timeout, techniques, use_web, stream_callback=pane.feed_b,
)
with ThreadPoolExecutor(max_workers=2) as pool:
fa = pool.submit(worker_a)
fb = pool.submit(worker_b)
out_a = fa.result()
out_b = fb.result()
print("\n")
else:
def worker(model):
return generate_multi_topics(model, user_input, topics_raw, temperature, use_memory, ollama_url, timeout, techniques, use_web)
with ThreadPoolExecutor(max_workers=2) as pool:
fa = pool.submit(worker, model_a)
fb = pool.submit(worker, model_b)
out_a = fa.result()
out_b = fb.result()
file_a = OUTPUT_DIR / f"{session_id}_{model_a.replace(':','_')}.md"
file_b = OUTPUT_DIR / f"{session_id}_{model_b.replace(':','_')}.md"
header_a = f"# Manifest -- {model_a}\n\nTask: {user_input}\nTopics: {topics_raw or '(global)'}\nGenerated: {session_id}\n\n---\n"
header_b = f"# Manifest -- {model_b}\n\nTask: {user_input}\nTopics: {topics_raw or '(global)'}\nGenerated: {session_id}\n\n---\n"
file_a.write_text(header_a + out_a, encoding="utf-8")
file_b.write_text(header_b + out_b, encoding="utf-8")
logger.info(f"Parallel output written to {file_a} and {file_b}")
append_session({
"timestamp": datetime.now(timezone.utc).isoformat(),
"user_input": user_input,
"session_id": session_id,
"topics": split_topics(topics_raw),
"mode": "parallel_both",
"files": [str(file_a.name), str(file_b.name)],
"techniques": techniques or DEFAULT_TECHNIQUES,
"web_enriched": use_web and check_internet(),
})
return out_a, out_b, file_a, file_b
def run_synthesis(
user_input: str,
manifest_a: str,
manifest_b: str,
temperature: float,
use_memory: bool,
ollama_url: str,
timeout: int,
synthesis_model: str = DEFAULT_SYNTH_MODEL,
techniques: Optional[List[int]] = None,
stream: bool = False,