bstraehle's picture
Update multi_agent.py
febf589 verified
raw
history blame
3.11 kB
import base64, os
from autogen import ConversableAgent, AssistantAgent
from autogen.coding import LocalCommandLineCodeExecutor
def read_image_file(image_file_path: str) -> str:
with open(image_file_path, "rb") as image_file:
image_data = image_file.read()
return base64.b64encode(image_data).decode("utf-8")
def generate_markdown_code(image_data: str) -> str:
return f"![Image](data:image/png;base64,{image_data})"
def read_python_file(file_path: str) -> str:
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def read_py_file(folder_path: str, extension: str) -> str:
for filename in os.listdir(folder_path):
if filename.endswith(extension):
with open(os.path.join(folder_path, filename), "r", encoding="utf-8") as file:
return file.read()
def format_as_markdown(code: str) -> str:
"""
Formats a string of Python code as Markdown.
Args:
code (str): The Python code to format.
Returns:
str: The formatted Markdown string.
"""
markdown_code = '```\n'
markdown_code += code
markdown_code += '\n```'
return markdown_code
def run_multi_agent(llm, message):
llm_config = {"model": llm}
executor = LocalCommandLineCodeExecutor(
timeout=60,
work_dir="coding",
)
code_executor_agent = ConversableAgent(
name="code_executor_agent",
llm_config=False,
code_execution_config={"executor": executor},
human_input_mode="NEVER",
default_auto_reply="Please continue. If everything is done, reply 'TERMINATE'.",
)
print("### code_executor_agent.system_message = " + code_executor_agent.system_message)
code_writer_agent = AssistantAgent(
name="code_writer_agent",
llm_config=llm_config,
code_execution_config=False,
human_input_mode="NEVER",
)
print("### code_writer_agent.system_message = " + code_writer_agent.system_message)
chat_result = code_executor_agent.initiate_chat(
code_writer_agent,
message=message,
max_turns=10
)
image_data = read_image_file("/home/user/app/coding/ytd_stock_gains.png")
markdown_code = generate_markdown_code(image_data)
print("### markdown_code = " + markdown_code)
current_folder = os.getcwd()
print(current_folder)
files_in_folder = os.listdir("coding")
print(f"Files in {current_folder}:")
for file in files_in_folder:
print(file)
#file_name_py = read_py_file("coding", ".py")
#file_name_sh = read_py_file("coding", ".sh")
#print(file_name_py)
#print(file_name_sh)
#file_path_py = "coding/" + file_name_py
#code = read_python_file(file_path_py)
#markdown_code = format_as_markdown(code)
#file_path_sh = "coding/" + file_name_sh
#code = read_python_file(file_path_sh)
#markdown_code += "<br />" + format_as_markdown(code)
#print("### markdown_code = " + markdown_code)
#print("###")
#print(chat_result)
#print("###")
return markdown_code