Solution ready for publish: update main.py
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
This implementation follows the assignment requirements:
|
This implementation follows the assignment requirements:
|
||||||
- Draft answer node
|
- Draft answer node
|
||||||
- Reflect node that uses try/except to retry generation when needed
|
- Reflect node that critiques the draft
|
||||||
- Rewrite node that updates draft based on critique
|
- Rewrite node that updates draft based on critique
|
||||||
- max_rounds default 2
|
- max_rounds default 2
|
||||||
- CLI entry point
|
- CLI entry point
|
||||||
@@ -12,7 +12,6 @@ from typing import TypedDict, Dict
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from langgraph.graph import StateGraph, END
|
from langgraph.graph import StateGraph, END
|
||||||
from langgraph.prebuilt import create_chat_agent
|
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
|
|
||||||
# --- State definition -----------------------------------------------------
|
# --- State definition -----------------------------------------------------
|
||||||
@@ -41,29 +40,21 @@ def draft_answer(state: ReflectState) -> Dict:
|
|||||||
def reflect(state: ReflectState) -> Dict:
|
def reflect(state: ReflectState) -> Dict:
|
||||||
"""Critique the draft.
|
"""Critique the draft.
|
||||||
|
|
||||||
Implements retry logic: if the LLM raises an exception during generation,
|
The node returns a verdict ('ok' or 'needs_revision') and 2–3 concise points of improvement.
|
||||||
it will be caught and the node will return a verdict of "needs_revision"
|
|
||||||
with an empty critique. This satisfies the feedback that the original
|
|
||||||
solution should use try/except instead of a dedicated reflect node.
|
|
||||||
"""
|
"""
|
||||||
draft = state["draft"]
|
draft = state["draft"]
|
||||||
question = state["question"]
|
question = state["question"]
|
||||||
try:
|
prompt = (
|
||||||
prompt = (
|
f"You are a critical reviewer. Evaluate the following draft answer to the question '{question}'. "
|
||||||
f"You are a critical reviewer. Evaluate the following draft answer to the question '{question}'. "
|
"Provide a verdict ('ok' or 'needs_revision') and 2–3 concise points of improvement. "
|
||||||
"Provide a verdict ('ok' or 'needs_revision') and 2–3 concise points of improvement. "
|
"Respond in JSON with keys 'verdict' and 'critique'."
|
||||||
"Respond in JSON with keys 'verdict' and 'critique'."
|
)
|
||||||
)
|
response = llm.invoke(prompt)
|
||||||
response = llm.invoke(prompt)
|
# Expect JSON; simple parse
|
||||||
# Expect JSON; simple parse
|
import json
|
||||||
import json
|
data = json.loads(response.content)
|
||||||
data = json.loads(response.content)
|
verdict = data.get("verdict", "needs_revision")
|
||||||
verdict = data.get("verdict", "needs_revision")
|
critique = data.get("critique", "")
|
||||||
critique = data.get("critique", "")
|
|
||||||
except Exception as e:
|
|
||||||
# On any exception, force a revision
|
|
||||||
verdict = "needs_revision"
|
|
||||||
critique = f"LLM error: {e}"
|
|
||||||
return {"verdict": verdict, "critique": critique}
|
return {"verdict": verdict, "critique": critique}
|
||||||
|
|
||||||
|
|
||||||
@@ -88,10 +79,13 @@ builder.add_node("rewrite", rewrite)
|
|||||||
# Connections
|
# Connections
|
||||||
builder.set_entry_point("draft_answer")
|
builder.set_entry_point("draft_answer")
|
||||||
builder.add_edge("draft_answer", "reflect")
|
builder.add_edge("draft_answer", "reflect")
|
||||||
|
|
||||||
|
# Conditional after reflect: if ok -> END, else if round < max_rounds -> rewrite, else -> END
|
||||||
builder.add_conditional_edges(
|
builder.add_conditional_edges(
|
||||||
"reflect",
|
"reflect",
|
||||||
lambda x: END if x["verdict"] == "ok" else "rewrite",
|
lambda x: END if x["verdict"] == "ok" else "rewrite" if x["round"] < x["max_rounds"] else END,
|
||||||
)
|
)
|
||||||
|
|
||||||
builder.add_edge("rewrite", "reflect")
|
builder.add_edge("rewrite", "reflect")
|
||||||
|
|
||||||
graph = builder.compile()
|
graph = builder.compile()
|
||||||
|
|||||||
Reference in New Issue
Block a user