|
from autogen import ConversableAgent, AssistantAgent |
|
from autogen.coding import LocalCommandLineCodeExecutor |
|
|
|
import base64, markdown, os |
|
|
|
|
|
def read_image_file(image_file_path: str) -> str: |
|
""" |
|
Read a PNG image file from the local filesystem and encode it as a base64 string. |
|
|
|
Args: |
|
image_file_path (str): The local file path of the image. |
|
|
|
Returns: |
|
str: The base64-encoded image data. |
|
""" |
|
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: |
|
""" |
|
Generate the markdown code to render an image. |
|
|
|
Args: |
|
image_data (str): The base64-encoded image data. |
|
|
|
Returns: |
|
str: The markdown code to render the image. |
|
""" |
|
return f"![Image](data:image/png;base64,{image_data})" |
|
|
|
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'.", |
|
) |
|
|
|
code_writer_agent = AssistantAgent( |
|
name="code_writer_agent", |
|
llm_config=llm_config, |
|
code_execution_config=False, |
|
human_input_mode="NEVER", |
|
) |
|
|
|
code_writer_agent_system_message = code_writer_agent.system_message |
|
|
|
print(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) |
|
|
|
return markdown_code |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|