Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

add examples

Qingzheng Gaob2fcbd0241ab
diff --git a/memgpt/autogen/examples/agent_autoreply.py b/memgpt/autogen/examples/agent_autoreply.py
new file mode 100644
index 000000000..34cc0832e
--- /dev/null
+++ b/memgpt/autogen/examples/agent_autoreply.py
@@ -0,0 +1,66 @@
+"""Example of how to add MemGPT into an AutoGen groupchat
+
+Based on the official AutoGen example here: https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb
+
+Begin by doing:
+ pip install "pyautogen[teachable]"
+ pip install pymemgpt
+ or
+ pip install -e . (inside the MemGPT home directory)
+"""
+
+import os
+import autogen
+from memgpt.autogen.memgpt_agent import create_memgpt_autogen_agent_from_config
+
+config_list = [
+ {
+ "model": "gpt-4",
+ "api_key": os.getenv("OPENAI_API_KEY"),
+ },
+]
+
+# If USE_MEMGPT is False, then this example will be the same as the official AutoGen repo
+# (https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb)
+# If USE_MEMGPT is True, then we swap out the "coder" agent with a MemGPT agent
+USE_MEMGPT = False
+
+llm_config = {"config_list": config_list, "seed": 42}
+
+# The user agent
+user_proxy = autogen.UserProxyAgent(
+ name="User_proxy",
+ system_message="A human admin.",
+ code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"},
+ human_input_mode="TERMINATE", # needed?
+ default_auto_reply="You are going to figure all out by your own. "
+ "Work by yourself, the user won't reply until you output `TERMINATE` to end the conversation.",
+)
+
+if not USE_MEMGPT:
+ # In the AutoGen example, we create an AssistantAgent to play the role of the coder
+ coder = autogen.AssistantAgent(
+ name="Coder",
+ llm_config=llm_config,
+ system_message=f"I am a 10x engineer, trained in Python. I was the first engineer at Uber "
+ f"(which I make sure to tell everyone I work with).",
+ human_input_mode="TERMINATE",
+ )
+
+else:
+ # In our example, we swap this AutoGen agent with a MemGPT agent
+ # This MemGPT agent will have all the benefits of MemGPT, ie persistent memory, etc.
+ coder = create_memgpt_autogen_agent_from_config(
+ "MemGPT_coder",
+ llm_config=llm_config,
+ system_message=f"I am a 10x engineer, trained in Python. I was the first engineer at Uber "
+ f"(which I make sure to tell everyone I work with).",
+ human_input_mode="TERMINATE",
+ )
+
+# Begin the group chat with a message from the user
+user_proxy.initiate_chat(
+ coder,
+ message="I want to design an app to make me one million dollars in one month. "
+ "Tell me all the details, then try out every steps.",
+)
diff --git a/memgpt/autogen/examples/agent_groupchat.py b/memgpt/autogen/examples/agent_groupchat.py
index e4d8224eb..d6339baab 100644
--- a/memgpt/autogen/examples/agent_groupchat.py
+++ b/memgpt/autogen/examples/agent_groupchat.py
@@ -11,7 +11,7 @@ Begin by doing:
import os
import autogen
-from memgpt.autogen.memgpt_agent import create_autogen_memgpt_agent
+from memgpt.autogen.memgpt_agent import create_autogen_memgpt_agent, create_memgpt_autogen_agent_from_config
config_list = [
{
@@ -20,10 +20,13 @@ config_list = [
},
]
-# If USE_MEMGPT is False, then this example will be the same as the official AutoGen repo (https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb)
+# If USE_MEMGPT is False, then this example will be the same as the official AutoGen repo
+# (https://github.com/microsoft/autogen/blob/main/notebook/agentchat_groupchat.ipynb)
# If USE_MEMGPT is True, then we swap out the "coder" agent with a MemGPT agent
USE_MEMGPT = True
+USE_AUTOGEN_WORKFLOW = False
+
llm_config = {"config_list": config_list, "seed": 42}
# The user agent
@@ -51,13 +54,25 @@ if not USE_MEMGPT:
else:
# In our example, we swap this AutoGen agent with a MemGPT agent
# This MemGPT agent will have all the benefits of MemGPT, ie persistent memory, etc.
- coder = create_autogen_memgpt_agent(
- "MemGPT_coder",
- persona_description="I am a 10x engineer, trained in Python. I was the first engineer at Uber (which I make sure to tell everyone I work with).",
- user_description=f"You are participating in a group chat with a user ({user_proxy.name}) and a product manager ({pm.name}).",
- # extra options
- # interface_kwargs={"debug": True},
- )
+ if not USE_AUTOGEN_WORKFLOW:
+ coder = create_autogen_memgpt_agent(
+ "MemGPT_coder",
+ persona_description="I am a 10x engineer, trained in Python. I was the first engineer at Uber "
+ "(which I make sure to tell everyone I work with).",
+ user_description=f"You are participating in a group chat with a user ({user_proxy.name}) "
+ f"and a product manager ({pm.name}).",
+ # extra options
+ # interface_kwargs={"debug": True},
+ )
+ else:
+ coder = create_memgpt_autogen_agent_from_config(
+ "MemGPT_coder",
+ llm_config=llm_config,
+ system_message=f"I am a 10x engineer, trained in Python. I was the first engineer at Uber "
+ f"(which I make sure to tell everyone I work with).\n"
+ f"You are participating in a group chat with a user ({user_proxy.name}) "
+ f"and a product manager ({pm.name}).",
+ )
# Initialize the group chat between the user and two LLM agents (PM and coder)
groupchat = autogen.GroupChat(agents=[user_proxy, pm, coder], messages=[], max_round=12)
@@ -66,5 +81,6 @@ manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)
# Begin the group chat with a message from the user
user_proxy.initiate_chat(
manager,
- message="I want to design an app to make me one million dollars in one month. Yes, your heard that right.",
+ message="I want to design an app to make me one million dollars in one month. "
+ "Yes, your heard that right.",
)
diff --git a/memgpt/autogen/memgpt_agent.py b/memgpt/autogen/memgpt_agent.py
index 9a62331d1..5cf70b5ff 100644
--- a/memgpt/autogen/memgpt_agent.py
+++ b/memgpt/autogen/memgpt_agent.py
@@ -27,13 +27,13 @@ def create_memgpt_autogen_agent_from_config(
"""Construct AutoGen config workflow in a clean way."""
model = constants.DEFAULT_MEMGPT_MODEL if llm_config is None else llm_config["config_list"][0]["model"]
- persona_desc = personas.DEFAULT if system_message is "" else system_message
- if human_input_mode is "ALWAYS":
+ persona_desc = personas.DEFAULT if system_message == "" else system_message
+ if human_input_mode == "ALWAYS":
user_desc = humans.DEFAULT
- elif human_input_mode is "TERMINATE":
- user_desc = "Output `TERMINATE` to end the conversation."
+ elif human_input_mode == "TERMINATE":
+ user_desc = "Work by yourself, the user won't reply until you output `TERMINATE` to end the conversation."
else:
- user_desc = ""
+ user_desc = "Work by yourself, the user won't reply. Elaborate as much as possible."
if function_map is not None or code_execution_config is not None:
raise NotImplementedError
@@ -47,20 +47,37 @@ def create_memgpt_autogen_agent_from_config(
is_termination_msg=is_termination_msg,
)
- if human_input_mode is not "ALWAYS":
- user_proxy = UserProxyAgent(
- name="user_proxy",
- system_message=humans.DEFAULT,
- human_input_mode="NEVER",
- default_auto_reply=default_auto_reply,
+ if human_input_mode != "ALWAYS":
+ coop_agent1 = create_autogen_memgpt_agent(
+ name,
+ preset=presets.DEFAULT,
+ model=model,
+ persona_description=persona_desc,
+ user_description=user_desc,
+ is_termination_msg=is_termination_msg,
)
+ if default_auto_reply != "":
+ coop_agent2 = UserProxyAgent(
+ name,
+ human_input_mode="NEVER",
+ default_auto_reply=default_auto_reply,
+ )
+ else:
+ coop_agent2 = create_autogen_memgpt_agent(
+ name,
+ preset=presets.DEFAULT,
+ model=model,
+ persona_description=persona_desc,
+ user_description=user_desc,
+ is_termination_msg=is_termination_msg,
+ )
groupchat = GroupChat(
- agents=[user_proxy, autogen_memgpt_agent],
+ agents=[autogen_memgpt_agent, coop_agent1, coop_agent2],
messages=[],
max_round=12 if max_consecutive_auto_reply is None else max_consecutive_auto_reply
)
- manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)
+ manager = GroupChatManager(name=name, groupchat=groupchat, llm_config=llm_config)
return manager
else:
@@ -136,7 +153,7 @@ class MemGPTAgent(ConversableAgent):
self.messages_processed_up_to_idx = 0
self._is_termination_msg = (
- is_termination_msg if is_termination_msg is not None else (lambda x: x.get("content") == "TERMINATE")
+ is_termination_msg if is_termination_msg is not None else (lambda x: x == "TERMINATE")
)
def format_other_agent_message(self, msg):
@@ -188,6 +205,8 @@ class MemGPTAgent(ConversableAgent):
# Extend the MemGPT message list with multiple 'user' messages, then push the last one with agent.step()
self.agent.messages.extend(new_messages[:-1])
user_message = new_messages[-1]
+ elif len(new_messages) < 1:
+ user_message = ""
else:
user_message = new_messages[0]