OperationcodeCreatorAgent:ソース:tools:FunctionExecuterAgent.py

EmotionalBehaviorAgent.pyから抜き出しただけでまだ動作確認していない。


from tools.AIAgent import AIAgent
from tools.command_list import load_ai_agent_name_list
from tools.fllow_controller import get_python_line_start_sharp_comment_from_respons

from tools.program_called_command_list import get_python_code
from tools.program_called_command_list import load_python_program_list
from tools.program_called_command_list import get_python_program_list

import tools.command_list 
import streamlit as st

DO_PYTHON_PROGRAM = 0
MAKE_PYTHON_PROGRAM = 1
DEEP_THINK = 2


class FunctionExecuterAgent():
    def __init__(self, max_check=3):
        name = "FunctionExecuter"
        self.agent_dict = load_ai_agent_name_list("FunctionExecuter")
        print(self.agent_dict)
        self.agent = AIAgent(name,
                             "",
                             [],
                             False)
        self.max_check = max_check
        self.temperature = 0.5

        self.latest_respons = ""

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

    # AIAgentと関数的互換性確保のために必要
    def get_respons(self, command):
        return self.FunctionExecute(command)
    
    def clear_memory(self):
        self.agent.clear_memory()

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

    def update_tools(self, tools):
        self.agent.update_tools(tools)
################################################

    def FunctionExecute(self, command):
        program_list_prompt = get_python_program_list()
        if "" == program_list_prompt:
            pre_promput = "現在扱える機能はありません。\r\n"
            return pre_promput
        else:
            pre_promput = "現在扱える機能は\r\n"+program_list_prompt
        # 選択
        self.agent.update_system_prompt(pre_promput+self.__get_agent_prompt("FunctionSelecter"))
        # コールバック関数の設定 (エージェントの動作の可視化用)
        tools.command_list.g_time_keeper.wait()
        respons = self.agent.get_respons("")
        func_name_list = get_python_line_start_sharp_comment_from_respons(respons)
        code = get_python_code(func_name_list[0])
        self.agent.update_system_prompt(pre_promput+self.__get_agent_prompt("PythonProgramExecuter"))
        tools.command_list.g_time_keeper.wait()
        respons = self.agent.get_respons(code)

        error, respons = self.respons_programe_check(respons)
        self.latest_respons = respons

        return respons

    def __get_python_program_timeout(self, respons):
        """ pythonプログラムの実行の時に実行すること。
        timeoutを取得
        """
        code = tools.fllow_controller.get_lengest_program_code_from_response(respons)
        if "python" in code:
            program = code["python"]
        else:
            return None
        if list is not type(program):
            program = program.split("\n")
        count = 0
        for line in program:
            l_line = line.rstrip()

            if 0 < len(l_line):
                if "#" == l_line[0]:
                    l_line = l_line[1:]
                    l_line = l_line.strip()
                    data = l_line.split("=")
                    if 1 < len(data):
                        if "timeout" == data[0]:
                            return float(data[1])
            count += 1      
            if 4 < count:
                return None
        return None

#########################################
    def __programe_check_sub(self, respons_text, error_respons, count):
        if False is tools.fllow_controller.get_code_error():
             error_respons = ""
             return False, error_respons, respons_text, count
        if self.max_check <= count:
            error_respons = "3回以上やり直してもエラーを修正しきれませんでした。"
            return False, error_respons, respons_text, count
        print(error_respons)
        tools.command_list.g_time_keeper.wait()
        if 0<=error_respons.find("ソースコードが前から変わっていません。"):
            self.update_temperature(1.0)
        respons_text = self.agent.get_respons(error_respons+"\r\n直してください。必要ならrun_pip_commandを使用してください。")
        count += 1
        return True, error_respons, respons_text, count

    def respons_programe_check(self, check_prompt, timeout=5):
        # pythonで エラーが出るようなら自動で作りなおしを依頼。

        count = 0
        respons_text = check_prompt
        loop_flag = True
        error_respons = ""
        while loop_flag:
            error_respons = tools.fllow_controller.python_error_check(respons_text, error_respons,timeout)
            loop_flag, error_respons, respons_text, count = self.__programe_check_sub(respons_text, error_respons, count)
            count += 1
        return error_respons, respons_text

#################################
    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