sharancreates commited on
Commit
280c92a
·
verified ·
1 Parent(s): bcf9a5f

Upload 6 files

Browse files
Files changed (6) hide show
  1. DockerFile +19 -0
  2. env.py +103 -0
  3. inference.py +84 -0
  4. models.py +21 -0
  5. openenv.yaml +16 -0
  6. requirements.txt +0 -0
DockerFile ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a lightweight Python base
2
+ FROM python:3.10-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Copy requirements and install
8
+ COPY requirements.txt .
9
+ RUN pip install --no-cache-dir -r requirements.txt
10
+
11
+ # Copy all project files
12
+ COPY . .
13
+
14
+ # Environment variables for OpenEnv (defaults)
15
+ ENV API_BASE_URL="https://router.huggingface.co/v1"
16
+ ENV MODEL_NAME="Qwen/Qwen2.5-72B-Instruct"
17
+
18
+ # The command to run your inference
19
+ CMD ["python", "inference.py"]
env.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Tuple, Dict, Any
3
+
4
+ # Updated imports based on your library's structure
5
+ from openenv.core.env_server import Environment
6
+ from models import Action, Observation, Reward
7
+
8
+ # Change the class name from CodeReviewEnv(OpenEnv) to CodeReviewEnv(Environment)
9
+ class CodeReviewEnv(Environment):
10
+ def __init__(self):
11
+ # In the new version, spec loading is often handled via the CLI
12
+ # or you can just initialize the base class directly.
13
+ super().__init__()
14
+ self.current_task_id = None
15
+ self.code = ""
16
+ self.step_count = 0
17
+ self.max_steps = 5
18
+
19
+ # ... the rest of your methods (reset, step, etc.) remain the same ...
20
+
21
+ def reset(self, task_id: str = "style-cleanup") -> Observation:
22
+ """Starts a new task session."""
23
+ self.current_task_id = task_id
24
+ self.step_count = 0
25
+
26
+ # Load the buggy code from our data folder
27
+ file_map = {
28
+ "style-cleanup": "data/style.py",
29
+ "efficiency-boost": "data/logic.py",
30
+ "security-audit": "data/security.py"
31
+ }
32
+
33
+ file_path = file_map.get(task_id, "data/style.py")
34
+ with open(file_path, "r") as f:
35
+ self.code = f.read()
36
+
37
+ return self._get_observation()
38
+
39
+ def step(self, action: Action) -> Tuple[Observation, float, bool, Dict[str, Any]]:
40
+ """Handles the agent's move and returns the reward."""
41
+ self.step_count += 1
42
+ reward_val = 0.0
43
+ done = False
44
+
45
+ # Logic: If the agent provides a fix, update our internal 'code' state
46
+ if action.action_type == "apply_fix":
47
+ self.code = action.content
48
+ reward_val = self._calculate_reward() # Partial progress
49
+
50
+ elif action.action_type == "submit":
51
+ reward_val = self._calculate_reward()
52
+ done = True
53
+
54
+ if self.step_count >= self.max_steps:
55
+ done = True
56
+
57
+ return self._get_observation(), reward_val, done, {}
58
+
59
+ def _get_observation(self) -> Observation:
60
+ """Returns what the agent sees right now."""
61
+ return Observation(
62
+ file_name=f"{self.current_task_id}.py",
63
+ code_content=self.code,
64
+ diff="Changes applied to buffer.",
65
+ linter_report=self._mock_linter(),
66
+ current_task=self.current_task_id
67
+ )
68
+
69
+ def _mock_linter(self):
70
+ """A simple linter to guide the agent."""
71
+ errors = []
72
+ if "import sys" in self.code and self.current_task_id == "style-cleanup":
73
+ errors.append("L001: Unused import 'sys' detected.")
74
+ if "f\"SELECT" in self.code and self.current_task_id == "security-audit":
75
+ errors.append("S001: SQL Injection vulnerability detected.")
76
+ return errors
77
+
78
+ def _calculate_reward(self) -> float:
79
+ """
80
+ CRITICAL: Deterministic grading logic.
81
+ This is where 25% of your score comes from.
82
+ """
83
+ score = 0.0
84
+
85
+ if self.current_task_id == "style-cleanup":
86
+ # Reward for fixing indentation AND removing the unused import
87
+ if "import sys" not in self.code: score += 0.5
88
+ if " print(\"Indentation" in self.code: score += 0.5
89
+
90
+ elif self.current_task_id == "security-audit":
91
+ # Reward for using parameterized queries instead of f-strings
92
+ if "execute(query)" not in self.code and ("?" in self.code or "," in self.code):
93
+ score = 1.0
94
+
95
+ elif self.current_task_id == "efficiency-boost":
96
+ # Reward for getting rid of the nested loop
97
+ if self.code.count("for ") == 1: # Suggests they used a set or dict
98
+ score = 1.0
99
+
100
+ return min(max(score, 0.0), 1.0) # Ensure it stays in [0, 1]
101
+
102
+ def state(self) -> Dict[str, Any]:
103
+ return {"code": self.code, "task": self.current_task_id}
inference.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import asyncio
4
+ from openai import OpenAI
5
+ from env import CodeReviewEnv
6
+ from models import Action
7
+
8
+ # 1. Setup Environment Variables
9
+ API_KEY = os.getenv("HF_TOKEN")
10
+ API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
11
+ MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
12
+
13
+ client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
14
+
15
+ def log_start(task: str, model: str):
16
+ print(f"[START] task={task} env=aion-code-reviewer model={model}", flush=True)
17
+
18
+ def log_step(step: int, action_type: str, reward: float, done: bool, error: str = "null"):
19
+ done_str = str(done).lower()
20
+ print(f"[STEP] step={step} action={action_type} reward={reward:.2f} done={done_str} error={error}", flush=True)
21
+
22
+ def log_end(success: bool, steps: int, score: float, rewards: list):
23
+ rewards_str = ",".join(f"{r:.2f}" for r in rewards)
24
+ success_str = str(success).lower()
25
+ print(f"[END] success={success_str} steps={steps} score={score:.3f} rewards={rewards_str}", flush=True)
26
+
27
+ async def run_inference():
28
+ env = CodeReviewEnv()
29
+ tasks = ["style-cleanup", "efficiency-boost", "security-audit"]
30
+
31
+ for task_id in tasks:
32
+ obs = env.reset(task_id=task_id)
33
+ log_start(task_id, MODEL_NAME)
34
+
35
+ step_idx = 1
36
+ total_rewards = []
37
+ is_success = False
38
+
39
+ while step_idx <= 5:
40
+ # 2. Build the Prompt for the LLM
41
+ prompt = f"""
42
+ Task: {obs.current_task}
43
+ Current Code:
44
+ {obs.code_content}
45
+ Linter Errors: {obs.linter_report}
46
+
47
+ Return a JSON object with:
48
+ {{
49
+ "action_type": "apply_fix" | "submit",
50
+ "content": "the entire corrected code string",
51
+ "line_number": 0
52
+ }}
53
+ """
54
+
55
+ try:
56
+ response = client.chat.completions.create(
57
+ model=MODEL_NAME,
58
+ messages=[{"role": "user", "content": prompt}],
59
+ response_format={ "type": "json_object" }
60
+ )
61
+
62
+ # 3. Parse LLM Response
63
+ data = json.loads(response.choices[0].message.content)
64
+ agent_action = Action(**data)
65
+
66
+ # 4. Take Step in Env
67
+ obs, reward, done, info = env.step(agent_action)
68
+ total_rewards.append(reward)
69
+
70
+ log_step(step_idx, agent_action.action_type, reward, done)
71
+
72
+ if done:
73
+ is_success = reward >= 0.8 # Threshold for success
74
+ break
75
+
76
+ step_idx += 1
77
+ except Exception as e:
78
+ log_step(step_idx, "error", 0.0, True, error=str(e))
79
+ break
80
+
81
+ log_end(is_success, step_idx, max(total_rewards) if total_rewards else 0.0, total_rewards)
82
+
83
+ if __name__ == "__main__":
84
+ asyncio.run(run_inference())
models.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import BaseModel, Field
2
+ from typing import Optional, List
3
+
4
+ class Action(BaseModel):
5
+ # The agent chooses what to do
6
+ action_type: str = Field(..., description="Values: 'comment', 'apply_fix', or 'submit'")
7
+ line_number: Optional[int] = Field(None, description="The line number to act upon")
8
+ content: str = Field(..., description="The text of the comment or the code to replace")
9
+
10
+ class Observation(BaseModel):
11
+ # What the agent 'sees' every turn
12
+ file_name: str
13
+ code_content: str
14
+ diff: str
15
+ linter_report: List[str]
16
+ current_task: str
17
+
18
+ class Reward(BaseModel):
19
+ # The score given back to the agent
20
+ value: float = Field(..., ge=0.0, le=1.0)
21
+ comment: str
openenv.yaml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: "1.0.0"
2
+ name: "aion-code-reviewer"
3
+ description: "An AI-powered sandbox for evaluating code review and bug-fixing capabilities."
4
+ tasks:
5
+ - id: "style-cleanup"
6
+ name: "Style & Linting"
7
+ difficulty: "easy"
8
+ description: "Fix indentation and remove unused imports."
9
+ - id: "efficiency-boost"
10
+ name: "Algorithm Optimization"
11
+ difficulty: "medium"
12
+ description: "Refactor a nested loop into a more efficient lookup."
13
+ - id: "security-audit"
14
+ name: "Security Vulnerability"
15
+ difficulty: "hard"
16
+ description: "Identify and fix a SQL injection vulnerability."
requirements.txt ADDED
File without changes