Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,321 +1,458 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
-
import
|
| 5 |
-
import dagshub
|
| 6 |
-
import uuid
|
| 7 |
-
|
| 8 |
-
# DagsHub Verbindung
|
| 9 |
-
dagshub.init(repo_owner='homuhe', repo_name='PromptPlenum-Tracking', mlflow=True)
|
| 10 |
-
|
| 11 |
-
# Das magische Autologging fΓΌr alle OpenAI-kompatiblen Aufrufe!
|
| 12 |
-
mlflow.openai.autolog()
|
| 13 |
|
| 14 |
# ==========================================
|
| 15 |
-
# 1. KONFIGURATION &
|
| 16 |
# ==========================================
|
| 17 |
|
| 18 |
MODERATOR_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"model": "openai/gpt-oss-120b",
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
- Liefere das logische GerΓΌst fΓΌr die LΓΆsung der Aufgabe.
|
| 27 |
-
- Antworte in 2-4 SΓ€tzen, professionell und bodenstΓ€ndig."""
|
| 28 |
},
|
| 29 |
-
|
|
|
|
| 30 |
"model": "deepseek-ai/DeepSeek-V3.2",
|
| 31 |
-
"
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
},
|
| 37 |
-
|
|
|
|
| 38 |
"model": "meta-llama/Llama-4-Maverick-17B-128E-Instruct",
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
class PromptManager:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
@staticmethod
|
| 51 |
-
def
|
| 52 |
return (
|
| 53 |
-
"Du bist
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
|
|
|
| 57 |
)
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
@staticmethod
|
| 60 |
-
def
|
| 61 |
return (
|
| 62 |
-
"Du bist
|
| 63 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
@staticmethod
|
| 67 |
-
def
|
|
|
|
| 68 |
return (
|
| 69 |
-
f"{
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
)
|
| 73 |
|
| 74 |
@staticmethod
|
| 75 |
-
def
|
| 76 |
-
if not discussion_history:
|
| 77 |
-
return f"Auftrag: '{user_prompt}'"
|
| 78 |
return (
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
"
|
| 83 |
-
"
|
| 84 |
-
"3. Komm direkt zum Punkt."
|
| 85 |
)
|
| 86 |
|
| 87 |
@staticmethod
|
| 88 |
-
def
|
| 89 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
@staticmethod
|
| 92 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
return (
|
| 94 |
-
f"
|
| 95 |
-
"
|
| 96 |
-
"
|
|
|
|
| 97 |
)
|
| 98 |
|
| 99 |
@staticmethod
|
| 100 |
-
def
|
| 101 |
return (
|
| 102 |
-
"Du bist ein
|
| 103 |
-
"
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
|
| 106 |
@staticmethod
|
| 107 |
-
def
|
| 108 |
return (
|
| 109 |
-
f"Auftrag:
|
| 110 |
-
"
|
| 111 |
-
"
|
| 112 |
-
"
|
| 113 |
-
"3. Wenn es Code ist: Liefere sauberen Code ohne Marketing-Sprech.\n"
|
| 114 |
-
"4. ABSOLUTES VERBOT: Generiere NIEMALS Bild-Platzhalter (wie '[Bild von...]') in den Text. Der Text muss 1:1 copy-paste-fertig sein.\n"
|
| 115 |
-
"5. Verarbeite alle Fakten aus dem Konsens."
|
| 116 |
-
|
| 117 |
)
|
| 118 |
|
|
|
|
| 119 |
# ==========================================
|
| 120 |
-
#
|
| 121 |
# ==========================================
|
|
|
|
| 122 |
class LLMService:
|
| 123 |
-
"""Kapselt die API-Kommunikation ΓΌber das OpenAI SDK (verbunden mit HF Router)"""
|
| 124 |
def __init__(self):
|
| 125 |
-
# Wir nutzen den OpenAI Client, leiten ihn aber an Hugging Face um!
|
| 126 |
self.client = OpenAI(
|
| 127 |
-
base_url="https://router.huggingface.co/v1",
|
| 128 |
-
api_key=os.getenv("HF_TOKEN")
|
| 129 |
)
|
| 130 |
|
| 131 |
-
|
| 132 |
-
def ask(self, model_id, system_prompt, user_input, session_id, role_name):
|
| 133 |
-
|
| 134 |
-
# FΓΌgt Meta-Daten zum Trace hinzu, damit du in DagsHub filtern kannst
|
| 135 |
-
mlflow.update_current_trace(tags={
|
| 136 |
-
"session_id": session_id,
|
| 137 |
-
"expert_role": role_name,
|
| 138 |
-
"model_name": model_id
|
| 139 |
-
})
|
| 140 |
-
|
| 141 |
messages = [
|
| 142 |
{"role": "system", "content": system_prompt},
|
| 143 |
-
{"role": "user", "content": user_input}
|
| 144 |
]
|
| 145 |
-
response = ""
|
| 146 |
try:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
stream=True
|
| 154 |
)
|
| 155 |
-
|
| 156 |
-
if chunk.choices[0].delta.content is not None:
|
| 157 |
-
response += chunk.choices[0].delta.content
|
| 158 |
-
return response
|
| 159 |
except Exception as e:
|
| 160 |
-
return f"π¨
|
|
|
|
|
|
|
| 161 |
class UIHelper:
|
| 162 |
-
"""Formatiert Ausgaben fΓΌr die Gradio Chatbot-UI."""
|
| 163 |
@staticmethod
|
| 164 |
def header(title, color="#FF5A4D"):
|
| 165 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
@staticmethod
|
| 168 |
-
def
|
| 169 |
-
return f"
|
|
|
|
| 170 |
|
| 171 |
# ==========================================
|
| 172 |
-
#
|
| 173 |
# ==========================================
|
|
|
|
| 174 |
class PlenumOrchestrator:
|
| 175 |
-
"""Steuert den gesamten Diskussions- und Generierungsprozess."""
|
| 176 |
def __init__(self):
|
| 177 |
self.llm = LLMService()
|
| 178 |
-
self.
|
| 179 |
self.ui = UIHelper()
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
def run(self, user_prompt, rounds):
|
| 182 |
-
if not user_prompt:
|
| 183 |
-
yield [{"role": "assistant", "content": "Bitte gib
|
| 184 |
return
|
| 185 |
-
|
| 186 |
-
# Generiert die Session ID fΓΌr das spΓ€tere BΓΌndeln in MLflow!
|
| 187 |
-
current_session_id = str(uuid.uuid4())
|
| 188 |
|
| 189 |
history = [{"role": "user", "content": user_prompt}]
|
| 190 |
yield history
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
yield history
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
MODERATOR_MODEL,
|
| 200 |
-
self.
|
| 201 |
-
|
| 202 |
-
current_session_id, # <-- NEU hinzugefΓΌgt
|
| 203 |
-
"Moderator_Kickoff" # <-- NEU hinzugefΓΌgt
|
| 204 |
)
|
| 205 |
-
|
| 206 |
-
history.append({"role": "assistant", "content": self.ui.message("π€ Moderator", kickoff_res, "#FF5A4D")})
|
| 207 |
yield history
|
| 208 |
|
| 209 |
-
#
|
| 210 |
for r in range(int(rounds)):
|
| 211 |
-
history.append({
|
|
|
|
|
|
|
|
|
|
| 212 |
yield history
|
| 213 |
-
|
| 214 |
-
# Moderator
|
| 215 |
-
if r > 0:
|
| 216 |
-
|
| 217 |
-
MODERATOR_MODEL,
|
| 218 |
-
self.
|
| 219 |
-
|
| 220 |
-
current_session_id, # <-- NEU hinzugefΓΌgt
|
| 221 |
-
"Moderator_Steuerung" # <-- NEU hinzugefΓΌgt
|
| 222 |
)
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
| 225 |
yield history
|
| 226 |
|
| 227 |
-
# Experten
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
config["model"],
|
| 234 |
-
sys_msg,
|
| 235 |
-
user_msg,
|
| 236 |
-
current_session_id,
|
| 237 |
-
name
|
| 238 |
-
)
|
| 239 |
-
discussion_history += f"{name}: {answer}\n\n"
|
| 240 |
-
|
| 241 |
-
history.append({"role": "assistant", "content": self.ui.message(name, answer)})
|
| 242 |
-
yield history
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
#
|
| 259 |
history.append({"role": "assistant", "content": self.ui.header("π FINALE AUSGABE")})
|
| 260 |
yield history
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
MODERATOR_MODEL,
|
| 264 |
-
self.
|
| 265 |
-
self.
|
| 266 |
-
current_session_id, # <-- NEU hinzugefΓΌgt
|
| 267 |
-
"Moderator_Finale" # <-- NEU hinzugefΓΌgt
|
| 268 |
)
|
| 269 |
-
history.append({"role": "assistant", "content":
|
| 270 |
yield history
|
| 271 |
|
| 272 |
-
|
|
|
|
| 273 |
orchestrator = PlenumOrchestrator()
|
| 274 |
|
|
|
|
| 275 |
# ==========================================
|
| 276 |
-
#
|
| 277 |
# ==========================================
|
|
|
|
| 278 |
v_theme = gr.themes.Soft(
|
| 279 |
-
primary_hue="indigo",
|
| 280 |
-
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 281 |
).set(
|
| 282 |
-
button_primary_background_fill="#4241A6",
|
| 283 |
button_primary_background_fill_hover="#2D2C73",
|
| 284 |
button_primary_text_color="white",
|
| 285 |
-
block_title_text_color="#FF5A4D",
|
| 286 |
block_label_text_color="#4241A6",
|
| 287 |
body_text_color="#1F2937",
|
| 288 |
-
color_accent_soft="#FFEBE8",
|
| 289 |
)
|
| 290 |
|
| 291 |
with gr.Blocks(theme=v_theme) as demo:
|
| 292 |
gr.HTML("""
|
| 293 |
-
<div style="text-align:
|
| 294 |
-
<h1 style="color:
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
</div>
|
| 297 |
""")
|
| 298 |
|
| 299 |
with gr.Row():
|
| 300 |
with gr.Column(scale=4):
|
| 301 |
input_text = gr.Textbox(
|
| 302 |
-
label="Plenumsauftrag",
|
| 303 |
-
placeholder=
|
| 304 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
)
|
| 306 |
with gr.Column(scale=1):
|
| 307 |
-
rounds_slider = gr.Slider(
|
| 308 |
-
|
|
|
|
|
|
|
| 309 |
with gr.Row():
|
| 310 |
start_btn = gr.Button("Sitzung starten", variant="primary", size="lg")
|
| 311 |
-
clear_btn = gr.ClearButton(
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
clear_btn.add(chatbot)
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
if __name__ == "__main__":
|
| 321 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
+
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# ==========================================
|
| 7 |
+
# 1. KONFIGURATION & AUFGABEN-PROFILE
|
| 8 |
# ==========================================
|
| 9 |
|
| 10 |
MODERATOR_MODEL = "meta-llama/Llama-3.3-70B-Instruct"
|
| 11 |
|
| 12 |
+
# Jedes Profil definiert, wie Experten denken und was am Ende rauskommt
|
| 13 |
+
TASK_PROFILES = {
|
| 14 |
+
"CODE": {
|
| 15 |
+
"label": "π» Code / Technisch",
|
| 16 |
+
"expert_focuses": [
|
| 17 |
+
"Architektur, Algorithmus & Gesamtstruktur des Codes",
|
| 18 |
+
"Edge-Cases, Fehlerbehandlung & Sicherheit",
|
| 19 |
+
"Saubere, idiomatische Implementierung & Lesbarkeit",
|
| 20 |
+
],
|
| 21 |
+
"final_instruction": (
|
| 22 |
+
"Liefere AUSSCHLIESSLICH den fertigen, lauffΓ€higen Code. "
|
| 23 |
+
"Inline-Kommentare nur wo nΓΆtig. Kein FlieΓtext drumherum. "
|
| 24 |
+
"Korrekter Code-Block fΓΌr die jeweilige Sprache."
|
| 25 |
+
),
|
| 26 |
+
"draft_label": "Aktueller Code-Entwurf",
|
| 27 |
+
},
|
| 28 |
+
"TEXT": {
|
| 29 |
+
"label": "βοΈ Text / Content",
|
| 30 |
+
"expert_focuses": [
|
| 31 |
+
"Argumentationsstruktur, roter Faden & Aufbau",
|
| 32 |
+
"Ton, Zielgruppe, Fakten & fehlende Tiefe",
|
| 33 |
+
"Formulierung, Wirkung & plattformgerechtes Format",
|
| 34 |
+
],
|
| 35 |
+
"final_instruction": (
|
| 36 |
+
"Liefere den fertigen, copy-paste-fΓ€higen Text. "
|
| 37 |
+
"Passe LΓ€nge, Ton und Format EXAKT an die Plattform an "
|
| 38 |
+
"(z.B. LinkedIn: kurze AbsΓ€tze, mobile-freundlich, sinnvolle Emojis). "
|
| 39 |
+
"Keine Platzhalter, keine KI-Floskeln, kein Meta-Kommentar."
|
| 40 |
+
),
|
| 41 |
+
"draft_label": "Aktueller Text-Entwurf",
|
| 42 |
+
},
|
| 43 |
+
"PLAN": {
|
| 44 |
+
"label": "π Strategie / Plan",
|
| 45 |
+
"expert_focuses": [
|
| 46 |
+
"Gesamtstrategie, Phasen & logische Abfolge",
|
| 47 |
+
"Risiken, AbhΓ€ngigkeiten, KPIs & blinde Flecken",
|
| 48 |
+
"Konkrete MaΓnahmen, Verantwortlichkeiten & Zeitplan",
|
| 49 |
+
],
|
| 50 |
+
"final_instruction": (
|
| 51 |
+
"Liefere einen klaren, strukturierten Aktionsplan. "
|
| 52 |
+
"Phasen, Meilensteine, MaΓnahmen. Tabellen wo sinnvoll. "
|
| 53 |
+
"Direkt umsetzbar, keine abstrakten WorthΓΌlsen."
|
| 54 |
+
),
|
| 55 |
+
"draft_label": "Aktueller Plan-Entwurf",
|
| 56 |
+
},
|
| 57 |
+
"ANALYSIS": {
|
| 58 |
+
"label": "π Analyse / Konzept",
|
| 59 |
+
"expert_focuses": [
|
| 60 |
+
"Problemstruktur, Hypothesen & Analyserahmen",
|
| 61 |
+
"Daten, Belege, Gegenargumente & LΓΌcken",
|
| 62 |
+
"Schlussfolgerungen, Empfehlungen & Priorisierung",
|
| 63 |
+
],
|
| 64 |
+
"final_instruction": (
|
| 65 |
+
"Liefere eine strukturierte Analyse: Befunde β Bewertung β Empfehlung. "
|
| 66 |
+
"Faktenbasiert, prΓ€zise, keine leeren Phrasen."
|
| 67 |
+
),
|
| 68 |
+
"draft_label": "Aktuelle Analyse",
|
| 69 |
+
},
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
COUNCIL_MEMBERS = [
|
| 73 |
+
{
|
| 74 |
+
"name": "π§ Experte I",
|
| 75 |
"model": "openai/gpt-oss-120b",
|
| 76 |
+
"tag": "STRUKTUR",
|
| 77 |
+
"color": "#1a6b3c",
|
| 78 |
+
"role_hint": "Erstelle oder verbessere die Grundstruktur und das logische GerΓΌst.",
|
|
|
|
|
|
|
| 79 |
},
|
| 80 |
+
{
|
| 81 |
+
"name": "π§ Experte II",
|
| 82 |
"model": "deepseek-ai/DeepSeek-V3.2",
|
| 83 |
+
"tag": "KRITIK",
|
| 84 |
+
"color": "#7c3aed",
|
| 85 |
+
"role_hint": (
|
| 86 |
+
"PrΓΌfe den Entwurf von [STRUKTUR] kritisch. "
|
| 87 |
+
"Finde konkrete Fehler, LΓΌcken und SchwΓ€chen β und behebe sie direkt im Entwurf."
|
| 88 |
+
),
|
| 89 |
},
|
| 90 |
+
{
|
| 91 |
+
"name": "π οΈ Experte III",
|
| 92 |
"model": "meta-llama/Llama-4-Maverick-17B-128E-Instruct",
|
| 93 |
+
"tag": "UMSETZUNG",
|
| 94 |
+
"color": "#b45309",
|
| 95 |
+
"role_hint": (
|
| 96 |
+
"Nimm den von [KRITIK] ΓΌberarbeiteten Entwurf und bringe ihn zur Serienreife. "
|
| 97 |
+
"SchΓ€rfe die Formulierungen, vervollstΓ€ndige fehlende Teile, sorge fΓΌr Konsistenz."
|
| 98 |
+
),
|
| 99 |
+
},
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ==========================================
|
| 104 |
+
# 2. PROMPT MANAGER
|
| 105 |
+
# ==========================================
|
| 106 |
|
| 107 |
class PromptManager:
|
| 108 |
+
|
|
|
|
| 109 |
@staticmethod
|
| 110 |
+
def task_detection_sys():
|
| 111 |
return (
|
| 112 |
+
"Du bist ein prΓ€ziser Aufgaben-Klassifikator. Analysiere die Anfrage und antworte NUR mit einem JSON-Objekt.\n"
|
| 113 |
+
"GΓΌltige task_type-Werte: CODE, TEXT, PLAN, ANALYSIS\n"
|
| 114 |
+
"Format (keine weiteren Zeichen auΓerhalb):\n"
|
| 115 |
+
'{"task_type": "...", "core_goal": "Ein-Satz-Beschreibung des Ziels", '
|
| 116 |
+
'"key_constraints": ["Constraint 1", "Constraint 2"]}'
|
| 117 |
)
|
| 118 |
+
|
| 119 |
+
@staticmethod
|
| 120 |
+
def task_detection_user(user_prompt):
|
| 121 |
+
return f"Aufgabe: {user_prompt}"
|
| 122 |
+
|
| 123 |
@staticmethod
|
| 124 |
+
def moderator_kickoff_sys(task_profile):
|
| 125 |
return (
|
| 126 |
+
f"Du bist Lead-Moderator eines Expertenrats. Aufgabentyp: {task_profile['label']}.\n"
|
| 127 |
+
"Brief das Team in genau 4 Punkten (je 1 Satz):\n"
|
| 128 |
+
"1. Konkretes Ziel\n"
|
| 129 |
+
"2. Wichtigste QualitΓ€tskriterien fΓΌr das Endprodukt\n"
|
| 130 |
+
"3. GrΓΆΓte Risiken / hΓ€ufigste Fehler bei diesem Aufgabentyp\n"
|
| 131 |
+
"4. Erwartetes Format des Endprodukts\n"
|
| 132 |
+
"Kein Smalltalk. Direkt. Ohne Anrede."
|
| 133 |
)
|
| 134 |
|
| 135 |
@staticmethod
|
| 136 |
+
def moderator_kickoff_user(user_prompt, task_info):
|
| 137 |
+
constraints = ", ".join(task_info.get("key_constraints", [])) or "keine"
|
| 138 |
return (
|
| 139 |
+
f"Auftrag: '{user_prompt}'\n"
|
| 140 |
+
f"Kernziel: {task_info.get('core_goal', '')}\n"
|
| 141 |
+
f"Constraints: {constraints}"
|
| 142 |
)
|
| 143 |
|
| 144 |
@staticmethod
|
| 145 |
+
def moderator_steering_sys():
|
|
|
|
|
|
|
| 146 |
return (
|
| 147 |
+
"Du bist Lead-Moderator. Bewerte den aktuellen Entwurf knapp und gib dann "
|
| 148 |
+
"EINEN einzigen, konkreten Arbeitsauftrag fΓΌr die nΓ€chste Runde.\n"
|
| 149 |
+
"Format:\n"
|
| 150 |
+
"STAND: [Was gut ist β 1 Satz]\n"
|
| 151 |
+
"AUFTRAG: [Was als nΓ€chstes konkret zu tun ist β 1-2 SΓ€tze, so spezifisch wie mΓΆglich]"
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
@staticmethod
|
| 155 |
+
def moderator_steering_user(current_draft, round_num):
|
| 156 |
+
return (
|
| 157 |
+
f"Aktueller Entwurf nach Runde {round_num}:\n\n{current_draft}\n\n"
|
| 158 |
+
"Gib Steuerungsanweisung fΓΌr Runde {next_round}.".replace(
|
| 159 |
+
"{next_round}", str(round_num + 1)
|
| 160 |
+
)
|
| 161 |
+
)
|
| 162 |
|
| 163 |
@staticmethod
|
| 164 |
+
def expert_sys(expert, task_profile, focus_area, round_num):
|
| 165 |
+
tag = expert["tag"]
|
| 166 |
+
return (
|
| 167 |
+
f"Du bist Experte [{tag}] in einem iterativen Expertenrat.\n\n"
|
| 168 |
+
f"DEIN FOKUS in dieser Runde: {focus_area}\n"
|
| 169 |
+
f"DEINE ROLLE [{tag}]: {expert['role_hint']}\n\n"
|
| 170 |
+
f"REGELN:\n"
|
| 171 |
+
f"- Beginne mit '[{tag}] '\n"
|
| 172 |
+
f"- Du lieferst den {task_profile['draft_label']} β nicht einen Kommentar darΓΌber.\n"
|
| 173 |
+
f"- Verbessere den Entwurf direkt. Kein 'Ich wΓΌrde vorschlagen...'\n"
|
| 174 |
+
f"- Keine Wiederholung des Auftrags, kein Meta-Kommentar am Ende.\n"
|
| 175 |
+
f"- Runde {round_num}: {'Erstelle die erste Version.' if round_num == 1 and tag == 'STRUKTUR' else 'Baue auf dem bestehenden Entwurf auf.'}"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
@staticmethod
|
| 179 |
+
def expert_user(user_prompt, current_draft, steering_instruction, expert_tag):
|
| 180 |
+
draft_block = (
|
| 181 |
+
f"\n\nAKTUELLER ENTWURF (verbessere diesen direkt):\n---\n{current_draft}\n---"
|
| 182 |
+
if current_draft
|
| 183 |
+
else "\n\nEs gibt noch keinen Entwurf. Erstelle die erste Version."
|
| 184 |
+
)
|
| 185 |
+
steering_block = (
|
| 186 |
+
f"\n\nMODERATOR-ANWEISUNG: {steering_instruction}"
|
| 187 |
+
if steering_instruction
|
| 188 |
+
else ""
|
| 189 |
+
)
|
| 190 |
return (
|
| 191 |
+
f"Auftrag: '{user_prompt}'"
|
| 192 |
+
f"{draft_block}"
|
| 193 |
+
f"{steering_block}\n\n"
|
| 194 |
+
f"Liefere jetzt den verbesserten {expert_tag}-Entwurf:"
|
| 195 |
)
|
| 196 |
|
| 197 |
@staticmethod
|
| 198 |
+
def final_sys(task_profile):
|
| 199 |
return (
|
| 200 |
+
f"Du bist ein Output-Finisher fΓΌr {task_profile['label']}-Aufgaben.\n\n"
|
| 201 |
+
f"ANWEISUNG:\n{task_profile['final_instruction']}\n\n"
|
| 202 |
+
"Nutze den vorliegenden Entwurf als Basis und liefere das polierte Endprodukt. "
|
| 203 |
+
"Kein einleitender Satz, kein abschlieΓender Kommentar β nur das Produkt."
|
| 204 |
)
|
| 205 |
|
| 206 |
@staticmethod
|
| 207 |
+
def final_user(user_prompt, best_draft, task_info):
|
| 208 |
return (
|
| 209 |
+
f"UrsprΓΌnglicher Auftrag: '{user_prompt}'\n"
|
| 210 |
+
f"Kernziel: {task_info.get('core_goal', '')}\n\n"
|
| 211 |
+
f"Bester Entwurf aus der Diskussion:\n---\n{best_draft}\n---\n\n"
|
| 212 |
+
"Erstelle das finale, sofort nutzbare Endprodukt:"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
)
|
| 214 |
|
| 215 |
+
|
| 216 |
# ==========================================
|
| 217 |
+
# 3. LLM SERVICE & UI HELPER
|
| 218 |
# ==========================================
|
| 219 |
+
|
| 220 |
class LLMService:
|
|
|
|
| 221 |
def __init__(self):
|
|
|
|
| 222 |
self.client = OpenAI(
|
| 223 |
+
base_url="https://router.huggingface.co/v1",
|
| 224 |
+
api_key=os.getenv("HF_TOKEN"),
|
| 225 |
)
|
| 226 |
|
| 227 |
+
def ask(self, model_id, system_prompt, user_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
messages = [
|
| 229 |
{"role": "system", "content": system_prompt},
|
| 230 |
+
{"role": "user", "content": user_input},
|
| 231 |
]
|
|
|
|
| 232 |
try:
|
| 233 |
+
response = self.client.chat.completions.create(
|
| 234 |
+
model=model_id,
|
| 235 |
+
messages=messages,
|
| 236 |
+
max_tokens=4000,
|
| 237 |
+
temperature=0.4,
|
| 238 |
+
stream=False,
|
|
|
|
| 239 |
)
|
| 240 |
+
return response.choices[0].message.content or ""
|
|
|
|
|
|
|
|
|
|
| 241 |
except Exception as e:
|
| 242 |
+
return f"π¨ Fehler ({model_id}): {str(e)}"
|
| 243 |
+
|
| 244 |
+
|
| 245 |
class UIHelper:
|
|
|
|
| 246 |
@staticmethod
|
| 247 |
def header(title, color="#FF5A4D"):
|
| 248 |
+
return (
|
| 249 |
+
f"<h2 style='color:{color}; border-bottom:2px solid #FFEBE8; "
|
| 250 |
+
f"padding-bottom:5px; margin-top:20px;'>{title}</h2>"
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
@staticmethod
|
| 254 |
+
def message(label, content, color="#4241A6"):
|
| 255 |
+
return f"**<span style='color:{color}; font-size:1.05em;'>{label}</span>**\n\n{content}"
|
| 256 |
|
| 257 |
@staticmethod
|
| 258 |
+
def info(content):
|
| 259 |
+
return f"> βΉοΈ {content}"
|
| 260 |
+
|
| 261 |
|
| 262 |
# ==========================================
|
| 263 |
+
# 4. ORCHESTRATOR
|
| 264 |
# ==========================================
|
| 265 |
+
|
| 266 |
class PlenumOrchestrator:
|
|
|
|
| 267 |
def __init__(self):
|
| 268 |
self.llm = LLMService()
|
| 269 |
+
self.pm = PromptManager()
|
| 270 |
self.ui = UIHelper()
|
| 271 |
|
| 272 |
+
def _detect_task(self, user_prompt):
|
| 273 |
+
raw = self.llm.ask(
|
| 274 |
+
MODERATOR_MODEL,
|
| 275 |
+
self.pm.task_detection_sys(),
|
| 276 |
+
self.pm.task_detection_user(user_prompt),
|
| 277 |
+
)
|
| 278 |
+
try:
|
| 279 |
+
clean = raw.strip().removeprefix("```json").removeprefix("```").removesuffix("```").strip()
|
| 280 |
+
return json.loads(clean)
|
| 281 |
+
except Exception:
|
| 282 |
+
return {"task_type": "TEXT", "core_goal": user_prompt, "key_constraints": []}
|
| 283 |
+
|
| 284 |
def run(self, user_prompt, rounds):
|
| 285 |
+
if not user_prompt.strip():
|
| 286 |
+
yield [{"role": "assistant", "content": "Bitte gib einen Auftrag ein."}]
|
| 287 |
return
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
history = [{"role": "user", "content": user_prompt}]
|
| 290 |
yield history
|
| 291 |
+
|
| 292 |
+
# Zustand: Das ist das HerzstΓΌck des neuen Ansatzes
|
| 293 |
+
current_draft = ""
|
| 294 |
+
steering_instruction = ""
|
| 295 |
+
|
| 296 |
+
# ββ SCHRITT 0: AUFGABE ERKENNEN ββββββββββββββββββββββββββββββ
|
| 297 |
+
history.append({"role": "assistant", "content": self.ui.header("π AUFGABENANALYSE", "#6b7280")})
|
| 298 |
+
yield history
|
| 299 |
+
|
| 300 |
+
task_info = self._detect_task(user_prompt)
|
| 301 |
+
task_type = task_info.get("task_type", "TEXT")
|
| 302 |
+
task_profile = TASK_PROFILES.get(task_type, TASK_PROFILES["TEXT"])
|
| 303 |
+
|
| 304 |
+
detection_text = (
|
| 305 |
+
f"**Erkannter Typ:** {task_profile['label']} \n"
|
| 306 |
+
f"**Kernziel:** {task_info.get('core_goal', 'β')} \n"
|
| 307 |
+
f"**Constraints:** {', '.join(task_info.get('key_constraints', ['β'])) or 'β'}"
|
| 308 |
+
)
|
| 309 |
+
history.append({"role": "assistant", "content": detection_text})
|
| 310 |
+
yield history
|
| 311 |
+
|
| 312 |
+
# ββ SCHRITT 1: KICK-OFF βββββββββββββββββββββββββββββββββββββββ
|
| 313 |
+
history.append({"role": "assistant", "content": self.ui.header("π€ MODERATOR: SITZUNGSERΓFFNUNG")})
|
| 314 |
yield history
|
| 315 |
+
|
| 316 |
+
kickoff = self.llm.ask(
|
| 317 |
+
MODERATOR_MODEL,
|
| 318 |
+
self.pm.moderator_kickoff_sys(task_profile),
|
| 319 |
+
self.pm.moderator_kickoff_user(user_prompt, task_info),
|
|
|
|
|
|
|
| 320 |
)
|
| 321 |
+
history.append({"role": "assistant", "content": self.ui.message("π€ Moderator", kickoff, "#FF5A4D")})
|
|
|
|
| 322 |
yield history
|
| 323 |
|
| 324 |
+
# ββ SCHRITT 2: ZYKLEN βββββββββββββββββββββββββββββββββββββββββ
|
| 325 |
for r in range(int(rounds)):
|
| 326 |
+
history.append({
|
| 327 |
+
"role": "assistant",
|
| 328 |
+
"content": self.ui.header(f"π ZYKLUS {r + 1} β EXPERTENDEBATTE", "#4241A6"),
|
| 329 |
+
})
|
| 330 |
yield history
|
| 331 |
+
|
| 332 |
+
# Moderator-Steuerung (ab Runde 2, basierend auf aktuellem Entwurf)
|
| 333 |
+
if r > 0 and current_draft:
|
| 334 |
+
steering_instruction = self.llm.ask(
|
| 335 |
+
MODERATOR_MODEL,
|
| 336 |
+
self.pm.moderator_steering_sys(),
|
| 337 |
+
self.pm.moderator_steering_user(current_draft, r),
|
|
|
|
|
|
|
| 338 |
)
|
| 339 |
+
history.append({
|
| 340 |
+
"role": "assistant",
|
| 341 |
+
"content": self.ui.message("π€ Moderator (Steuerung)", steering_instruction, "#FF5A4D"),
|
| 342 |
+
})
|
| 343 |
yield history
|
| 344 |
|
| 345 |
+
# Experten arbeiten SEQUENZIELL:
|
| 346 |
+
# Experte I sieht aktuellen Entwurf β produziert Draft A
|
| 347 |
+
# Experte II sieht Draft A β produziert Draft B (mit Korrekturen)
|
| 348 |
+
# Experte III sieht Draft B β produziert Draft C (serienreif)
|
| 349 |
+
# β Draft C wird zum current_draft fΓΌr die nΓ€chste Runde
|
| 350 |
+
round_draft = current_draft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
for idx, expert in enumerate(COUNCIL_MEMBERS):
|
| 353 |
+
focus = task_profile["expert_focuses"][idx]
|
| 354 |
+
|
| 355 |
+
sys_msg = self.pm.expert_sys(expert, task_profile, focus, r + 1)
|
| 356 |
+
usr_msg = self.pm.expert_user(user_prompt, round_draft, steering_instruction if r > 0 else "", expert["tag"])
|
| 357 |
+
|
| 358 |
+
answer = self.llm.ask(expert["model"], sys_msg, usr_msg)
|
| 359 |
+
|
| 360 |
+
# Dieser Experte liefert den Entwurf fΓΌr den NΓ€chsten
|
| 361 |
+
round_draft = answer
|
| 362 |
+
|
| 363 |
+
label = f"{expert['name']} [{expert['tag']}] β {focus}"
|
| 364 |
+
history.append({"role": "assistant", "content": self.ui.message(label, answer, expert["color"])})
|
| 365 |
+
yield history
|
| 366 |
+
|
| 367 |
+
# Bester Stand dieser Runde = Output von Experte III
|
| 368 |
+
current_draft = round_draft
|
| 369 |
+
history.append({
|
| 370 |
+
"role": "assistant",
|
| 371 |
+
"content": self.ui.info(f"Zyklus {r + 1} abgeschlossen. Entwurf gesichert β Basis fΓΌr nΓ€chste Runde."),
|
| 372 |
+
})
|
| 373 |
+
yield history
|
| 374 |
|
| 375 |
+
# ββ SCHRITT 3: FINALE AUSGABE βββββββββββββββββββββββββββββββββ
|
| 376 |
history.append({"role": "assistant", "content": self.ui.header("π FINALE AUSGABE")})
|
| 377 |
yield history
|
| 378 |
+
|
| 379 |
+
final = self.llm.ask(
|
| 380 |
+
MODERATOR_MODEL,
|
| 381 |
+
self.pm.final_sys(task_profile),
|
| 382 |
+
self.pm.final_user(user_prompt, current_draft, task_info),
|
|
|
|
|
|
|
| 383 |
)
|
| 384 |
+
history.append({"role": "assistant", "content": final})
|
| 385 |
yield history
|
| 386 |
|
| 387 |
+
|
| 388 |
+
# Orchestrator-Instanz
|
| 389 |
orchestrator = PlenumOrchestrator()
|
| 390 |
|
| 391 |
+
|
| 392 |
# ==========================================
|
| 393 |
+
# 5. GRADIO UI
|
| 394 |
# ==========================================
|
| 395 |
+
|
| 396 |
v_theme = gr.themes.Soft(
|
| 397 |
+
primary_hue="indigo",
|
| 398 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
| 399 |
).set(
|
| 400 |
+
button_primary_background_fill="#4241A6",
|
| 401 |
button_primary_background_fill_hover="#2D2C73",
|
| 402 |
button_primary_text_color="white",
|
| 403 |
+
block_title_text_color="#FF5A4D",
|
| 404 |
block_label_text_color="#4241A6",
|
| 405 |
body_text_color="#1F2937",
|
| 406 |
+
color_accent_soft="#FFEBE8",
|
| 407 |
)
|
| 408 |
|
| 409 |
with gr.Blocks(theme=v_theme) as demo:
|
| 410 |
gr.HTML("""
|
| 411 |
+
<div style="text-align:center; margin-bottom:2rem; margin-top:1rem;">
|
| 412 |
+
<h1 style="color:#FF5A4D; font-weight:900; font-size:2.8rem; margin-bottom:0.2rem;
|
| 413 |
+
font-family:'Inter',sans-serif; letter-spacing:-0.02em;">PromptPlenum42</h1>
|
| 414 |
+
<p style="color:#4B5563; font-size:1.1rem; font-family:'Inter',sans-serif;">
|
| 415 |
+
AI-Driven Multi-Agent Consensus System
|
| 416 |
+
</p>
|
| 417 |
</div>
|
| 418 |
""")
|
| 419 |
|
| 420 |
with gr.Row():
|
| 421 |
with gr.Column(scale=4):
|
| 422 |
input_text = gr.Textbox(
|
| 423 |
+
label="Plenumsauftrag",
|
| 424 |
+
placeholder=(
|
| 425 |
+
"z.B. 'Refaktoriere dieses Python-Skript' Β· "
|
| 426 |
+
"'Schreibe einen LinkedIn-Post ΓΌber KI' Β· "
|
| 427 |
+
"'Erstelle einen Go-to-Market-Plan fΓΌr ein SaaS-Produkt'"
|
| 428 |
+
),
|
| 429 |
+
lines=3,
|
| 430 |
)
|
| 431 |
with gr.Column(scale=1):
|
| 432 |
+
rounds_slider = gr.Slider(
|
| 433 |
+
minimum=1, maximum=5, value=1, step=1, label="Diskussionszyklen"
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
with gr.Row():
|
| 437 |
start_btn = gr.Button("Sitzung starten", variant="primary", size="lg")
|
| 438 |
+
clear_btn = gr.ClearButton(
|
| 439 |
+
components=[input_text], value="Protokoll leeren", size="lg"
|
| 440 |
+
)
|
| 441 |
+
|
| 442 |
+
chatbot = gr.Chatbot(
|
| 443 |
+
label="Sitzungsprotokoll",
|
| 444 |
+
height=700,
|
| 445 |
+
type="messages",
|
| 446 |
+
render_markdown=True,
|
| 447 |
+
)
|
| 448 |
clear_btn.add(chatbot)
|
| 449 |
|
| 450 |
+
input_text.submit(
|
| 451 |
+
orchestrator.run, inputs=[input_text, rounds_slider], outputs=[chatbot]
|
| 452 |
+
)
|
| 453 |
+
start_btn.click(
|
| 454 |
+
orchestrator.run, inputs=[input_text, rounds_slider], outputs=[chatbot]
|
| 455 |
+
)
|
| 456 |
|
| 457 |
if __name__ == "__main__":
|
| 458 |
demo.launch()
|