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

Add MemGPT "Python Client" #697

Closed
cpacker opened this issue Dec 25, 2023 · 1 comment
Closed

Add MemGPT "Python Client" #697

cpacker opened this issue Dec 25, 2023 · 1 comment
Labels
API Related to MemGPT API enhancement New feature or request feature request priority Merge ASAP

Comments

@cpacker
Copy link
Owner

cpacker commented Dec 25, 2023

Is your feature request related to a problem? Please describe.

tl;dr support something similar to openai.client for python developers


Add a familiar python interface to the API server. In essence, instead of making python developers write REST/requests code or asking them to do this (taken from #689):

### imports
# (truncated)

### wrapper code
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}")

### Main loop

persona_desc = utils.get_persona_text("sam_pov.txt")
user_desc = utils.get_human_text("basic.txt")

# Create an AgentConfig option from the inputs
agent_config = AgentConfig(
    # name="agent_4",
    name="bill_gates",
    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,
)

Allow developers to use a client that sits inside the package and is a light wrapper on top of the API server (https://github.com/cpacker/MemGPT/blob/main/memgpt/server/server.py):

import memgpt.client as memgpt_client

client.create_agent(agent_config)
response_messages = client.send_user_message(agent_config)
# etcetc
@cpacker cpacker added enhancement New feature or request feature request API Related to MemGPT API priority Merge ASAP labels Dec 25, 2023
@BabellDev
Copy link
Contributor

Hi @cpacker just checking in to confirm will indeed be looking at this. Was quite busy over the weekend, but will be actively working on it over the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Related to MemGPT API enhancement New feature or request feature request priority Merge ASAP
Projects
None yet
Development

No branches or pull requests

2 participants