OperationcodeCreatorAgent2:ソースコード:Agents:FunctionExecuterAgent.py


from Agents.AIAgent import AIAgent
from flow.flow_controller import get_python_line_start_sharp_comment_from_respons

from tools.program_called_command_list import load_ai_agent_name_list
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 flow.flow_controller

import streamlit as st

DO_PYTHON_PROGRAM = 0
MAKE_PYTHON_PROGRAM = 1
DEEP_THINK = 2


class FunctionExecuterAgent():
    def __init__(self, max_check=3):
        name = "FunctionSelecter"
        self.agent_dict = load_ai_agent_name_list("FunctionExecuterAgent")
        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()

    # AIAgentと関数的互換性確保のために必要
    def get_name(self):
        return FunctionExecuterAgent.__name__

    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 get_respons(self, command):
        return self.function_execute(command)
################################################

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

    def function_execute(self, command):
        program_list_prompt = get_python_program_list()
        if "" == program_list_prompt:
            pre_promput = command + "現在扱える機能はありません。\r\n"
            return pre_promput
        else:
            pre_promput = command + "現在扱える機能は\r\n"+program_list_prompt
        # 選択
        self.agent.update_system_prompt(
            pre_promput+self.__get_agent_prompt("FunctionSelecter"))
        # コールバック関数の設定 (エージェントの動作の可視化用)

        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"))

        respons = self.agent.get_respons(code)

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

        return respons

#########################################
    def __programe_check_sub(self, respons_text, error_respons, count):
        if False is flow.flow_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)

        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 = flow.flow_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):

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