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

SQLAlchemy Warning Due to Duplicate Dynamic Model Class Creation in Python #735

Closed
NoahBoesen opened this issue Dec 29, 2023 · 3 comments
Closed

Comments

@NoahBoesen
Copy link

My code has become very slow, and it started when I received the following error:
image

Here is the text: c:\Users\memgpt\connectors\db.py:47: SAWarning: This declarative base already contains a class with the same class name and module name as memgpt.connectors.db.Memgpt_agent_testing_userModel, and will be replaced in the string-lookup table.
Model = type(class_name, (PassageModel,), {"tablename": table_name, "table_args": {"extend_existing": True}})

Please describe your setup

  • I have memgpt connected to postgres
  • I run memgpt dicetly from another pythonscript. (I have linked to the code later in the message)
  • MemGPT version = 0.2.7
  • I program on a computer with windows 11
  • I use the OpenAI Api

Here is my code:

import os
import openai

from pathlib import Path

from memgpt.agent import Agent as _Agent

from typing import Callable, Optional, List, Dict, Union, Any, Tuple

from memgpt.persistence_manager import LocalStateManager
import memgpt.constants as constants
import memgpt.system as system
import memgpt.utils as utils
import memgpt.presets.presets as presets
from memgpt.config import AgentConfig
from memgpt.interface import CLIInterface as interface
from memgpt.constants import MEMGPT_DIR




# Set the path to the .memgpt or config file
# MEMGPT_DIR = r"C:\Users\noahb\MemGPT\.memgpt"


def process_agent_step(agent, user_message, no_verify):
    """Copied from main.py (unpacks the content of agent.step())"""
    new_messages, heartbeat_request, function_failed, token_warning = agent.step(user_message, first_message=False, skip_verify=no_verify)

    skip_next_user_input = False
    if token_warning:
        user_message = system.get_token_limit_warning()
        skip_next_user_input = True
    elif function_failed:
        user_message = system.get_heartbeat(constants.FUNC_FAILED_HEARTBEAT_MESSAGE)
        skip_next_user_input = True
    elif heartbeat_request:
        user_message = system.get_heartbeat(constants.REQ_HEARTBEAT_MESSAGE)
        skip_next_user_input = True

    return new_messages, user_message, skip_next_user_input


def send_agent_a_message(agent, user_input, no_verify=False, allow_multi_step=True):
    """A convenience wrapper around the agent step, which:
    1. Packages the first message correctly
    2. Allows the agent to run back-to-back calls
    """
    # package the message
    user_message = system.package_user_message(user_input)

    while True:
        try:
            new_messages, user_message, skip_next_user_input = process_agent_step(agent, user_message, no_verify)
            if not allow_multi_step or not skip_next_user_input:
                break
        except KeyboardInterrupt:
            print("User interrupt occured.")
            input("Continue?")
        except Exception as e:
            print(f"An exception ocurred when running agent.step(): {e}")


# from dotenv import load_dotenv
# load_dotenv()

# Set your OpenAI API key
# os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
# cpacker: Assume I set the key with `export OPENAI_API_KEY=...`

# Point to your config file (or remove this line if you just want to use the default)
# hallo = os.environ["MEMGPT_CONFIG_PATH"] = Path.home().joinpath(".memgpt").joinpath("config").as_posix()
hallo = MEMGPT_DIR


# Necessary to avoid errors when LLM is invoked
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_base = "https://api.openai.com/v1"

# persona_desc = utils.get_persona_text(constants.DEFAULT_PERSONA)
persona_desc = utils.get_persona_text("1440.txt")
user_desc = utils.get_human_text("starter.txt")


# Create an AgentConfig option from the inputs
agent_config = AgentConfig(
    # name="agent_4",
    name="testing_user",
    persona=persona_desc,
    human=user_desc,
    preset="memgpt_chat",
    model="gpt-4",
    model_endpoint_type="openai",
    model_endpoint="https://api.openai.com/v1",
    context_window=8192,
)

skip_verify = agent_config.model != "gpt-4"



USER_INPUT = ""

# NEW_AGENT = False
NEW_AGENT = False
if NEW_AGENT:
    persistence_manager = LocalStateManager(agent_config)

    memgpt_agent = presets.use_preset(
        preset_name=agent_config.preset,
        agent_config=agent_config,
        model=agent_config.model,
        persona=agent_config.persona,
        human=agent_config.human,
        interface=interface,
        persistence_manager=persistence_manager,
    )

    # print("agent messages:", memgpt_agent.messages)
    print("agent memories:", str(memgpt_agent.memory))

    print("DEBUG first step")
    # memgpt_agent.step(user_message="Hi my name is Bill Gates.  I love the Windows OS!", first_message=True, skip_verify=skip_verify)
    send_agent_a_message(agent=memgpt_agent, user_input="Hi my name is Bill Gates.  I love the Windows OS!")
    print("DEBUG second step")
    input("continue?")
    # memgpt_agent.step(user_message="I am 68 years old, born October 28, 1955", first_message=False, skip_verify=skip_verify)
    send_agent_a_message(agent=memgpt_agent, user_input="I am 68 years old, born October 28, 1955")
    print("DEBUG saving")

    # print("agent messages:", memgpt_agent.messages)
    print("agent memories:", str(memgpt_agent.memory))
    memgpt_agent.save()
else:
    memgpt_agent = _Agent.load_agent(interface, agent_config)
    # memgpt_agent.step(user_message="What have i told you that i like to drink?", first_message=True, skip_verify=skip_verify)

    # Let's test if the agent remembers my name
    # memgpt_agent.step(user_message="Who am I?", first_message=False, skip_verify=skip_verify)
    send_agent_a_message(agent=memgpt_agent, user_input="What country am i from?")
    memgpt_agent.save()
@stracker-phil
Copy link

stracker-phil commented Jan 1, 2024

I also started seeing the same warning after updating MemGPT to the latest version.

  • MemGPT version 0.2.10
  • Archival Storage: PostgreSQL
❯ memgpt run      

? Would you like to select an existing agent? Yes
? Select agent: agent_2

🔁 Using existing agent agent_2

.../MemGPT/memgpt/connectors/db.py:47: SAWarning: This declarative base already contains a class
with the same class name and module name as memgpt.connectors.db.Memgpt_agent_agent_2Model, and
will be replaced in the string-lookup table.
  Model = type(class_name, (PassageModel,), {"__tablename__": table_name, "__table_args__":
  {"extend_existing": True}})

Hit enter to begin (will request first MemGPT message)

@NoahBoesen
Copy link
Author

Hmmm yeah, it's strange. Have you found a solution? @stracker-phil

Copy link

github-actions bot commented Dec 6, 2024

This issue has been automatically closed due to 60 days of inactivity.

@github-actions github-actions bot closed this as completed Dec 6, 2024
carenthomas pushed a commit that referenced this issue Jan 24, 2025
Co-authored-by: Charles Packer <packercharles@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants