Solution ready for publishing: update main.py

This commit is contained in:
2026-06-18 12:16:28 +00:00
parent ef21fd6dc3
commit 1872effdf5
+21 -37
View File
@@ -2,10 +2,10 @@
Implementation follows assignment: Implementation follows assignment:
- State: CodeReviewState with 4 criteria. - State: CodeReviewState with 4 criteria.
- Nodes: draft_review, reflect, rewrite. - Nodes: review_and_critique, rewrite.
- Graph: START -> draft_review -> reflect -> (ok -> END) or (needs_revision & round<max_rounds -> rewrite -> reflect). - Graph: START -> review_and_critique -> (ok -> END) or (needs_revision & round<max_rounds -> rewrite -> review_and_critique).
- Uses LangGraph and LangChain OpenAI for LLM calls. - Uses LangGraph and LangChain OpenAI for LLM calls.
- Structured output for reflect via Pydantic model. - Structured output for critique via Pydantic model.
- Demo function sort_numbers. - Demo function sort_numbers.
""" """
@@ -34,8 +34,14 @@ class CodeReviewState(TypedDict):
llm = ChatOpenAI(temperature=0) llm = ChatOpenAI(temperature=0)
# ---------- Nodes ---------- # ---------- Nodes ----------
class CritiqueOutput(BaseModel):
review: str = Field(..., description="Draft review text")
scores: Dict[str, int] = Field(..., description="Scores 0-10 for each criterion")
weakest_criterion: str = Field(..., description="Criterion with lowest score")
verdict: str = Field(..., description="'ok' or 'needs_revision'")
def draft_review(state: CodeReviewState) -> CodeReviewState:
def review_and_critique(state: CodeReviewState) -> CodeReviewState:
code = state["code"] code = state["code"]
prompt = f""" prompt = f"""
Write a concise code review (3-6 bullet points) for the following Python function. Focus on style, correctness, and potential improvements. Write a concise code review (3-6 bullet points) for the following Python function. Focus on style, correctness, and potential improvements.
@@ -43,34 +49,13 @@ def draft_review(state: CodeReviewState) -> CodeReviewState:
```python ```python
{code} {code}
``` ```
"""
review = llm.invoke([HumanMessage(content=prompt)])
state["draft_review"] = review.content
return state
class ReflectOutput(BaseModel): Then evaluate the review on four criteria (PEP8, type_hints, edge_cases, naming) on a scale 0-10.
scores: Dict[str, int] = Field(..., description="Scores 0-10 for each criterion") Return a JSON object with keys: review (string), scores (dict), weakest_criterion (string), verdict ('ok' if all scores >=7 else 'needs_revision').
weakest_criterion: str = Field(..., description="Criterion with lowest score")
verdict: str = Field(..., description="'ok' or 'needs_revision'")
def reflect(state: CodeReviewState) -> CodeReviewState:
code = state["code"]
review = state["draft_review"]
prompt = f"""
You are a code review critic. Evaluate the following review of a Python function.
Function code:
```python
{code}
```
Review:
{review}
Score the review on four criteria (PEP8, type_hints, edge_cases, naming) on a scale 0-10.
Return a JSON object with keys: scores (dict), weakest_criterion (string), verdict ('ok' if all scores >=7 else 'needs_revision').
""" """
response = llm.invoke([HumanMessage(content=prompt)]) response = llm.invoke([HumanMessage(content=prompt)])
data = ReflectOutput.model_validate_json(response.content) data = CritiqueOutput.model_validate_json(response.content)
state["draft_review"] = data.review
state["criteria_scores"] = data.scores state["criteria_scores"] = data.scores
state["weakest_criterion"] = data.weakest_criterion state["weakest_criterion"] = data.weakest_criterion
state["verdict"] = data.verdict state["verdict"] = data.verdict
@@ -92,22 +77,21 @@ def rewrite(state: CodeReviewState) -> CodeReviewState:
# ---------- Graph ---------- # ---------- Graph ----------
builder = StateGraph(CodeReviewState) builder = StateGraph(CodeReviewState)
builder.add_node("draft_review", draft_review) builder.add_node("review_and_critique", review_and_critique)
builder.add_node("reflect", reflect)
builder.add_node("rewrite", rewrite) builder.add_node("rewrite", rewrite)
builder.set_entry_point("draft_review") builder.set_entry_point("review_and_critique")
builder.add_edge("draft_review", "reflect") # After initial review_and_critique, decide to end if verdict ok
builder.add_conditional_edges( builder.add_conditional_edges(
"reflect", "review_and_critique",
lambda state: state["verdict"] == "ok", lambda state: state["verdict"] == "ok",
{"ok": END, "needs_revision": "rewrite"}, {"ok": END, "needs_revision": "rewrite"},
) )
# After rewrite, decide to reflect again or end if max rounds reached # After rewrite, go back to review_and_critique if rounds remain
builder.add_conditional_edges( builder.add_conditional_edges(
"rewrite", "rewrite",
lambda state: "reflect" if state["round"] < state["max_rounds"] else END, lambda state: "review_and_critique" if state["round"] < state["max_rounds"] else END,
{"reflect": "reflect", END: END} {"review_and_critique": "review_and_critique", END: END}
) )
graph = builder.compile() graph = builder.compile()