File size: 2,376 Bytes
d8d14f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import subprocess
from typing import Any, Dict, List

from swarms.utils.loguru_logger import initialize_logger

from pydantic import BaseModel

from swarms.structs.agent import Agent

logger = initialize_logger(log_folder="pandas_utils")

try:
    import pandas as pd
except ImportError:
    logger.error("Failed to import pandas")
    subprocess.run(["pip", "install", "pandas"])
    import pandas as pd


def display_agents_info(agents: List[Agent]) -> None:
    """
    Displays information about all agents in a list using a DataFrame.

    :param agents: List of Agent instances.
    """
    # Extracting relevant information from each agent
    agent_data = []
    for agent in agents:
        try:
            agent_info = {
                "ID": agent.id,
                "Name": agent.agent_name,
                "Description": agent.description,
                "max_loops": agent.max_loops,
                # "Docs": agent.docs,
                "System Prompt": agent.system_prompt,
                "LLM Model": agent.llm.model_name,  # type: ignore
            }
            agent_data.append(agent_info)
        except AttributeError as e:
            logger.error(
                f"Failed to extract information from agent {agent}: {e}"
            )
            continue

    # Creating a DataFrame to display the data
    try:
        df = pd.DataFrame(agent_data)
    except Exception as e:
        logger.error(f"Failed to create DataFrame: {e}")
        return

    # Displaying the DataFrame
    try:
        print(df)
    except Exception as e:
        logger.error(f"Failed to print DataFrame: {e}")


def dict_to_dataframe(data: Dict[str, Any]) -> pd.DataFrame:
    """
    Converts a dictionary into a pandas DataFrame.

    :param data: Dictionary to convert.
    :return: A pandas DataFrame representation of the dictionary.
    """
    # Convert dictionary to DataFrame
    df = pd.json_normalize(data)
    return df


def pydantic_model_to_dataframe(model: BaseModel) -> pd.DataFrame:
    """
    Converts a Pydantic Base Model into a pandas DataFrame.

    :param model: Pydantic Base Model to convert.
    :return: A pandas DataFrame representation of the Pydantic model.
    """
    # Convert Pydantic model to dictionary
    model_dict = model.dict()

    # Convert dictionary to DataFrame
    df = dict_to_dataframe(model_dict)
    return df