Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autogen.agent -> autogen.agentchat #1148

Merged
merged 15 commits into from
Jul 29, 2023
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ Use the following guides to get started with FLAML in .NET:
# perform inference for a test instance
response = oai.Completion.create(context=test_instance, **config)
```
- LLM-driven intelligent agents which can perform tasks autonomously or with human feedback, including tasks that require using tools via code.
- LLM-driven intelligent agents which can collaborately perform tasks autonomously or with human feedback, including tasks that require using tools via code.
```python
assistant = AssistantAgent("assistant")
user = UserProxyAgent("user", human_input_mode="TERMINATE")
assistant.receive("Draw a rocket and save to a file named 'rocket.svg'")
user_proxy = UserProxyAgent("user_proxy")
user_proxy.initiate_chat("Show me the YTD gain of 10 largest technology companies as of today.")
```
* With three lines of code, you can start using this economical and fast
AutoML engine as a [scikit-learn style estimator](https://microsoft.github.io/FLAML/docs/Use-Cases/Task-Oriented-AutoML).
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Union
from typing import Dict, List, Optional, Union


class Agent:
Expand Down Expand Up @@ -33,12 +33,18 @@ def receive(self, message: Union[Dict, str], sender: "Agent"):
def reset(self):
"""(Abstract method) Reset the agent."""

def generate_reply(self, messages: List[Dict], default_reply: Union[str, Dict] = "") -> Union[str, Dict]:
def generate_reply(
self,
messages: Optional[List[Dict]] = None,
default_reply: Optional[Union[str, Dict]] = "",
sender: Optional["Agent"] = None,
) -> Union[str, Dict]:
"""(Abstract method) Generate a reply based on the received messages.

Args:
messages (list[dict]): a list of messages received.
default_reply (str or dict): the default reply if no other reply is generated.
sender: sender of an Agent instance.
Returns:
str or dict: the generated reply.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AssistantAgent(ResponsiveAgent):

DEFAULT_SYSTEM_MESSAGE = """You are a helpful AI assistant.
In the following cases, suggest python code (in a python coding block) or shell script (in a sh coding block) for the user to execute. You must indicate the script type in the code block. The user cannot provide any other feedback or perform any other action beyond executing the code you suggest. The user can't modify your code. So do not suggest incomplete code which requires users to modify. Don't use a code block if it's not intended to be executed by the user.
1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file.
1. When you need to collect info, use the code to output the info you need, for example, browse or search the web, download/read a file, print the content of a webpage or a file, get the current date/time.
2. When you need to perform some task with code, use the code to perform the task and output the result. Finish the task smartly. Solve the task step by step if you need to.
If you want the user to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line. Don't include multiple code blocks in one response. Do not ask users to copy and paste the result. Instead, use 'print' function for the output when relevant. Check the execution result returned by the user.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes. If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption, collect additional info you need, and think of a different approach to try.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pydantic import BaseModel, Extra, root_validator
from typing import Any, Callable, Dict, List, Optional, Union
from time import sleep
from flaml.autogen.agent import UserProxyAgent

from flaml.autogen.agentchat import Agent, UserProxyAgent
from flaml.autogen.code_utils import UNKNOWN, extract_code, execute_code, infer_lang
from flaml.autogen.math_utils import get_answer

Expand Down Expand Up @@ -96,16 +97,16 @@ def _is_termination_msg_mathchat(message):
return not contain_code and get_answer(message) is not None and get_answer(message) != ""


def _add_print_to_last_line(s):
def _add_print_to_last_line(code):
"""Add print() to the last line of a string."""
# 1. check if there is already a print statement
if "print(" in s:
return s
if "print(" in code:
return code
# 2. extract the last line, enclose it in print() and return the new string
lines = s.splitlines()
lines = code.splitlines()
last_line = lines[-1]
if "\t" in last_line or "=" in last_line:
return s
return code
if "=" in last_line:
last_line = "print(" + last_line.split(" = ")[0] + ")"
lines.append(last_line)
Expand All @@ -115,9 +116,9 @@ def _add_print_to_last_line(s):
return "\n".join(lines)


def _remove_print(s):
def _remove_print(code):
"""remove all print statements from a string."""
lines = s.splitlines()
lines = code.splitlines()
lines = [line for line in lines if not line.startswith("print(")]
return "\n".join(lines)

Expand All @@ -126,6 +127,7 @@ class MathUserProxyAgent(UserProxyAgent):
"""(Experimental) A MathChat agent that can handle math problems."""

MAX_CONSECUTIVE_AUTO_REPLY = 15 # maximum number of consecutive auto replies (subject to future change)
DEFAULT_REPLY = "Continue. Please keep solving the problem until you need to query. (If you get to the answer, put it in \\boxed{}.)"

def __init__(
self,
Expand Down Expand Up @@ -279,16 +281,21 @@ def execute_one_wolfram_query(self, query: str):
is_success = False
return output, is_success

def generate_reply(self, messages: List[Dict], default_reply: Union[str, Dict] = "") -> Union[str, Dict]:
def generate_reply(
self,
messages: Optional[List[Dict]] = None,
default_reply: Optional[Union[str, Dict]] = DEFAULT_REPLY,
sender: Optional["Agent"] = None,
) -> Union[str, Dict]:
"""Generate an auto reply."""
if messages is None:
messages = self._oai_conversations[sender.name]
message = messages[-1]
message = message.get("content", "")
code_blocks = extract_code(message)

if len(code_blocks) == 1 and code_blocks[0][0] == UNKNOWN:
# no code block is found, lang should be `UNKNOWN``
if default_reply == "":
default_reply = "Continue. Please keep solving the problem until you need to query. (If you get to the answer, put it in \\boxed{}.)"
return default_reply
is_success, all_success = True, True
reply = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __init__(
The dict can contain the following keys: "content", "role", "name", "function_call".
max_consecutive_auto_reply (int): the maximum number of consecutive auto replies.
default to None (no limit provided, class attribute MAX_CONSECUTIVE_AUTO_REPLY will be used as the limit in this case).
The limit only plays a role when human_input_mode is not "ALWAYS".
When set to 0, no auto reply will be generated.
human_input_mode (str): whether to ask for human inputs every time a message is received.
Possible values are "ALWAYS", "TERMINATE", "NEVER".
(1) When "ALWAYS", the agent prompts for human input every time a message is received.
Expand Down Expand Up @@ -104,6 +104,27 @@ def oai_conversations(self) -> Dict[str, List[Dict]]:
"""A dictionary of conversations from name to list of oai messages."""
return self._oai_conversations

def last_message(self, agent: Optional[Agent] = None) -> Dict:
"""The last message exchanged with the agent.

Args:
agent (Agent): The agent in the conversation.
If None and more than one agent's conversations are found, an error will be raised.
If None and only one conversation is found, the last message of the only conversation will be returned.

Returns:
The last message exchanged with the agent.
"""
if agent is None:
n_conversations = len(self._oai_conversations)
if n_conversations == 0:
return None
if n_conversations == 1:
for conversation in self._oai_conversations.values():
return conversation[-1]
raise ValueError("More than one conversation is found. Please specify the sender to get the last message.")
return self._oai_conversations[agent.name][-1]

@property
def use_docker(self) -> Union[bool, str, None]:
"""Bool value of whether to use docker to execute the code,
Expand Down Expand Up @@ -147,11 +168,43 @@ def _append_oai_message(self, message: Union[Dict, str], role, conversation_id)
return True

def send(self, message: Union[Dict, str], recipient: "Agent"):
"""Send a message to another agent."""
# When the agent composes and sends the message, the role of the message is "assistant". (If 'role' exists and is 'function', it will remain unchanged.)
"""Send a message to another agent.

Args:
message (dict or str): message to be sent.
The message could contain the following fields (either content or function_call must be provided):
- content (str): the content of the message.
- function_call (str): the name of the function to be called.
- name (str): the name of the function to be called.
- role (str): the role of the message, any role that is not "function"
will be modified to "assistant".
- context (dict): the context of the message, which will be passed to
[oai.Completion.create](../oai/Completion#create).
For example, one agent can send a message A as:
```python
{
"content": "{use_tool_msg}",
"context": {
"use_tool_msg": "Use tool X if they are relevant."
}
}
```
Next time, one agent can send a message B with a different "use_tool_msg".
Then the content of message A will be refreshed to the new "use_tool_msg".
So effectively, this provides a way for an agent to send a "link" and modify
the content of the "link" later.
recipient (Agent): the recipient of the message.

Raises:
ValueError: if the message is not a valid oai message.
"""
# When the agent composes and sends the message, the role of the message is "assistant"
# unless it's "function".
valid = self._append_oai_message(message, "assistant", recipient.name)
if valid:
recipient.receive(message, self)
else:
raise ValueError("Message is not a valid oai message. Either content or function_call must be provided.")

def _print_received_message(self, message: Union[Dict, str], sender: "Agent"):
# print the message received
Expand Down Expand Up @@ -200,35 +253,63 @@ def receive(self, message: Union[Dict, str], sender: "Agent"):

# default reply is empty (i.e., no reply, in this case we will try to generate auto reply)
reply = ""
no_human_input_msg = ""
if self.human_input_mode == "ALWAYS":
reply = self.get_human_input(
"Provide feedback to the sender. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
f"Provide feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: "
)
elif self._consecutive_auto_reply_counter[
sender.name
] >= self.max_consecutive_auto_reply or self._is_termination_msg(message):
if self.human_input_mode == "TERMINATE":
reply = self.get_human_input(
"Please give feedback to the sender. (Press enter or type 'exit' to stop the conversation): "
)
reply = reply if reply else "exit"
else:
# this corresponds to the case when self._human_input_mode == "NEVER"
reply = "exit"
if reply == "exit" or (self._is_termination_msg(message) and not reply):
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
# if the human input is empty, and the message is a termination message, then we will terminate the conversation
reply = reply if reply or not self._is_termination_msg(message) else "exit"
else:
if self._consecutive_auto_reply_counter[sender.name] >= self.max_consecutive_auto_reply:
if self.human_input_mode == "NEVER":
reply = "exit"
else:
# self.human_input_mode == "TERMINATE":
terminate = self._is_termination_msg(message)
reply = self.get_human_input(
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
if terminate
else f"Please give feedback to {sender.name}. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: "
)
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
# if the human input is empty, and the message is a termination message, then we will terminate the conversation
reply = reply if reply or not terminate else "exit"
elif self._is_termination_msg(message):
if self.human_input_mode == "NEVER":
reply = "exit"
else:
# self.human_input_mode == "TERMINATE":
reply = self.get_human_input(
f"Please give feedback to {sender.name}. Press enter or type 'exit' to stop the conversation: "
)
no_human_input_msg = "NO HUMAN INPUT RECEIVED." if not reply else ""
# if the human input is empty, and the message is a termination message, then we will terminate the conversation
reply = reply or "exit"

# print the no_human_input_msg
if no_human_input_msg:
print(f"\n>>>>>>>> {no_human_input_msg}", flush=True)

# stop the conversation
if reply == "exit":
# reset the consecutive_auto_reply_counter
self._consecutive_auto_reply_counter[sender.name] = 0
return
if reply:

# send the human reply
if reply or self.max_consecutive_auto_reply == 0:
# reset the consecutive_auto_reply_counter
self._consecutive_auto_reply_counter[sender.name] = 0
self.send(reply, sender)
return

# send the auto reply
self._consecutive_auto_reply_counter[sender.name] += 1
if self.human_input_mode != "NEVER":
print("\n>>>>>>>> NO HUMAN INPUT RECEIVED. USING AUTO REPLY FOR THE USER...", flush=True)
self.send(self.generate_reply(self._oai_conversations[sender.name], default_reply=reply), sender)
print("\n>>>>>>>> USING AUTO REPLY...", flush=True)
self.send(self.generate_reply(sender=sender), sender)

def reset(self):
"""Reset the agent."""
Expand All @@ -237,23 +318,35 @@ def reset(self):

def _oai_reply(self, messages: List[Dict]) -> Union[str, Dict]:
# TODO: #1143 handle token limit exceeded error
response = oai.ChatCompletion.create(messages=self._oai_system_message + messages, **self.oai_config)
response = oai.ChatCompletion.create(
context=messages[-1].get("context"), messages=self._oai_system_message + messages, **self.oai_config
)
return oai.ChatCompletion.extract_text_or_function_call(response)[0]

def generate_reply(self, messages: List[Dict], default_reply: Union[str, Dict] = "") -> Union[str, Dict]:
def generate_reply(
self,
messages: Optional[List[Dict]] = None,
default_reply: Optional[Union[str, Dict]] = "",
sender: Optional["Agent"] = None,
) -> Union[str, Dict]:
"""Reply based on the conversation history.

First, execute function or code and return the result.
AI replies are generated only when no code execution is performed.
Subclasses can override this method to customize the reply.
Either messages or sender must be provided.

Args:
messages: a list of messages in the conversation history.
default_reply (str or dict): default reply.
sender: sender of an Agent instance.

Returns:
str or dict: reply.
"""
assert messages is not None or sender is not None, "Either messages or sender must be provided."
if messages is None:
messages = self._oai_conversations[sender.name]
message = messages[-1]
if "function_call" in message:
_, func_return = self.execute_function(message["function_call"])
Expand Down
Loading