| 1 | from abc import ABC, abstractmethod |
| 2 | from graphrag_sdk.models.model import GenerativeModelChatSession |
| 3 | |
| 4 | |
| 5 | class AgentResponseCode: |
| 6 | """ |
| 7 | Represents the response codes for an agent. |
| 8 | """ |
| 9 | |
| 10 | AGENT_RESPONSE = "agent_response" |
| 11 | AGENT_ERROR = "agent_error" |
| 12 | AGENT_REQUEST_INPUT = "agent_request_input" |
| 13 | |
| 14 | @staticmethod |
| 15 | def from_str(text: str) -> "AgentResponseCode": |
| 16 | """ |
| 17 | Converts a string to an AgentResponseCode object. |
| 18 | |
| 19 | Args: |
| 20 | text (str): The string representation of the response code. |
| 21 | |
| 22 | Returns: |
| 23 | AgentResponseCode: The corresponding AgentResponseCode object. |
| 24 | |
| 25 | Raises: |
| 26 | ValueError: If the input string does not match any known response code. |
| 27 | """ |
| 28 | if text == AgentResponseCode.AGENT_RESPONSE: |
| 29 | return AgentResponseCode.AGENT_RESPONSE |
| 30 | elif text == AgentResponseCode.AGENT_ERROR: |
| 31 | return AgentResponseCode.AGENT_ERROR |
| 32 | elif text == AgentResponseCode.AGENT_REQUEST_INPUT: |
| 33 | return AgentResponseCode.AGENT_REQUEST_INPUT |
| 34 | else: |
| 35 | raise ValueError(f"Unknown agent response code: {text}") |
| 36 | |
| 37 | |
| 38 | class AgentResponse: |
| 39 | """ |
| 40 | Represents a response from an agent. |
| 41 | |
| 42 | Attributes: |
| 43 | response_code (AgentResponseCode): The response code. |
| 44 | payload (dict): The payload of the response. |
| 45 | """ |
| 46 | |
| 47 | def __init__(self, response_code: AgentResponseCode, payload: dict): |
| 48 | """ |
| 49 | Initializes a new Agent object. |
| 50 | |
| 51 | Args: |
| 52 | response_code (AgentResponseCode): The response code of the agent. |
| 53 | payload (dict): The payload associated with the agent. |
| 54 | |
| 55 | """ |
| 56 | self.response_code = response_code |
| 57 | self.payload = payload |
| 58 | |
| 59 | def to_json(self) -> dict: |
| 60 | """ |
| 61 | Converts the AgentResponse object to a JSON-compatible dictionary. |
| 62 | |
| 63 | Returns: |
| 64 | dict: The JSON representation of the AgentResponse object. |
| 65 | """ |
| 66 | return { |
| 67 | "response_code": self.response_code, |
| 68 | "payload": self.payload, |
| 69 | } |
| 70 | |
| 71 | @staticmethod |
| 72 | def from_json(json: dict) -> "AgentResponse": |
| 73 | """ |
| 74 | Creates an AgentResponse object from a JSON-compatible dictionary. |
| 75 | |
| 76 | Args: |
| 77 | json (dict): The JSON representation of the AgentResponse object. |
| 78 | |
| 79 | Returns: |
| 80 | AgentResponse: The created AgentResponse object. |
| 81 | """ |
| 82 | return AgentResponse( |
| 83 | AgentResponseCode.from_str(json["response_code"]), |
| 84 | json["payload"], |
| 85 | ) |
| 86 | |
| 87 | def __str__(self) -> str: |
| 88 | """ |
| 89 | Returns a string representation of the AgentResponse object. |
| 90 | |
| 91 | Returns: |
| 92 | str: The string representation of the AgentResponse object. |
| 93 | """ |
| 94 | return ( |
| 95 | f"AgentResponse(response_code={self.response_code}, payload={self.payload})" |
| 96 | ) |
| 97 | |
| 98 | def __repr__(self) -> str: |
| 99 | """ |
| 100 | Returns a string representation of the AgentResponse object. |
| 101 | |
| 102 | Returns: |
| 103 | str: The string representation of the AgentResponse object. |
| 104 | """ |
| 105 | return str(self) |
| 106 | |
| 107 | |
| 108 | class Agent(ABC): |
| 109 | """ |
| 110 | Abstract base class for agents in the system. |
| 111 | """ |
| 112 | |
| 113 | @property |
| 114 | @abstractmethod |
| 115 | def agent_id(self) -> str: |
| 116 | """ |
| 117 | Get the unique identifier of the agent. |
| 118 | |
| 119 | Returns: |
| 120 | str: The agent's identifier. |
| 121 | """ |
| 122 | pass |
| 123 | |
| 124 | @property |
| 125 | @abstractmethod |
| 126 | def introduction(self) -> str: |
| 127 | """ |
| 128 | Get the introduction of the agent. |
| 129 | |
| 130 | Returns: |
| 131 | str: The agent's introduction. |
| 132 | """ |
| 133 | pass |
| 134 | |
| 135 | @property |
| 136 | @abstractmethod |
| 137 | def interface(self) -> list[dict]: |
| 138 | """ |
| 139 | Get the interface of the agent. |
| 140 | |
| 141 | Returns: |
| 142 | List[Dict]: The agent's interface. |
| 143 | """ |
| 144 | pass |
| 145 | |
| 146 | @abstractmethod |
| 147 | def run( |
| 148 | self, params: dict, session: GenerativeModelChatSession |
| 149 | ) -> tuple[str, GenerativeModelChatSession]: |
| 150 | """ |
| 151 | Run the agent with the given parameters and chat session. |
| 152 | |
| 153 | Args: |
| 154 | params (Dict): The parameters for the agent. |
| 155 | session (GenerativeModelChatSession): The chat session. |
| 156 | |
| 157 | Returns: |
| 158 | Tuple[str, GenerativeModelChatSession]: The agent's response and the updated chat session. |
| 159 | """ |
| 160 | pass |
| 161 | |
| 162 | @abstractmethod |
| 163 | def __repr__(self) -> str: |
| 164 | """ |
| 165 | Get a string representation of the agent. |
| 166 | |
| 167 | Returns: |
| 168 | str: The string representation of the agent. |
| 169 | """ |
| 170 | pass |