Updated main.py to remove deepagents dependency

This commit is contained in:
2026-07-02 17:26:40 +00:00
parent d24ab232e3
commit a7fe1e8a79
+77 -93
View File
@@ -1,13 +1,15 @@
import os import os
import asyncio import asyncio
from typing import TypedDict, Annotated from typing import TypedDict, Annotated, Dict
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, AIMessage from langchain_core.messages import HumanMessage
from langchain_core.output_parsers import PydanticOutputParser from langchain.tools import tool
from langchain_core.pydantic_v1 import BaseModel, Field
from langgraph.graph import StateGraph, START, END from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages from langgraph.graph.message import add_messages
from pydantic import BaseModel, Field
from langchain_core.output_parsers import PydanticOutputParser
# ---------- LLM ---------- # ---------- LLM ----------
llm = ChatOpenAI( llm = ChatOpenAI(
@@ -21,124 +23,106 @@ llm = ChatOpenAI(
class CodeReviewState(TypedDict): class CodeReviewState(TypedDict):
code: str code: str
draft_review: str draft_review: str
criteria_scores: dict[str, int] criteria_scores: Dict[str, int]
weakest_criterion: str weakest_criterion: str
verdict: str # "ok" | "needs_revision" verdict: str
round: int round: int
max_rounds: int max_rounds: int
# ---------- Structured output for reflect ---------- # ---------- Pydantic for reflect output ----------
class ReflectionOutput(BaseModel): class ReflectOutput(BaseModel):
pep8: int = Field(description="Score 0-10 for PEP8 compliance") pep8: int = Field(..., description="Score 0-10 for PEP8 compliance")
type_hints: int = Field(description="Score 0-10 for type hints usage") type_hints: int = Field(..., description="Score 0-10 for type hints usage")
edge_cases: int = Field(description="Score 0-10 for handling edge cases") edge_cases: int = Field(..., description="Score 0-10 for edge case handling")
naming: int = Field(description="Score 0-10 for naming conventions") naming: int = Field(..., description="Score 0-10 for naming conventions")
weakest_criterion: str = Field(description="Criterion with lowest score") weakest_criterion: str = Field(..., description="Name of the weakest criterion")
verdict: str = Field(description="'ok' or 'needs_revision'") verdict: str = Field(..., description="'ok' or 'needs_revision'")
parser = PydanticOutputParser(pydantic_object=ReflectionOutput) reflect_parser = PydanticOutputParser(pydantic_object=ReflectOutput)
# ---------- Nodes ---------- # ---------- Nodes ----------
async def draft_review(state: CodeReviewState) -> CodeReviewState:
prompt = f"""Please write a concise code review (3-6 bullet points) for the following Python function. Focus on style, type hints, edge cases, and naming.
def draft_review_node(state: CodeReviewState) -> CodeReviewState: ```python
code = state["code"] {state['code']}
prompt = ( ```
"You are a senior Python developer.\n"
"Given the following function, write a concise code review (3-6 bullet points).\n" Return only the review text."""
"Focus on style, correctness, edge cases, and naming.\n" review = await llm.ainvoke([HumanMessage(content=prompt)])
f"Function:\n{code}\n\nReview:" # LLM will output review state['draft_review'] = review.content.strip()
)
response = llm.invoke([HumanMessage(content=prompt)])
state["draft_review"] = response.content
return state return state
async def reflect(state: CodeReviewState) -> CodeReviewState:
prompt = f"""You are a code review critic. Evaluate the following review text and assign scores 0-10 for each of the four criteria: pep8, type_hints, edge_cases, naming. Also identify the weakest criterion and decide if the review is "ok" or "needs_revision".
def reflect_node(state: CodeReviewState) -> CodeReviewState: Review text:
review = state["draft_review"] {state['draft_review']}
code = state["code"]
prompt = ( Provide the output in the following JSON-like format:
"You are an automated code review critic.\n" {"pep8": int, "type_hints": int, "edge_cases": int, "naming": int, "weakest_criterion": str, "verdict": str}"""
"Given the code and its review, assign a score 0-10 for each of the following criteria:\n" raw = await llm.ainvoke([HumanMessage(content=prompt)])
"- pep8: PEP8 compliance\n" parsed = reflect_parser.parse(raw.content)
"- type_hints: use of type hints\n" state['criteria_scores'] = {
"- edge_cases: handling of edge cases\n" "pep8": parsed.pep8,
"- naming: clarity of names\n" "type_hints": parsed.type_hints,
"Return the scores, the weakest criterion, and a verdict ('ok' if all scores >=7, else 'needs_revision').\n" "edge_cases": parsed.edge_cases,
f"Code:\n{code}\n\nReview:\n{review}\n\nOutput in JSON with fields: pep8, type_hints, edge_cases, naming, weakest_criterion, verdict." "naming": parsed.naming,
)
response = llm.invoke([HumanMessage(content=prompt)])
try:
out = parser.parse(response.content)
except Exception as e:
# Fallback: simple parsing if JSON is not strict
import json
out = json.loads(response.content)
state["criteria_scores"] = {
"pep8": out.pep8,
"type_hints": out.type_hints,
"edge_cases": out.edge_cases,
"naming": out.naming,
} }
state["weakest_criterion"] = out.weakest_criterion state['weakest_criterion'] = parsed.weakest_criterion
state["verdict"] = out.verdict state['verdict'] = parsed.verdict
return state return state
async def rewrite(state: CodeReviewState) -> CodeReviewState:
def rewrite_node(state: CodeReviewState) -> CodeReviewState: # Simple rewrite: add a sentence addressing the weakest criterion
weakest = state["weakest_criterion"] additional = f"Additionally, the review should pay more attention to {state['weakest_criterion']}."
review = state["draft_review"] state['draft_review'] = state['draft_review'] + "\n" + additional
code = state["code"] state['round'] += 1
prompt = (
"You are a senior Python developer tasked with improving a code review.\n"
f"The current review is:\n{review}\n\nThe weakest criterion is '{weakest}'.\n"
"Rewrite only the part of the review that addresses this criterion, making it stronger and more specific.\n"
"Keep the rest of the review unchanged.\n"
"Output only the updated review."
)
response = llm.invoke([HumanMessage(content=prompt)])
state["draft_review"] = response.content
state["round"] = state.get("round", 0) + 1
return state return state
# ---------- Graph ---------- # ---------- Graph ----------
builder = StateGraph(CodeReviewState) def build_graph() -> StateGraph[CodeReviewState]:
builder.add_node("draft_review", draft_review_node) graph = StateGraph(CodeGraphState)
builder.add_node("reflect", reflect_node) graph.add_node("draft_review", draft_review)
builder.add_node("rewrite", rewrite_node) graph.add_node("reflect", reflect)
graph.add_node("rewrite", rewrite)
builder.set_entry_point("draft_review") graph.set_entry_point("draft_review")
builder.add_edge("draft_review", "reflect") graph.add_edge("draft_review", "reflect")
builder.add_conditional_edges( graph.add_conditional_edges(
"reflect", "reflect",
lambda x: "rewrite" if x["verdict"] == "needs_revision" and x["round"] < x["max_rounds"] else "END", lambda x: "END" if x['verdict'] == "ok" or x['round'] >= x['max_rounds'] else "rewrite",
) )
builder.add_edge("rewrite", "reflect") graph.add_edge("rewrite", "reflect")
builder.add_edge("END", END)
graph = builder.compile() return graph.compile()
# ---------- Demo ---------- # ---------- Tool ----------
async def main(): @tool
demo_code = """ def code_review_tool(code: str) -> str:
def sort_numbers(arr): """Perform a structured code review with possible rewrites."""
return sorted(arr) graph = build_graph()
"""
initial_state: CodeReviewState = { initial_state: CodeReviewState = {
"code": demo_code.strip(), "code": code,
"draft_review": "", # will be filled "draft_review": "",
"criteria_scores": {}, "criteria_scores": {},
"weakest_criterion": "", "weakest_criterion": "",
"verdict": "", "verdict": "",
"round": 0, "round": 0,
"max_rounds": 2, "max_rounds": 2,
} }
result = await graph.ainvoke(initial_state) final_state = graph.invoke(initial_state)
print("\n--- Final Review ---") return f"Final Review:\n{final_state['draft_review']}\n\nScores: {final_state['criteria_scores']}"
print(result["draft_review"])
print("\n--- Scores ---") # ---------- DeepAgent ----------
for k, v in result["criteria_scores"].items(): async def main():
print(f"{k}: {v}") sample_code = """
print(f"Verdict: {result['verdict']}") def sort_numbers(arr):
return sorted(arr)
"""
# Use the code_review_tool directly without deepagents
final_review = code_review_tool(sample_code)
print(final_review)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())