Skip to content

Commit 1a1fd19

Browse files
Add Multi-Agent System For Iterative Code Generation, Execution, And Debugging
1 parent 319a2a8 commit 1a1fd19

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ print(client.get_languages())
236236
#### Simple Example with Ollama
237237

238238
```python
239+
# pip install judge0 ollama
239240
import os
240241
from ollama import Client
241242
import judge0
@@ -271,6 +272,7 @@ print(f"CODE EXECUTION RESULT:\n{result.stdout}")
271272
#### Tool Calling (a.k.a. Function Calling) with Ollama
272273

273274
```python
275+
# pip install judge0 ollama
274276
import os
275277
from ollama import Client
276278
import judge0
@@ -329,6 +331,87 @@ final_response = client.chat(model=model, messages=messages)
329331
print(f'FINAL RESPONSE BY THE MODEL:\n{final_response["message"]["content"]}')
330332
```
331333

334+
#### Multi-Agent System For Iterative Code Generation, Execution, And Debugging
335+
336+
```python
337+
# pip install judge0 ag2[openai]
338+
from typing import Annotated, Optional
339+
340+
from autogen import ConversableAgent, LLMConfig, register_function
341+
from autogen.tools import Tool
342+
from pydantic import BaseModel, Field
343+
import judge0
344+
345+
346+
class PythonCodeExecutionTool(Tool):
347+
def __init__(self) -> None:
348+
class CodeExecutionRequest(BaseModel):
349+
code: Annotated[str, Field(description="Python code to execute")]
350+
351+
async def execute_python_code(
352+
code_execution_request: CodeExecutionRequest,
353+
) -> Optional[str]:
354+
result = judge0.run(
355+
source_code=code_execution_request.code,
356+
language=judge0.PYTHON,
357+
redirect_stderr_to_stdout=True,
358+
)
359+
return result.stdout
360+
361+
super().__init__(
362+
name="python_execute_code",
363+
description="Executes Python code and returns the result.",
364+
func_or_tool=execute_python_code,
365+
)
366+
367+
368+
python_executor = PythonCodeExecutionTool()
369+
370+
llm_config = LLMConfig(
371+
{
372+
"api_type": "openai",
373+
"base_url": "http://localhost:11434/v1",
374+
"api_key": "ollama",
375+
"model": "qwen3-coder:30b",
376+
}
377+
)
378+
379+
code_runner = ConversableAgent(
380+
name="code_runner",
381+
system_message="You are a code executor agent, when you don't execute code write the message 'TERMINATE' by itself.",
382+
human_input_mode="NEVER",
383+
llm_config=llm_config,
384+
)
385+
386+
question_agent = ConversableAgent(
387+
name="question_agent",
388+
system_message=(
389+
"You are a developer AI agent. "
390+
"Send all your code suggestions to the python_executor tool where it will be executed and result returned to you. "
391+
"Keep refining the code until it works."
392+
),
393+
llm_config=llm_config,
394+
)
395+
396+
register_function(
397+
python_executor,
398+
caller=question_agent,
399+
executor=code_runner,
400+
description="Run Python code",
401+
)
402+
403+
result = code_runner.initiate_chat(
404+
recipient=question_agent,
405+
message=(
406+
"Write Python code to print the current Python version followed by the numbers 1 to 11. "
407+
"Make a syntax error in the first version and fix it in the second version."
408+
),
409+
max_turns=5,
410+
)
411+
412+
print(f"Result: {result.summary}")
413+
```
414+
332415
### Filesystem
333416

334417
This example shows how to use Judge0 Python SDK to:

0 commit comments

Comments
 (0)