|
1 |
| -from typing import Dict, List, Optional, Union |
| 1 | +from typing import Any, Dict, List, Optional, Protocol, Union, runtime_checkable |
2 | 2 |
|
3 | 3 |
|
4 |
| -class Agent: |
5 |
| - """(In preview) An abstract class for AI agent. |
| 4 | +@runtime_checkable |
| 5 | +class Agent(Protocol): |
| 6 | + """(In preview) A protocol for Agent. |
6 | 7 |
|
7 | 8 | An agent can communicate with other agents and perform actions.
|
8 | 9 | Different agents can differ in what actions they perform in the `receive` method.
|
9 | 10 | """
|
10 | 11 |
|
11 |
| - def __init__( |
| 12 | + @property |
| 13 | + def name(self) -> str: |
| 14 | + """The name of the agent.""" |
| 15 | + ... |
| 16 | + |
| 17 | + @property |
| 18 | + def description(self) -> str: |
| 19 | + """The description of the agent. Used for the agent's introduction in |
| 20 | + a group chat setting.""" |
| 21 | + ... |
| 22 | + |
| 23 | + def send( |
12 | 24 | self,
|
13 |
| - name: str, |
14 |
| - ): |
15 |
| - """ |
| 25 | + message: Union[Dict[str, Any], str], |
| 26 | + recipient: "Agent", |
| 27 | + request_reply: Optional[bool] = None, |
| 28 | + ) -> None: |
| 29 | + """Send a message to another agent. |
| 30 | +
|
16 | 31 | Args:
|
17 |
| - name (str): name of the agent. |
| 32 | + message (dict or str): the message to send. If a dict, it should be |
| 33 | + a JSON-serializable and follows the OpenAI's ChatCompletion schema. |
| 34 | + recipient (Agent): the recipient of the message. |
| 35 | + request_reply (bool): whether to request a reply from the recipient. |
18 | 36 | """
|
19 |
| - # a dictionary of conversations, default value is list |
20 |
| - self._name = name |
| 37 | + ... |
21 | 38 |
|
22 |
| - @property |
23 |
| - def name(self): |
24 |
| - """Get the name of the agent.""" |
25 |
| - return self._name |
| 39 | + async def a_send( |
| 40 | + self, |
| 41 | + message: Union[Dict[str, Any], str], |
| 42 | + recipient: "Agent", |
| 43 | + request_reply: Optional[bool] = None, |
| 44 | + ) -> None: |
| 45 | + """(Async) Send a message to another agent. |
26 | 46 |
|
27 |
| - def send(self, message: Union[Dict, str], recipient: "Agent", request_reply: Optional[bool] = None): |
28 |
| - """(Abstract method) Send a message to another agent.""" |
| 47 | + Args: |
| 48 | + message (dict or str): the message to send. If a dict, it should be |
| 49 | + a JSON-serializable and follows the OpenAI's ChatCompletion schema. |
| 50 | + recipient (Agent): the recipient of the message. |
| 51 | + request_reply (bool): whether to request a reply from the recipient. |
| 52 | + """ |
| 53 | + ... |
29 | 54 |
|
30 |
| - async def a_send(self, message: Union[Dict, str], recipient: "Agent", request_reply: Optional[bool] = None): |
31 |
| - """(Abstract async method) Send a message to another agent.""" |
| 55 | + def receive( |
| 56 | + self, |
| 57 | + message: Union[Dict[str, Any], str], |
| 58 | + sender: "Agent", |
| 59 | + request_reply: Optional[bool] = None, |
| 60 | + ) -> None: |
| 61 | + """Receive a message from another agent. |
32 | 62 |
|
33 |
| - def receive(self, message: Union[Dict, str], sender: "Agent", request_reply: Optional[bool] = None): |
34 |
| - """(Abstract method) Receive a message from another agent.""" |
| 63 | + Args: |
| 64 | + message (dict or str): the message received. If a dict, it should be |
| 65 | + a JSON-serializable and follows the OpenAI's ChatCompletion schema. |
| 66 | + sender (Agent): the sender of the message. |
| 67 | + request_reply (bool): whether the sender requests a reply. |
| 68 | + """ |
35 | 69 |
|
36 |
| - async def a_receive(self, message: Union[Dict, str], sender: "Agent", request_reply: Optional[bool] = None): |
37 |
| - """(Abstract async method) Receive a message from another agent.""" |
| 70 | + async def a_receive( |
| 71 | + self, |
| 72 | + message: Union[Dict[str, Any], str], |
| 73 | + sender: "Agent", |
| 74 | + request_reply: Optional[bool] = None, |
| 75 | + ) -> None: |
| 76 | + """(Async) Receive a message from another agent. |
38 | 77 |
|
39 |
| - def reset(self): |
40 |
| - """(Abstract method) Reset the agent.""" |
| 78 | + Args: |
| 79 | + message (dict or str): the message received. If a dict, it should be |
| 80 | + a JSON-serializable and follows the OpenAI's ChatCompletion schema. |
| 81 | + sender (Agent): the sender of the message. |
| 82 | + request_reply (bool): whether the sender requests a reply. |
| 83 | + """ |
| 84 | + ... |
41 | 85 |
|
42 | 86 | def generate_reply(
|
43 | 87 | self,
|
44 |
| - messages: Optional[List[Dict]] = None, |
| 88 | + messages: Optional[List[Dict[str, Any]]] = None, |
45 | 89 | sender: Optional["Agent"] = None,
|
46 |
| - **kwargs, |
47 |
| - ) -> Union[str, Dict, None]: |
48 |
| - """(Abstract method) Generate a reply based on the received messages. |
| 90 | + **kwargs: Any, |
| 91 | + ) -> Union[str, Dict[str, Any], None]: |
| 92 | + """Generate a reply based on the received messages. |
49 | 93 |
|
50 | 94 | Args:
|
51 |
| - messages (list[dict]): a list of messages received. |
| 95 | + messages (list[dict]): a list of messages received from other agents. |
| 96 | + The messages are dictionaries that are JSON-serializable and |
| 97 | + follows the OpenAI's ChatCompletion schema. |
52 | 98 | sender: sender of an Agent instance.
|
| 99 | +
|
53 | 100 | Returns:
|
54 | 101 | str or dict or None: the generated reply. If None, no reply is generated.
|
55 | 102 | """
|
56 | 103 |
|
57 | 104 | async def a_generate_reply(
|
58 | 105 | self,
|
59 |
| - messages: Optional[List[Dict]] = None, |
| 106 | + messages: Optional[List[Dict[str, Any]]] = None, |
60 | 107 | sender: Optional["Agent"] = None,
|
61 |
| - **kwargs, |
62 |
| - ) -> Union[str, Dict, None]: |
63 |
| - """(Abstract async method) Generate a reply based on the received messages. |
| 108 | + **kwargs: Any, |
| 109 | + ) -> Union[str, Dict[str, Any], None]: |
| 110 | + """(Async) Generate a reply based on the received messages. |
64 | 111 |
|
65 | 112 | Args:
|
66 |
| - messages (list[dict]): a list of messages received. |
| 113 | + messages (list[dict]): a list of messages received from other agents. |
| 114 | + The messages are dictionaries that are JSON-serializable and |
| 115 | + follows the OpenAI's ChatCompletion schema. |
67 | 116 | sender: sender of an Agent instance.
|
| 117 | +
|
68 | 118 | Returns:
|
69 | 119 | str or dict or None: the generated reply. If None, no reply is generated.
|
70 | 120 | """
|
| 121 | + |
| 122 | + |
| 123 | +@runtime_checkable |
| 124 | +class LLMAgent(Agent, Protocol): |
| 125 | + """(In preview) A protocol for an LLM agent.""" |
| 126 | + |
| 127 | + @property |
| 128 | + def system_message(self) -> str: |
| 129 | + """The system message of this agent.""" |
| 130 | + |
| 131 | + def update_system_message(self, system_message: str) -> None: |
| 132 | + """Update this agent's system message. |
| 133 | +
|
| 134 | + Args: |
| 135 | + system_message (str): system message for inference. |
| 136 | + """ |
0 commit comments