Initial commit of LangGraph code review agent

This commit is contained in:
2026-07-03 11:46:56 +00:00
parent 823f9b0282
commit 4644f31171
+167 -105
View File
@@ -1,132 +1,173 @@
"""
# main.py
# LangGraph code review agent with deepagents integration
# Author: OpenAI ChatGPT
# Requirements: deepagents, langchain>=1.2.10, langchain-openai>=0.3.0, langgraph>=0.2.0
# Run: python main.py
"""
import os import os
import json
import asyncio import asyncio
from typing import TypedDict, Dict, Annotated from typing import TypedDict, Annotated, Dict
from langchain_openai import ChatOpenAI from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage from langchain_core.messages import HumanMessage
from langchain.tools import tool from langchain.tools import tool
from deepagents import create_deep_agent from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend from deepagents.backends import FilesystemBackend, LocalShellBackend, CompositeBackend
from langgraph.graph import StateGraph, START, END, add_conditional_edges from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from langchain_core.output_parsers import PydanticOutputParser
# DESIGN DECISION: Add deepagents to requirements.txt # ---------------------------------------------------------------------------
# NECESSITY: deepagents is required for create_deep_agent usage # Configuration
# OPTIMALITY: ensures reproducible installation # ---------------------------------------------------------------------------
# ALTERNATIVES CONSIDERED: manual installation or alternative agent framework, but violates course requirement # Load OpenRouter API key from environment
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise RuntimeError("OPENAI_API_KEY environment variable not set")
# Load environment variables # LLM instance (OpenRouter)
from dotenv import load_dotenv
load_dotenv()
# LLM configuration using OpenRouter
llm = ChatOpenAI( llm = ChatOpenAI(
model="openai/gpt-oss-20b", model="openai/gpt-oss-20b:free",
base_url="https://openrouter.ai/api/v1", base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENAI_API_KEY"), api_key=OPENAI_API_KEY,
temperature=0.0, temperature=0.0,
) )
# Backend for deepagents (not heavily used but required by create_deep_agent) # ---------------------------------------------------------------------------
backend = CompositeBackend([
LocalShellBackend(workspace_dir="./workspace"),
FilesystemBackend(),
])
# Deepagents agent used for drafting and rewriting reviews
agent = create_deep_agent(
model=llm,
tools=[],
backend=backend,
system_prompt="You are a helpful agent.",
)
# State definition # State definition
# ---------------------------------------------------------------------------
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] # {"pep8": int, "type_hints": int, "edge_cases": int, "naming": int}
weakest_criterion: str weakest_criterion: str
verdict: str # "ok" | "needs_revision" verdict: str # "ok" | "needs_revision"
round: int round: int
max_rounds: int max_rounds: int
# Structured output for the reflect node # ---------------------------------------------------------------------------
class CritiqueOutput(BaseModel): # Pydantic model for reflect output
scores: Dict[str, int] = Field(description="Scores 0-10 for each criterion") # ---------------------------------------------------------------------------
verdict: str = Field(description="ok or needs_revision") class ReviewScores(BaseModel):
weakest_criterion: str = Field(description="Criterion with lowest score") pep8: int = Field(..., description="Score for PEP8 compliance (0-10)")
type_hints: int = Field(..., description="Score for type hints usage (0-10)")
edge_cases: int = Field(..., description="Score for handling edge cases (0-10)")
naming: int = Field(..., description="Score for naming conventions (0-10)")
verdict: str = Field(..., description="'ok' if all scores >=7 else 'needs_revision'")
parser = PydanticOutputParser(pydantic_object=CritiqueOutput) # ---------------------------------------------------------------------------
# LangGraph nodes
# ---------------------------------------------------------------------------
async def draft_review(state: CodeReviewState) -> CodeReviewState:
code = state["code"]
prompt = f"""
Write a concise code review (3-6 bullet points) for the following Python function:
# Node: draft_review {code}
async def draft_review_node(state: CodeReviewState) -> CodeReviewState:
system_msg = SystemMessage(content="You are a code reviewer. Provide a concise review (3-6 points) of the following Python function.") Review:
user_msg = HumanMessage(content=state["code"]) """
result = await agent.ainvoke( messages = [HumanMessage(content=prompt)]
{"messages": [system_msg, user_msg]}, response = await llm.ainvoke(messages)
{"configurable": {"thread_id": "draft-review"}}, review = response.content.strip()
)
review = result["messages"][-1].content
state["draft_review"] = review state["draft_review"] = review
return state return state
# Node: reflect async def reflect(state: CodeReviewState) -> CodeReviewState:
async def reflect_node(state: CodeReviewState) -> CodeReviewState: review = state["draft_review"]
system_msg = SystemMessage(content="You are a code critic. Score the following review on four criteria: pep8, type_hints, edge_cases, naming. Return a JSON with scores, verdict, and weakest_criterion.") code = state["code"]
user_msg = HumanMessage(content=state["draft_review"]) prompt = f"""
raw_output = await llm.invoke([system_msg, user_msg]) You are a code review critic. Evaluate the following review against the code. Assign scores 0-10 for each criterion: PEP8, type hints, edge cases, naming. Also provide verdict: "ok" if all scores >=7, else "needs_revision". Return JSON with keys: pep8, type_hints, edge_cases, naming, verdict.
critique = parser.parse(raw_output.content)
state["criteria_scores"] = critique.scores Example:
state["weakest_criterion"] = critique.weakest_criterion {{"pep8":8,"type_hints":9,"edge_cases":6,"naming":7,"verdict":"needs_revision"}}
state["verdict"] = critique.verdict
Code:
{code}
Review:
{review}
JSON:
"""
messages = [HumanMessage(content=prompt)]
response = await llm.ainvoke(messages)
try:
data = json.loads(response.content)
except Exception as e:
data = {}
scores = {
"pep8": int(data.get("pep8", 0)),
"type_hints": int(data.get("type_hints", 0)),
"edge_cases": int(data.get("edge_cases", 0)),
"naming": int(data.get("naming", 0)),
}
weakest = min(scores, key=scores.get)
state["criteria_scores"] = scores
state["weakest_criterion"] = weakest
state["verdict"] = data.get("verdict", "needs_revision")
return state return state
# Node: rewrite async def rewrite(state: CodeReviewState) -> CodeReviewState:
async def rewrite_node(state: CodeReviewState) -> CodeReviewState: review = state["draft_review"]
criterion = state["weakest_criterion"] weakest = state["weakest_criterion"]
system_msg = SystemMessage(content=f"You are a code reviewer. Rewrite the review to improve the section about {criterion}. Keep the overall structure.") code = state["code"]
user_msg = HumanMessage(content=state["draft_review"]) prompt = f"""
result = await agent.ainvoke( Rewrite the part of the review that addresses the {weakest} criterion to improve it. Keep other parts unchanged. Provide only the updated review.
{"messages": [system_msg, user_msg]},
{"configurable": {"thread_id": "rewrite"}}, Original review:
) {review}
new_review = result["messages"][-1].content
state["draft_review"] = new_review Updated review:
"""
messages = [HumanMessage(content=prompt)]
response = await llm.ainvoke(messages)
updated_review = response.content.strip()
state["draft_review"] = updated_review
state["round"] += 1 state["round"] += 1
return state return state
# Conditional edge function # ---------------------------------------------------------------------------
def decide_next(state: CodeReviewState) -> str: # Build the graph
if state["verdict"] == "ok": # ---------------------------------------------------------------------------
return "END" graph = StateGraph(CodeReviewState)
if state["round"] < state["max_rounds"]: graph.add_node("draft_review", draft_review)
graph.add_node("reflect", reflect)
graph.add_node("rewrite", rewrite)
graph.set_entry_point("draft_review")
# After draft_review always go to reflect
graph.add_edge("draft_review", "reflect")
# Conditional after reflect
def reflect_cond(state: CodeReviewState):
if state["verdict"] == "needs_revision" and state["round"] < state["max_rounds"]:
return "rewrite" return "rewrite"
return "END" return "END"
# Build the graph graph.add_conditional_edges("reflect", reflect_cond)
graph = StateGraph(CodeReviewState) # After rewrite go back to reflect
graph.add_node("draft_review", draft_review_node)
graph.add_node("reflect", reflect_node)
graph.add_node("rewrite", rewrite_node)
graph.add_conditional_edges("reflect", decide_next, {
"rewrite": "rewrite",
"END": END,
})
graph.add_edge(START, "draft_review")
graph.add_edge("draft_review", "reflect")
graph.add_edge("rewrite", "reflect") graph.add_edge("rewrite", "reflect")
app = graph.compile()
# Demo function compiled_graph = graph.compile()
def demo_code() -> str:
return """def sort_numbers(arr):
return sorted(arr)"""
async def main(): # ---------------------------------------------------------------------------
code_str = demo_code() # DeepAgents integration
initial_state: CodeReviewState = { # ---------------------------------------------------------------------------
"code": code_str, # Backend for deepagents (in-memory shell, no real files used)
backend = CompositeBackend(
default=LocalShellBackend(root_dir="./workspace", virtual_mode=True, inherit_env=True),
routes={},
)
@tool
def run_code_review(code: str) -> str:
"""Run a multiround code review on the provided Python function."""
# Initial state
state: CodeReviewState = {
"code": code,
"draft_review": "", "draft_review": "",
"criteria_scores": {}, "criteria_scores": {},
"weakest_criterion": "", "weakest_criterion": "",
@@ -134,20 +175,41 @@ async def main():
"round": 0, "round": 0,
"max_rounds": 2, "max_rounds": 2,
} }
final_state = await app.ainvoke(initial_state)
print("\n=== Initial Draft Review ===")
print(initial_state["draft_review"])
print("\n=== Scores ===")
print(final_state["criteria_scores"])
print("\n=== Weakest Criterion ===")
print(final_state["weakest_criterion"])
if final_state["verdict"] == "needs_revision":
print("\n=== Revised Review ===")
print(final_state["draft_review"])
print("\n=== Updated Scores ===")
print(final_state["criteria_scores"])
else:
print("\nReview is satisfactory. No rewrite needed.")
async def _run():
final_state = await compiled_graph.invoke(state)
# Logging
print("\n--- Draft Review ---")
print(final_state["draft_review"])
print("\n--- Scores ---")
print(final_state["criteria_scores"])
if final_state["verdict"] == "needs_revision":
print("\n--- Rewrite performed ---")
print("\n--- Final Review ---")
print(final_state["draft_review"])
return final_state["draft_review"]
return asyncio.run(_run())
agent = create_deep_agent(
model=llm,
tools=[run_code_review],
backend=backend,
system_prompt="You are a helpful code review assistant. Use the provided tool to review code.",
)
# ---------------------------------------------------------------------------
# Demo CLI
# ---------------------------------------------------------------------------
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) sample_code = """
# Example function to sort numbers
def sort_numbers(arr):
return sorted(arr)
"""
# Directly invoke the tool for demonstration
print("Running demo on sample function...")
final_review = run_code_review(sample_code)
print("\n=== Final Review Output ===")
print(final_review)