code generator utils
This commit is contained in:
parent
769b58c929
commit
ac4381cae9
|
|
@ -65,9 +65,9 @@ flowchart TD
|
||||||
- Used by all LLM-powered nodes for generating tests, code, and analysis
|
- Used by all LLM-powered nodes for generating tests, code, and analysis
|
||||||
|
|
||||||
2. **Execute Python Code** (`utils/code_executor.py`)
|
2. **Execute Python Code** (`utils/code_executor.py`)
|
||||||
- *Input*: function_code (str), test_case (dict)
|
- *Input*: function_code (str), input (dict/list/any)
|
||||||
- *Output*: test_result (dict with passed, failed, error details)
|
- *Output*: output (any), error (str)
|
||||||
- Used by RunTests batch node to safely execute generated code against individual test cases
|
- Used by RunTests batch node to safely execute generated code against individual input
|
||||||
|
|
||||||
## Node Design
|
## Node Design
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
anthropic
|
||||||
|
pocketflow
|
||||||
|
pyyaml
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
from anthropic import Anthropic
|
||||||
|
import os
|
||||||
|
|
||||||
|
def call_llm(prompt):
|
||||||
|
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY", "your-api-key"))
|
||||||
|
response = client.messages.create(
|
||||||
|
model="claude-sonnet-4-20250514",
|
||||||
|
max_tokens=6000,
|
||||||
|
messages=[
|
||||||
|
{"role": "user", "content": prompt}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return response.content[0].text
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print("## Testing call_llm")
|
||||||
|
prompt = "In a few words, what is the meaning of life?"
|
||||||
|
print(f"## Prompt: {prompt}")
|
||||||
|
response = call_llm(prompt)
|
||||||
|
print(f"## Response: {response}")
|
||||||
|
|
@ -0,0 +1,56 @@
|
||||||
|
import sys
|
||||||
|
import io
|
||||||
|
import traceback
|
||||||
|
from contextlib import redirect_stdout, redirect_stderr
|
||||||
|
|
||||||
|
def execute_python(function_code, input):
|
||||||
|
try:
|
||||||
|
namespace = {"__builtins__": __builtins__}
|
||||||
|
stdout_capture = io.StringIO()
|
||||||
|
stderr_capture = io.StringIO()
|
||||||
|
|
||||||
|
with redirect_stdout(stdout_capture), redirect_stderr(stderr_capture):
|
||||||
|
exec(function_code, namespace)
|
||||||
|
|
||||||
|
if "run_code" not in namespace:
|
||||||
|
return None, "Function 'run_code' not found"
|
||||||
|
|
||||||
|
run_code = namespace["run_code"]
|
||||||
|
|
||||||
|
if isinstance(input, dict):
|
||||||
|
result = run_code(**input)
|
||||||
|
elif isinstance(input, (list, tuple)):
|
||||||
|
result = run_code(*input)
|
||||||
|
else:
|
||||||
|
result = run_code(input)
|
||||||
|
|
||||||
|
return result, None
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return None, f"{type(e).__name__}: {str(e)}"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Test 1: Working function
|
||||||
|
function_code = """
|
||||||
|
def run_code(nums, target):
|
||||||
|
for i in range(len(nums)):
|
||||||
|
for j in range(i + 1, len(nums)):
|
||||||
|
if nums[i] + nums[j] == target:
|
||||||
|
return [i, j]
|
||||||
|
return []
|
||||||
|
"""
|
||||||
|
|
||||||
|
input = {"nums": [2, 7, 11, 15], "target": 9}
|
||||||
|
output, error = execute_python(function_code, input)
|
||||||
|
print(f"Output: {output}")
|
||||||
|
print(f"Error: {error}")
|
||||||
|
|
||||||
|
# Test 2: Function with error
|
||||||
|
broken_function_code = """
|
||||||
|
def run_code(nums, target):
|
||||||
|
return nums[100] # Index error
|
||||||
|
"""
|
||||||
|
|
||||||
|
output2, error2 = execute_python(broken_function_code, input)
|
||||||
|
print(f"Output: {output2}")
|
||||||
|
print(f"Error: {error2}")
|
||||||
Loading…
Reference in New Issue