Add psychoanalysis and output nodes to the LangGraph graph

This commit is contained in:
2025-06-01 10:54:57 +02:00
parent e1489239e1
commit 0515ac769a

View File

@@ -271,3 +271,105 @@ async def sache_node(state): return await ohr_node("sache", SACHE_PROMPT, s
async def selbst_node(state): return await ohr_node("selbst", SELBST_PROMPT, state)
async def beziehung_node(state): return await ohr_node("beziehung", BEZIEHUNG_PROMPT, state)
async def appell_node(state): return await ohr_node("appell", APPELL_PROMPT, state)
async def psychoanalysis_node(state):
debug = state.get("debug", False)
await spinner_status.set_running("psychoanalysis")
prompt = PSYCHOANALYSIS_PROMPT.format(
user_input=state["user_input"]
)
message = ""
if debug:
print("[DEBUG] Node 'psychoanalysis' gestartet")
print(f"[DEBUG][psychoanalysis] Prompt:\n{prompt.strip()}\n---")
print("[DEBUG][psychoanalysis] Starte gestreamte Ausgabe:\n")
async for chunk in llm.astream(prompt):
if debug:
print(chunk.content, end="", flush=True)
message += chunk.content
if debug:
print(f"\n[DEBUG][psychoanalysis] Ausgabe beendet.\n")
try:
data = json.loads(message)
except Exception:
data = message
await spinner_status.set_finished("psychoanalysis")
return {"psychoanalysis": data}
async def output_node(state):
debug = state.get("debug", False)
psycho_json_str = (
json.dumps(state["psychoanalysis"], ensure_ascii=False, indent=2)
if isinstance(state["psychoanalysis"], dict)
else str(state["psychoanalysis"])
)
prompt = OUTPUT_NODE_PROMPT.format(
sache_ohr=state["sache"]["message"],
selbst_ohr=state["selbst"]["message"],
beziehung_ohr=state["beziehung"]["message"],
appell_ohr=state["appell"]["message"],
psychoanalysis_json=psycho_json_str,
user_input=state["user_input"],
)
output_text = ""
if debug:
print(f"[DEBUG] Node 'output' gestartet")
print(f"[DEBUG][output] Prompt:\n{prompt.strip()}\n---")
print("[DEBUG][output] Starte gestreamte Ausgabe:\n")
async for chunk in llm.astream(prompt):
print(chunk.content, end="", flush=True)
output_text += chunk.content
print()
if debug:
print(f"\n[DEBUG][output] Ausgabe beendet.")
return {"final_message": output_text}
# ---------- LangGraph Graph ----------
graph = StateGraph(state_schema=EmpathyState)
graph.add_node("sache_node", sache_node)
graph.add_node("selbst_node", selbst_node)
graph.add_node("beziehung_node", beziehung_node)
graph.add_node("appell_node", appell_node)
graph.add_node("psychoanalysis_node", psychoanalysis_node)
graph.add_node("output_node", output_node)
graph.add_edge(START, "sache_node")
graph.add_edge(START, "selbst_node")
graph.add_edge(START, "beziehung_node")
graph.add_edge(START, "appell_node")
graph.add_edge(START, "psychoanalysis_node")
graph.add_edge("sache_node", "output_node")
graph.add_edge("selbst_node", "output_node")
graph.add_edge("beziehung_node", "output_node")
graph.add_edge("appell_node", "output_node")
graph.add_edge("psychoanalysis_node", "output_node")
graph.add_edge("output_node", END)
app = graph.compile()
# ---------- Ausführung ----------
async def main():
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true", help="Debug-Ausgaben aktivieren")
args = parser.parse_args()
user_input = input("Bitte gib deine Nachricht ein: ")
state = {
"user_input": user_input,
"debug": args.debug,
}
# Spinner als Background-Task starten (sofort, vor dem Graph)
spinner_task = None
if not args.debug:
spinner_task = asyncio.create_task(spinner_loop(debug=False))
result = await app.ainvoke(state)
# Warten, bis spinner sich sauber beendet hat
if spinner_task:
await asyncio.sleep(0.4) # kurz warten, damit "done"-Status angezeigt wird
#if not args.debug:
# print("\nAntwort:\n" + result["final_message"])
if __name__ == "__main__":
asyncio.run(main())