このリポジトリは、ユーザーのプロンプトと対応する単体テストを受け取る LLM エージェントを実装します。エージェントはコードを生成し、それが正しく実行されるかどうかをテストし、単体テストに合格することを確認します。コードの実行が失敗するか単体テストに失敗した場合、エージェントは成功するまでコードを再生成します。
このプロジェクトは、LangChain のノートブック (https://github.com/mistralai/cookbook/blob/main/third_party/langchain/langgraph_code_assistant_mistral.ipynb) のコードに基づいて構築されており、エージェントはユーザーのプロンプトに基づいてコードを生成し、実行されるかどうかを確認します。動作します。
このプロジェクトの主な貢献は、単体テストをコード実行プロセスに統合したことです。
さらに、元の Jupyter ノートブック コードは、デプロイメントに適した構造化された Python プロジェクトにリファクタリングされています。ベスト コーディング プラクティスに従うことで、コードがモジュール化され、再利用可能な関数が作成され、適切な文書化とテストが保証されます。その結果、保守可能で運用の準備が整った、よく整理された .py プロジェクトが作成されます。
git clone https : // github . com / paulomuraroferreira / Self_correcting_coding_agent . git
cd Self_correcting_coding_agent
$ pip install - e .
.env ファイルを作成し、次の環境変数を入力します。
OPENAI_API_KEY = your_openai_api_key
OPENAI_CHAT_MODEL = "gpt-4o-2024-08-06"
コードのプロンプトを main.py ファイルに入力します。プロンプトは説明的であり、クラスの名前を指定する必要があります。
例えば:
QUESTION3 = '''
Python Class Description
Class Name: BankAccount
Description:
The BankAccount class represents a user's bank account, allowing for deposits, withdrawals, and viewing the transaction history. The class ensures that withdrawals cannot exceed the current balance and that deposits and withdrawals are properly recorded in the transaction history.
Attributes:
balance (float): The current balance of the account, initialized to 0.
transactions (list): A list to store the history of transactions. Each transaction is stored as a dictionary with keys type (either 'deposit' or 'withdrawal'), amount, and date.
Methods:
deposit(amount: float) -> None: Adds the specified amount to the balance and records the transaction.
withdraw(amount: float) -> bool: Attempts to subtract the specified amount from the balance. Returns True if successful, otherwise returns False. Records the transaction if successful.
get_balance() -> float: Returns the current balance.
get_transaction_history() -> list: Returns a list of all transactions.
'''
main = Main(QUESTION3)
単体テスト コードを src/unit_test_folder の .py ファイルに追加します。また、対応するクラスを code_solution.py ファイルからインポートします。たとえば、上記のプロンプトの場合、LLM は BankAccount というクラスを生成します。したがって、src/unit_test_folder/test_unit_test.py には以下を含める必要があります。
src.code_solution から BankAccount をインポート
main.py ファイルを実行します。
python main . py
上記の例の場合、コードは次の出力を表示します。
================================ Human Message =================================
Python Class Description
Class Name: BankAccount
Description:
The BankAccount class represents a user's bank account, allowing for deposits, withdrawals, and viewing the transaction history. The class ensures that withdrawals cannot exceed the current balance and that deposits and withdrawals are properly recorded in the transaction history.
Attributes:
balance (float): The current balance of the account, initialized to 0.
transactions (list): A list to store the history of transactions. Each transaction is stored as a dictionary with keys type (either 'deposit' or 'withdrawal'), amount, and date.
Methods:
deposit(amount: float) -> None: Adds the specified amount to the balance and records the transaction.
withdraw(amount: float) -> bool: Attempts to subtract the specified amount from the balance. Returns True if successful, otherwise returns False. Records the transaction if successful.
get_balance() -> float: Returns the current balance.
get_transaction_history() -> list: Returns a list of all transactions.
2024-08-18 14:51:24,955 - INFO - agent.py - ---GENERATING CODE SOLUTION---
================================== Ai Message ==================================
Here is my attempt to solve the problem: The BankAccount class is designed to manage a user's bank account by allowing deposits, withdrawals, and tracking transaction history. It ensures that withdrawals do not exceed the available balance and records each transaction with details such as type, amount, and date.
Imports: from datetime import datetime
Code: class BankAccount:
def __init__(self):
self.balance = 0.0
self.transactions = []
def deposit(self, amount: float) -> None:
self.balance += amount
transaction = {
'type': 'deposit',
'amount': amount,
'date': datetime.now()
}
self.transactions.append(transaction)
def withdraw(self, amount: float) -> bool:
if amount > self.balance:
return False
self.balance -= amount
transaction = {
'type': 'withdrawal',
'amount': amount,
'date': datetime.now()
}
self.transactions.append(transaction)
return True
def get_balance(self) -> float:
return self.balance
def get_transaction_history(self) -> list:
return self.transactions
2024-08-18 14:51:28,979 - INFO - agent.py - ---CHECKING CODE---
2024-08-18 14:51:29,170 - INFO - agent.py -
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 -- /home/paulo/Python_projects/Self_correcting_coding_agent/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/paulo/Python_projects/Self_correcting_coding_agent
configfile: pyproject.toml
plugins: anyio-4.4.0
collecting ... collected 7 items
src/unit_test_folder/test_unit_test.py::test_initial_balance PASSED [ 14%]
src/unit_test_folder/test_unit_test.py::test_deposit PASSED [ 28%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_successful PASSED [ 42%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_insufficient_funds PASSED [ 57%]
src/unit_test_folder/test_unit_test.py::test_transaction_history PASSED [ 71%]
src/unit_test_folder/test_unit_test.py::test_invalid_deposit FAILED [ 85%]
src/unit_test_folder/test_unit_test.py::test_invalid_withdrawal FAILED [100%]
=================================== FAILURES ===================================
_____________________________ test_invalid_deposit _____________________________
account = <src.code_solution.BankAccount object at 0x7fac0d4f0b90>
def test_invalid_deposit(account):
"""Test that depositing a negative amount raises ValueError."""
> with pytest.raises(ValueError):
E Failed: DID NOT RAISE <class 'ValueError'>
src/unit_test_folder/test_unit_test.py:48: Failed
___________________________ test_invalid_withdrawal ____________________________
account = <src.code_solution.BankAccount object at 0x7fac0d4f1f40>
def test_invalid_withdrawal(account):
"""Test that withdrawing a negative amount raises ValueError."""
> with pytest.raises(ValueError):
E Failed: DID NOT RAISE <class 'ValueError'>
src/unit_test_folder/test_unit_test.py:53: Failed
=========================== short test summary info ============================
FAILED src/unit_test_folder/test_unit_test.py::test_invalid_deposit - Failed:...
FAILED src/unit_test_folder/test_unit_test.py::test_invalid_withdrawal - Fail...
========================= 2 failed, 5 passed in 0.02s ==========================
2024-08-18 14:51:29,170 - INFO - agent.py - Some tests failed.
2024-08-18 14:51:29,172 - INFO - agent.py - ---DECISION: RE-TRY SOLUTION---
================================ Human Message =================================
Your solution failed the unit test: ============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 -- /home/paulo/Python_projects/Self_correcting_coding_agent/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/paulo/Python_projects/Self_correcting_coding_agent
configfile: pyproject.toml
plugins: anyio-4.4.0
collecting ... collected 7 items
src/unit_test_folder/test_unit_test.py::test_initial_balance PASSED [ 14%]
src/unit_test_folder/test_unit_test.py::test_deposit PASSED [ 28%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_successful PASSED [ 42%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_insufficient_funds PASSED [ 57%]
src/unit_test_folder/test_unit_test.py::test_transaction_history PASSED [ 71%]
src/unit_test_folder/test_unit_test.py::test_invalid_deposit FAILED [ 85%]
src/unit_test_folder/test_unit_test.py::test_invalid_withdrawal FAILED [100%]
=================================== FAILURES ===================================
_____________________________ test_invalid_deposit _____________________________
account = <src.code_solution.BankAccount object at 0x7fac0d4f0b90>
def test_invalid_deposit(account):
"""Test that depositing a negative amount raises ValueError."""
> with pytest.raises(ValueError):
E Failed: DID NOT RAISE <c ... (truncated)
2024-08-18 14:51:29,175 - INFO - agent.py - ---GENERATING CODE SOLUTION---
================================== Ai Message ==================================
Here is my attempt to solve the problem: The previous solution did not handle cases where negative amounts were deposited or withdrawn. The unit tests expect a ValueError to be raised in such cases, but the original implementation did not include this validation. To fix this, we need to add checks in the deposit and withdraw methods to raise a ValueError if the amount is negative.
Imports: from datetime import datetime
Code: class BankAccount:
def __init__(self):
self.balance = 0.0
self.transactions = []
def deposit(self, amount: float) -> None:
if amount < 0:
raise ValueError("Deposit amount must be positive.")
self.balance += amount
transaction = {
'type': 'deposit',
'amount': amount,
'date': datetime.now()
}
self.transactions.append(transaction)
def withdraw(self, amount: float) -> bool:
if amount < 0:
raise ValueError("Withdrawal amount must be positive.")
if amount > self.balance:
return False
self.balance -= amount
transaction = {
'type': 'withdrawal',
'amount': amount,
'date': datetime.now()
}
self.transactions.append(transaction)
return True
def get_balance(self) -> float:
return self.balance
def get_transaction_history(self) -> ... (truncated)
2024-08-18 14:51:34,913 - INFO - agent.py - ---CHECKING CODE---
2024-08-18 14:51:35,131 - INFO - agent.py -
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.3.2, pluggy-1.5.0 -- /home/paulo/Python_projects/Self_correcting_coding_agent/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/paulo/Python_projects/Self_correcting_coding_agent
configfile: pyproject.toml
plugins: anyio-4.4.0
collecting ... collected 7 items
src/unit_test_folder/test_unit_test.py::test_initial_balance PASSED [ 14%]
src/unit_test_folder/test_unit_test.py::test_deposit PASSED [ 28%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_successful PASSED [ 42%]
src/unit_test_folder/test_unit_test.py::test_withdrawal_insufficient_funds PASSED [ 57%]
src/unit_test_folder/test_unit_test.py::test_transaction_history PASSED [ 71%]
src/unit_test_folder/test_unit_test.py::test_invalid_deposit PASSED [ 85%]
src/unit_test_folder/test_unit_test.py::test_invalid_withdrawal PASSED [100%]
============================== 7 passed in 0.01s ===============================
2024-08-18 14:51:35,131 - INFO - agent.py - All tests passed!
2024-08-18 14:51:35,131 - INFO - agent.py - ---NO CODE TEST FAILURES---
2024-08-18 14:51:35,133 - INFO - agent.py - ---DECISION: FINISH---