Skip to content

Add MemGPT "Python Client" #697

@cpacker

Description

@cpacker

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions