Skip to content

Commit 13bbb00

Browse files
committed
Use openroute
1 parent 21a3c5a commit 13bbb00

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

math_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
def aaa(a):
3+
return a + 1
4+
5+
def calculate_average(numbers):
6+
return sum(numbers) / len(numbers)
7+
8+
def bbbaaa(a):
9+
print(a)
10+
return a + 1
11+

poc_python_search_replace.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
import autogen
2-
from typing import List, Tuple
2+
from typing import List, Tuple, Dict
33
import re
44
import tempfile
55
import os
66
import pytest
77
import shutil
8+
import requests
9+
import json
10+
from llm.openrouter import OpenRouterLLM
11+
12+
class CustomAssistantAgent(autogen.AssistantAgent):
13+
def __init__(self, name: str, llm_config: dict, openrouter_llm: OpenRouterLLM):
14+
super().__init__(name=name, llm_config=llm_config)
15+
self.openrouter_llm = openrouter_llm
16+
17+
def generate_reply(self, messages: List[Dict]):
18+
"""Override the default generate_reply to use OpenRouterLLM"""
19+
try:
20+
response = self.openrouter_llm.generate_response(messages)
21+
return response
22+
except Exception as e:
23+
print(f"Error generating reply: {e}")
24+
return None
825

926
class CodeModifier:
10-
def __init__(self, temperature=0.7):
27+
def __init__(self, api_key: str):
28+
# Initialize OpenRouterLLM
29+
self.llm = OpenRouterLLM(api_key)
30+
1131
# Configure the assistant and user proxies
12-
self.assistant = autogen.AssistantAgent(
32+
self.assistant = CustomAssistantAgent(
1333
name="assistant",
1434
llm_config={
15-
"temperature": temperature,
16-
"config_list": [{"model": "gpt-4"}],
17-
}
35+
"temperature": 0.1,
36+
},
37+
openrouter_llm=self.llm
1838
)
19-
39+
2040
self.user_proxy = autogen.UserProxyAgent(
2141
name="user_proxy",
2242
human_input_mode="NEVER",
@@ -30,9 +50,9 @@ def apply_diff(self, file_path: str, diff_block: str) -> bool:
3050
content = f.read()
3151

3252
# Parse the diff block
33-
search_pattern = re.search(r'<<<<<<< SEARCH\n(.*?)\n=======\n(.*?)\n>>>>>>> REPLACE',
53+
search_pattern = re.search(r'<<<<<<< SEARCH\n(.*?)\n=======\n(.*?)\n>>>>>>> REPLACE',
3454
diff_block, re.DOTALL)
35-
55+
3656
if not search_pattern:
3757
return False
3858

@@ -61,13 +81,13 @@ def run_tests(self, test_file: str) -> Tuple[bool, str]:
6181

6282
def process_code(self, problem_description: str, target_files: List[str], max_iterations: int = 5) -> bool:
6383
"""Main workflow to process code modifications and testing."""
64-
84+
6585
# Create a temporary working directory
6686
with tempfile.TemporaryDirectory() as temp_dir:
6787
# Copy target files to workspace
6888
for file_path in target_files:
6989
shutil.copy(file_path, temp_dir)
70-
90+
7191
iteration = 0
7292
while iteration < max_iterations:
7393
# Generate conversation messages
@@ -76,14 +96,14 @@ def process_code(self, problem_description: str, target_files: List[str], max_it
7696
"role": "user",
7797
"content": f"""
7898
Problem: {problem_description}
79-
99+
80100
Current code files:
81101
{self._read_files(target_files)}
82-
102+
83103
Please generate:
84104
1. Code modifications in diff format
85105
2. Pytest test cases
86-
106+
87107
Use this diff format:
88108
<<<<<<< SEARCH
89109
(original code)
@@ -154,15 +174,15 @@ def _extract_test_code(self, response: str) -> str:
154174

155175
# Example usage
156176
if __name__ == "__main__":
157-
modifier = CodeModifier()
158-
177+
api_key = os.getenv('OPENROUTER_API_KEY')
178+
modifier = CodeModifier(api_key=api_key)
179+
159180
problem = """
160-
Fix the implementation of the calculate_average function to handle empty lists
181+
Fix the implementation of the calculate_average function to handle empty lists
161182
and return the correct average of numbers.
162183
"""
163-
184+
164185
target_files = ["math_utils.py"]
165-
186+
166187
success = modifier.process_code(problem, target_files)
167188
print(f"Code modification {'succeeded' if success else 'failed'}")
168-

0 commit comments

Comments
 (0)