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.

Support for MemGPT + Autogen + Local LLM (#231)

Vivian Fang5a60e92e8d16
diff --git a/memgpt/autogen/README.md b/memgpt/autogen/README.md
new file mode 100644
index 000000000..75dc84519
--- /dev/null
+++ b/memgpt/autogen/README.md
@@ -0,0 +1,23 @@
+# MemGPT + Autogen examples
+[examples/agent_groupchat.py](examples/agent_groupchat.py) contains an example of a groupchat where one of the agents is powered by MemGPT.
+
+**Local LLM support**
+In order to run MemGPT+Autogen on a local LLM, uncomment lines 31-46 in [examples/agent_groupchat.py](exmaples/agent_groupchat.py) and fill in the config files with your local LLM's deployment details. For example, if you are using webui, it will look something like this:
+
+```
+config_list = [
+ {
+ "model": "dolphin-2.1-mistral-7b",
+ "api_base": "http://127.0.0.1:5001/v1"
+ "api_key": "NULL", # this is a placeholder
+ "api_type": "open_ai",
+ },
+]
+config_list_memgpt = [
+ {
+ "model": "dolphin-2.1-mistral-7b",
+ },
+]
+```
+
+Also be sure to export `OPENAI_API_BASE` and `BACKEND_TYPE` as described in [Local LLM support](../local_llm).
\ No newline at end of file
diff --git a/memgpt/autogen/examples/agent_groupchat.py b/memgpt/autogen/examples/agent_groupchat.py
index fd45c5bb6..bcd73e0bc 100644
--- a/memgpt/autogen/examples/agent_groupchat.py
+++ b/memgpt/autogen/examples/agent_groupchat.py
@@ -9,25 +9,70 @@ Begin by doing:
pip install -e . (inside the MemGPT home directory)
"""
+
import os
import autogen
from memgpt.autogen.memgpt_agent import create_autogen_memgpt_agent, create_memgpt_autogen_agent_from_config
+# This config is for autogen agents that are not powered by MemGPT
config_list = [
{
"model": "gpt-4",
"api_key": os.getenv("OPENAI_API_KEY"),
+ }
+]
+
+# This config is for autogen agents that powered by MemGPT
+config_list_memgpt = [
+ {
+ "model": "gpt-4",
},
]
+# Uncomment and fill in the following for local LLM deployment:
+# # This config is for autogen agents that are not powered by MemGPT
+# # See https://github.com/oobabooga/text-generation-webui/tree/main/extensions/openai
+config_list = [
+ {
+ "model": "YOUR_MODEL", # ex. This is the model name, not the wrapper
+ "api_base": "YOUR_URL", # ex. "http://127.0.0.1:5001/v1" if you are using webui, "http://localhost:1234/v1/" if you are using LM Studio
+ "api_key": "NULL", # this is a placeholder
+ "api_type": "open_ai",
+ },
+]
+
+# # This config is for autogen agents that powered by MemGPT
+# # For this to work, you need to have your environment variables set correctly, e.g.
+# # For web UI:
+# # OPENAI_API_BASE=http://127.0.0.1:5000
+# # BACKEND_TYPE=webui
+# # For LM Studio:
+# # OPENAI_API_BASE=http://127.0.0.1:1234
+# # BACKEND_TYPE=lmstudio
+# # "model" here specifies the "wrapper" that will be used, setting it to "gpt-4" uses the default
+config_list_memgpt = [
+ {"model": "airoboros-l2-70b-2.1"}, # if you set this to gpt-4, it will fall back to the default wrapper
+]
+
+
# 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
+USE_AUTOGEN_WORKFLOW = True
+
+# Set to True if you want to print MemGPT's inner workings.
+DEBUG = False
+
+interface_kwargs = {
+ "debug": DEBUG,
+ "show_inner_thoughts": DEBUG,
+ "show_function_outputs": DEBUG,
+}
llm_config = {"config_list": config_list, "seed": 42}
+llm_config_memgpt = {"config_list": config_list_memgpt, "seed": 42}
# The user agent
user_proxy = autogen.UserProxyAgent(
@@ -35,6 +80,7 @@ user_proxy = autogen.UserProxyAgent(
system_message="A human admin.",
code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"},
human_input_mode="TERMINATE", # needed?
+ default_auto_reply="...", # Set a default auto-reply message here (non-empty auto-reply is required for LM Studio)
)
# The agent playing the role of the product manager (PM)
@@ -42,6 +88,7 @@ pm = autogen.AssistantAgent(
name="Product_manager",
system_message="Creative in software product ideas.",
llm_config=llm_config,
+ default_auto_reply="...", # Set a default auto-reply message here (non-empty auto-reply is required for LM Studio)
)
if not USE_MEMGPT:
@@ -61,17 +108,18 @@ else:
"(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": False, "show_inner_thoughts": True, "show_function_outputs": True},
+ model=config_list_memgpt[0]["model"],
+ interface_kwargs=interface_kwargs,
)
else:
coder = create_memgpt_autogen_agent_from_config(
"MemGPT_coder",
- llm_config=llm_config,
+ llm_config=llm_config_memgpt,
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}).",
+ interface_kwargs=interface_kwargs,
)
# Initialize the group chat between the user and two LLM agents (PM and coder)
diff --git a/memgpt/autogen/memgpt_agent.py b/memgpt/autogen/memgpt_agent.py
index 4032ac7fa..feeb57b55 100644
--- a/memgpt/autogen/memgpt_agent.py
+++ b/memgpt/autogen/memgpt_agent.py
@@ -24,9 +24,13 @@ def create_memgpt_autogen_agent_from_config(
code_execution_config: Optional[Union[Dict, bool]] = None,
llm_config: Optional[Union[Dict, bool]] = None,
default_auto_reply: Optional[Union[str, Dict, None]] = "",
+ interface_kwargs: Dict = None,
):
"""Construct AutoGen config workflow in a clean way."""
+ if interface_kwargs is None:
+ interface_kwargs = {}
+
model = constants.DEFAULT_MEMGPT_MODEL if llm_config is None else llm_config["config_list"][0]["model"]
persona_desc = personas.DEFAULT if system_message == "" else system_message
if human_input_mode == "ALWAYS":
@@ -41,21 +45,23 @@ def create_memgpt_autogen_agent_from_config(
autogen_memgpt_agent = create_autogen_memgpt_agent(
name,
- preset=presets.DEFAULT_PRESET,
+ preset=presets.SYNC_CHAT,
model=model,
persona_description=persona_desc,
user_description=user_desc,
is_termination_msg=is_termination_msg,
+ interface_kwargs=interface_kwargs,
)
if human_input_mode != "ALWAYS":
coop_agent1 = create_autogen_memgpt_agent(
name,
- preset=presets.DEFAULT_PRESET,
+ preset=presets.SYNC_CHAT,
model=model,
persona_description=persona_desc,
user_description=user_desc,
is_termination_msg=is_termination_msg,
+ interface_kwargs=interface_kwargs,
)
if default_auto_reply != "":
coop_agent2 = UserProxyAgent(
@@ -66,11 +72,12 @@ def create_memgpt_autogen_agent_from_config(
else:
coop_agent2 = create_autogen_memgpt_agent(
name,
- preset=presets.DEFAULT_PRESET,
+ preset=presets.SYNC_CHAT,
model=model,
persona_description=persona_desc,
user_description=user_desc,
is_termination_msg=is_termination_msg,
+ interface_kwargs=interface_kwargs,
)
groupchat = GroupChat(
diff --git a/memgpt/local_llm/chat_completion_proxy.py b/memgpt/local_llm/chat_completion_proxy.py
index 4d9e82e63..4c7216f43 100644
--- a/memgpt/local_llm/chat_completion_proxy.py
+++ b/memgpt/local_llm/chat_completion_proxy.py
@@ -15,7 +15,7 @@ DEBUG = False
DEFAULT_WRAPPER = airoboros.Airoboros21InnerMonologueWrapper()
-async def get_chat_completion(
+def get_chat_completion(
model, # no model, since the model is fixed to whatever you set in your own backend
messages,
functions,
diff --git a/memgpt/openai_tools.py b/memgpt/openai_tools.py
index 7a5f1f316..bb4f7f7a9 100644
--- a/memgpt/openai_tools.py
+++ b/memgpt/openai_tools.py
@@ -58,7 +58,20 @@ def retry_with_exponential_backoff(
@retry_with_exponential_backoff
def completions_with_backoff(**kwargs):
- return openai.ChatCompletion.create(**kwargs)
+ # Local model
+ if HOST_TYPE is not None:
+ return get_chat_completion(**kwargs)
+
+ # OpenAI / Azure model
+ else:
+ if using_azure():
+ azure_openai_deployment = os.getenv("AZURE_OPENAI_DEPLOYMENT")
+ if azure_openai_deployment is not None:
+ kwargs["deployment_id"] = azure_openai_deployment
+ else:
+ kwargs["engine"] = MODEL_TO_AZURE_ENGINE[kwargs["model"]]
+ kwargs.pop("model")
+ return openai.ChatCompletion.create(**kwargs)
def aretry_with_exponential_backoff(
@@ -108,7 +121,7 @@ def aretry_with_exponential_backoff(
async def acompletions_with_backoff(**kwargs):
# Local model
if HOST_TYPE is not None:
- return await get_chat_completion(**kwargs)
+ return get_chat_completion(**kwargs)
# OpenAI / Azure model
else:
diff --git a/memgpt/utils.py b/memgpt/utils.py
index 6450d85ff..c2e4710a2 100644
--- a/memgpt/utils.py
+++ b/memgpt/utils.py
@@ -20,8 +20,6 @@ from memgpt.constants import MEMGPT_DIR
from llama_index import set_global_service_context, ServiceContext, VectorStoreIndex, load_index_from_storage, StorageContext
from llama_index.embeddings import OpenAIEmbedding
-from memgpt.embeddings import embedding_model
-
def count_tokens(s: str, model: str = "gpt-4") -> int:
encoding = tiktoken.encoding_for_model(model)
@@ -156,7 +154,7 @@ def chunk_file(file, tkns_per_chunk=300, model="gpt-4"):
encoding = tiktoken.encoding_for_model(model)
if file.endswith(".db"):
- return # can't read the sqlite db this way, will get handled in main.py
+ return # can't read the sqlite db this way, will get handled in main.py
with open(file, "r") as f:
if file.endswith(".pdf"):
@@ -372,6 +370,7 @@ def get_index(name, docs):
:type docs: List[Document]
"""
from memgpt.config import MemGPTConfig # avoid circular import
+ from memgpt.embeddings import embedding_model # avoid circular import
# TODO: configure to work for local
print("Warning: get_index(docs) only supported for OpenAI")