OperationcodeCreatorAgent:ソース:tools:PythonProgramerAgent.py


from tools.AIAgent import AIAgent
from tools.command_list import load_ai_agent_name_list
import tools.command_list 
import flow.flow_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 = flow.flow_controller.python_error_check(respons_text)
            if False is flow.flow_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