Agent Select:ソースコード:PythonProgramerAgent.py

pythonプログラムを作成します。エラーが出ている間は、エラーコードと合わせて”直して”という指示を自動で出し、修正を求め続けます。

update_temperatureが追加されています。


from tools.AIAgent import AIAgent
from tools.command_list import load_ai_agent_name_list
import tools.command_list 
import tools.fllow_controller
import streamlit as st

from selenium.webdriver.common.by import By


class PythonProgramerAgent():
    def __init__(self, max_check=3):
        name = "PythonProgramer"
        self.agetn_dict = load_ai_agent_name_list("PythonProgramer")
        self.agent = AIAgent(name,
                             self.__get_agent_prompt(name),
                             [],
                             False)
        self.serch_result = []
        self.max_check = max_check

    def __get_agent_prompt(self, name):
        return self.agetn_dict[name][1]

    # AIAgentと関数的互換性確保のために必要
    def get_respons(self, command):
        error_respons, respons_text = self.program(command)
        return respons_text

    def clear_memory(self):
        self.agent.clear_memory()

    def update_temperature(self, temperature):
        self.agent.update_temperature(temperature)
################################################

    def program(self, command):
        self.agent.update_system_prompt(
            self.__get_agent_prompt("PythonProgramer"))
        # コールバック関数の設定 (エージェントの動作の可視化用)
        tools.command_list.g_time_keeper.wait()
        respons = self.agent.get_respons(command)
        return self.respons_programe_check(respons)

    def respons_programe_check(self, check_prompt):
        # 次の作業者を決定。
        count = 0
        respons_text = check_prompt
        # pythonで エラーが出るようなら自動で作りなおしを依頼。
       
        while True:
            error_respons = tools.fllow_controller.python_error_check(respons_text)
            if False is tools.fllow_controller.get_code_error():
                error_respons = ""
                return error_respons, respons_text
            if self.max_check <= count:
                error_respons = "3回以上やり直してもエラーを修正しきれませんでした。"
                return error_respons, respons_text
            print(error_respons)

            respons_text = self.think_agent(error_respons+"\r\n直してください。")
            count += 1

    def think_agent(self, prompt):
        tools.command_list.g_time_keeper.wait()
        # AIエージェントの思考
        with st.chat_message("PythonProgramer"):
            # エージェントを実行
            response = self.agent.get_respons(prompt)
            st.write(response)
        return response

以下 AIによる説明

PythonProgramerAgent クラス マニュアル

概要

PythonProgramerAgent クラスは、LangChain の AIAgent をベースに、Python プログラムの生成と修正を専門とするエージェントです。このクラスは、Python コードのエラーチェックと修正を自動化し、ユーザーがより簡単に Python プログラムを作成できるように支援します。

コンストラクタ

  • __init__(self, max_check=3): PythonProgramerAgent オブジェクトを初期化します。
    • max_check: エラー修正の最大試行回数 (デフォルトは 3)。

メソッド

  • __get_agent_prompt(self, name): 指定されたエージェントの名前のシステムプロンプトを取得します。
  • get_respons(self, command): AIAgent との関数的互換性確保のために必要です。
  • clear_memory(self): エージェントのメモリをクリアします。
  • update_temperature(self, temperature): エージェントの温度を更新します。
  • program(self, command): ユーザーからのコマンドを受け取り、Python プログラムを生成します。
  • respons_programe_check(self, check_prompt): 生成された Python コードのエラーチェックを行い、必要に応じて修正を依頼します。
  • think_agent(self, prompt): エージェントにプロンプトを与え、思考を実行します。

使用方法

  1. PythonProgramerAgent オブジェクトを作成します。
  2. program() メソッドにユーザーからのコマンドを渡します。
  3. エージェントは、コマンドに基づいて Python プログラムを生成します。
  4. respons_programe_check() メソッドは、生成されたコードのエラーチェックを行い、必要に応じて修正を依頼します。
  5. エージェントは、修正されたコードを返します。

# PythonProgramerAgent オブジェクトを作成
agent = PythonProgramerAgent()

# ユーザーからのコマンド
command = "Pythonで、リストの要素を逆順に並べ替えるプログラムを作成してください。"

# プログラムを生成
error_respons, respons_text = agent.program(command)

# 結果を表示
print(f"エラーメッセージ: {error_respons}")
print(f"生成されたコード: {respons_text}")

注意

  • このエージェントは、Python コードのエラーチェックと修正を自動化しますが、完璧ではありません。
  • エラー修正の最大試行回数は、max_check パラメータで設定できます。
  • エージェントの動作は、tools.command_list.g_time_keeper.wait() メソッドによって制限されています。

追加情報

  • このエージェントは、Streamlit を使用して、エージェントの動作を可視化しています。
  • エージェントのシステムプロンプトは、load_ai_agent_name_list() メソッドを使用して取得されます。
  • エラーチェックは、tools.fllow_controller.python_error_check() メソッドによって実行されます。