-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft_engine.py
More file actions
56 lines (42 loc) · 1.67 KB
/
draft_engine.py
File metadata and controls
56 lines (42 loc) · 1.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
"""
draft_engine.py — Parallel Draft Generation
Handles multi-temperature draft generation and editor selection.
"""
import concurrent.futures
from typing import Optional
from config import WRITER_MODEL
from ollama_client import call_ollama
from prompts import select_best_draft
from logger import logger
def generate_parallel_drafts(system_context: str, user_prompt: str) -> Optional[str]:
"""Generates 3 drafts with different temperatures and selects the best one."""
messages = [
{"role": "system", "content": system_context},
{"role": "user", "content": user_prompt}
]
# Temperatures: 0.7 (Safe), 0.9 (Creative), 1.1 (Chaotic/Innovative)
temps = [0.7, 0.9, 1.1]
logger.info(f"Drafting 3 variants in parallel (Temps: {temps})...")
drafts = []
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
futures = [
executor.submit(call_ollama, messages, WRITER_MODEL, False, 32768, None, t)
for t in temps
]
for f in concurrent.futures.as_completed(futures):
res = f.result()
if res:
drafts.append(res)
if not drafts:
return None
if len(drafts) == 1:
return drafts[0]
logger.info(f"Evaluating {len(drafts)} drafts via Editor-in-Chief...")
selection = select_best_draft(drafts)
idx = selection.get("best_draft_index", 1) - 1
reason = selection.get("reasoning", "No valid reason provided.")
# Safety Check
if idx < 0 or idx >= len(drafts):
idx = 0
logger.info(f"Selected Draft {idx+1}: {reason[:100]}...")
return drafts[idx]