fix: main.py — Повторный экзамен #2: Граф с рефлексией на код
This commit is contained in:
@@ -1,34 +1,44 @@
|
|||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
from typing import TypedDict, Annotated, Dict
|
from typing import TypedDict, Dict, Annotated
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_core.messages import HumanMessage
|
from langchain_core.messages import HumanMessage, SystemMessage
|
||||||
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
|
from langgraph.graph import StateGraph, START, END, add_conditional_edges
|
||||||
from langgraph.graph.message import add_messages
|
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from langchain_core.output_parsers import PydanticOutputParser
|
from langchain_core.output_parsers import PydanticOutputParser
|
||||||
|
|
||||||
|
# DESIGN DECISION: Add deepagents to requirements.txt
|
||||||
|
# NECESSITY: deepagents is required for create_deep_agent usage
|
||||||
|
# OPTIMALITY: ensures reproducible installation
|
||||||
|
# ALTERNATIVES CONSIDERED: manual installation or alternative agent framework, but violates course requirement
|
||||||
|
|
||||||
# Load environment variables
|
# Load environment variables
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# LLM configuration - OpenRouter
|
# LLM configuration using OpenRouter
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="openai/gpt-oss-20b",
|
||||||
base_url="https://openrouter.ai/api/v1",
|
base_url="https://openrouter.ai/api/v1",
|
||||||
api_key=os.getenv("OPENAI_API_KEY"),
|
api_key=os.getenv("OPENAI_API_KEY"),
|
||||||
temperature=0.0,
|
temperature=0.0,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Backend for deepagents
|
# Backend for deepagents (not heavily used but required by create_deep_agent)
|
||||||
backend = CompositeBackend(
|
backend = CompositeBackend([
|
||||||
[
|
|
||||||
LocalShellBackend(workspace_dir="./workspace"),
|
LocalShellBackend(workspace_dir="./workspace"),
|
||||||
FilesystemBackend(),
|
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
|
||||||
@@ -41,100 +51,82 @@ class CodeReviewState(TypedDict):
|
|||||||
round: int
|
round: int
|
||||||
max_rounds: int
|
max_rounds: int
|
||||||
|
|
||||||
# Structured output for reflect node
|
# Structured output for the reflect node
|
||||||
class ReviewScores(BaseModel):
|
class CritiqueOutput(BaseModel):
|
||||||
pep8: int = Field(description="Score 0-10 for PEP8 compliance")
|
scores: Dict[str, int] = Field(description="Scores 0-10 for each criterion")
|
||||||
type_hints: int = Field(description="Score 0-10 for type hints usage")
|
verdict: str = Field(description="ok or needs_revision")
|
||||||
edge_cases: int = Field(description="Score 0-10 for edge case handling")
|
weakest_criterion: str = Field(description="Criterion with lowest score")
|
||||||
naming: int = Field(description="Score 0-10 for naming conventions")
|
|
||||||
verdict: str = Field(description='\"ok\" or \"needs_revision\"')
|
|
||||||
|
|
||||||
review_parser = PydanticOutputParser(pydantic_object=ReviewScores)
|
parser = PydanticOutputParser(pydantic_object=CritiqueOutput)
|
||||||
|
|
||||||
# Draft review node
|
# Node: draft_review
|
||||||
def draft_review(state: CodeReviewState) -> CodeReviewState:
|
async def draft_review_node(state: CodeReviewState) -> CodeReviewState:
|
||||||
prompt = f"""Write a concise code review for the following Python function. Provide 3-6 bullet points. Do not include any code blocks. The function is:
|
system_msg = SystemMessage(content="You are a code reviewer. Provide a concise review (3-6 points) of the following Python function.")
|
||||||
```python
|
user_msg = HumanMessage(content=state["code"])
|
||||||
{state["code"]}
|
result = await agent.ainvoke(
|
||||||
```"""
|
{"messages": [system_msg, user_msg]},
|
||||||
response = llm.invoke(prompt)
|
{"configurable": {"thread_id": "draft-review"}},
|
||||||
state["draft_review"] = response.content.strip()
|
)
|
||||||
print("\n--- Draft Review ---")
|
review = result["messages"][-1].content
|
||||||
print(state["draft_review"])
|
state["draft_review"] = review
|
||||||
return state
|
return state
|
||||||
|
|
||||||
# Reflect node
|
# Node: reflect
|
||||||
def reflect(state: CodeReviewState) -> CodeReviewState:
|
async def reflect_node(state: CodeReviewState) -> CodeReviewState:
|
||||||
prompt = f"""You are a code review critic. Evaluate the following draft review for the given code. Provide scores 0-10 for each criterion and a verdict. Return JSON matching the schema:
|
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.")
|
||||||
{review_parser.get_format_instructions()}
|
user_msg = HumanMessage(content=state["draft_review"])
|
||||||
Draft review:
|
raw_output = await llm.invoke([system_msg, user_msg])
|
||||||
{state["draft_review"]}
|
critique = parser.parse(raw_output.content)
|
||||||
|
state["criteria_scores"] = critique.scores
|
||||||
Code:
|
state["weakest_criterion"] = critique.weakest_criterion
|
||||||
```python
|
state["verdict"] = critique.verdict
|
||||||
{state["code"]}
|
|
||||||
```"""
|
|
||||||
response = llm.invoke(prompt)
|
|
||||||
parsed = review_parser.parse(response.content)
|
|
||||||
state["criteria_scores"] = {
|
|
||||||
"pep8": parsed.pep8,
|
|
||||||
"type_hints": parsed.type_hints,
|
|
||||||
"edge_cases": parsed.edge_cases,
|
|
||||||
"naming": parsed.naming,
|
|
||||||
}
|
|
||||||
state["verdict"] = parsed.verdict
|
|
||||||
state["weakest_criterion"] = min(state["criteria_scores"], key=state["criteria_scores"].get)
|
|
||||||
print("\n--- Reflection ---")
|
|
||||||
print(f"Scores: {state['criteria_scores']}")
|
|
||||||
print(f"Weakest criterion: {state['weakest_criterion']}")
|
|
||||||
print(f"Verdict: {state['verdict']}")
|
|
||||||
return state
|
return state
|
||||||
|
|
||||||
# Rewrite node
|
# Node: rewrite
|
||||||
def rewrite(state: CodeReviewState) -> CodeReviewState:
|
async def rewrite_node(state: CodeReviewState) -> CodeReviewState:
|
||||||
prompt = f"""Rewrite the section of the draft review that addresses the weakest criterion ({state['weakest_criterion']}) to improve it. Keep other parts unchanged. Return only the updated draft review.
|
criterion = state["weakest_criterion"]
|
||||||
Original draft review:
|
system_msg = SystemMessage(content=f"You are a code reviewer. Rewrite the review to improve the section about {criterion}. Keep the overall structure.")
|
||||||
{state['draft_review']}
|
user_msg = HumanMessage(content=state["draft_review"])
|
||||||
|
result = await agent.ainvoke(
|
||||||
Code:
|
{"messages": [system_msg, user_msg]},
|
||||||
```python
|
{"configurable": {"thread_id": "rewrite"}},
|
||||||
{state['code']}
|
)
|
||||||
```"""
|
new_review = result["messages"][-1].content
|
||||||
response = llm.invoke(prompt)
|
state["draft_review"] = new_review
|
||||||
state["draft_review"] = response.content.strip()
|
|
||||||
state["round"] += 1
|
state["round"] += 1
|
||||||
print("\n--- Rewrite ---")
|
|
||||||
print(state["draft_review"])
|
|
||||||
return state
|
return state
|
||||||
|
|
||||||
# Graph definition
|
# Conditional edge function
|
||||||
def build_graph() -> StateGraph:
|
def decide_next(state: CodeReviewState) -> str:
|
||||||
graph = StateGraph(CodeReviewState)
|
|
||||||
graph.add_node("draft_review", draft_review)
|
|
||||||
graph.add_node("reflect", reflect)
|
|
||||||
graph.add_node("rewrite", rewrite)
|
|
||||||
|
|
||||||
graph.set_entry_point("draft_review")
|
|
||||||
graph.add_edge("draft_review", "reflect")
|
|
||||||
|
|
||||||
def condition(state: CodeReviewState):
|
|
||||||
if state["verdict"] == "ok":
|
if state["verdict"] == "ok":
|
||||||
return "END"
|
return "END"
|
||||||
if state["round"] >= state["max_rounds"]:
|
if state["round"] < state["max_rounds"]:
|
||||||
return "END"
|
|
||||||
return "rewrite"
|
return "rewrite"
|
||||||
|
return "END"
|
||||||
|
|
||||||
graph.add_conditional_edges("reflect", condition, ["rewrite", "END"])
|
# Build the graph
|
||||||
|
graph = StateGraph(CodeReviewState)
|
||||||
|
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()
|
||||||
|
|
||||||
return graph
|
# Demo function
|
||||||
|
def demo_code() -> str:
|
||||||
|
return """def sort_numbers(arr):
|
||||||
|
return sorted(arr)"""
|
||||||
|
|
||||||
# Tool that runs the graph
|
async def main():
|
||||||
@tool
|
code_str = demo_code()
|
||||||
def run_review(code: str) -> str:
|
|
||||||
"""Run a code review cycle on the provided Python function."""
|
|
||||||
initial_state: CodeReviewState = {
|
initial_state: CodeReviewState = {
|
||||||
"code": code,
|
"code": code_str,
|
||||||
"draft_review": "",
|
"draft_review": "",
|
||||||
"criteria_scores": {},
|
"criteria_scores": {},
|
||||||
"weakest_criterion": "",
|
"weakest_criterion": "",
|
||||||
@@ -142,32 +134,20 @@ def run_review(code: str) -> str:
|
|||||||
"round": 0,
|
"round": 0,
|
||||||
"max_rounds": 2,
|
"max_rounds": 2,
|
||||||
}
|
}
|
||||||
graph = build_graph()
|
final_state = await app.ainvoke(initial_state)
|
||||||
final_state = graph.invoke(initial_state)
|
print("\n=== Initial Draft Review ===")
|
||||||
output = f"Final review after {final_state['round']} round(s):\n{final_state['draft_review']}\n\nScores: {final_state['criteria_scores']}\nVerdict: {final_state['verdict']}"
|
print(initial_state["draft_review"])
|
||||||
return output
|
print("\n=== Scores ===")
|
||||||
|
print(final_state["criteria_scores"])
|
||||||
# Deepagents agent
|
print("\n=== Weakest Criterion ===")
|
||||||
agent = create_deep_agent(
|
print(final_state["weakest_criterion"])
|
||||||
model=llm,
|
if final_state["verdict"] == "needs_revision":
|
||||||
tools=[run_review],
|
print("\n=== Revised Review ===")
|
||||||
backend=backend,
|
print(final_state["draft_review"])
|
||||||
system_prompt="You are a helpful code review agent.",
|
print("\n=== Updated Scores ===")
|
||||||
)
|
print(final_state["criteria_scores"])
|
||||||
|
else:
|
||||||
# Demo function
|
print("\nReview is satisfactory. No rewrite needed.")
|
||||||
demo_code = """
|
|
||||||
def sort_numbers(arr):
|
|
||||||
return sorted(arr)
|
|
||||||
"""
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
result = await agent.ainvoke(
|
|
||||||
{"messages": [HumanMessage(content=demo_code)]},
|
|
||||||
{"configurable": {"thread_id": "session-1"}},
|
|
||||||
)
|
|
||||||
print("\n=== Final Output ===")
|
|
||||||
print(result["messages"][-1].content)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
||||||
Reference in New Issue
Block a user