Files
letta-server/docs/python_client.md
Owen Easter 796c33f1a6 docs: linting, syntax, formatting & spelling fixes for all files (#761)
* Update README.md

* fix: 'ollama run' should be 'ollama pull'

* fix: linting, syntax, spelling corrections for all docs

* fix: markdown linting rules and missed fixes

* fix: readded space to block

* fix: changed sh blocks to text

* docs: added exception for bare urls in markdown

* docs: added exception for in-line html (MD033/no-inline-html)

* docs: made python indentation level consistent (4 space tabs) even though I prefer 2.

---------

Co-authored-by: Charles Packer <packercharles@gmail.com>
2024-01-02 10:31:50 -08:00

2.9 KiB

title, excerpt, category
title excerpt category
Python client Developing using the MemGPT Python client 6580dab16cade8003f996d17

The fastest way to integrate MemGPT with your own Python projects is through the MemGPT client class:

from memgpt import MemGPT

# Create a MemGPT client object (sets up the persistent state)
client = MemGPT(
    quickstart="openai",
    config={
      "openai_api_key": "YOUR_API_KEY"
    }
)

# You can set many more parameters, this is just a basic example
agent_id = client.create_agent(
    agent_config={
      "persona": "sam_pov",
      "user": "cs_phd",
    }
)

# Now that we have an agent_name identifier, we can send it a message!
# The response will have data from the MemGPT agent
my_message = "Hi MemGPT! How's it going?"
response = client.user_message(agent_id=agent_id, message=my_message)

More in-depth example of using the MemGPT Python client

from memgpt.config import AgentConfig
from memgpt import MemGPT
from memgpt import constants
from memgpt.cli.cli import QuickstartChoice


client = MemGPT(
    # When auto_save is 'True' then the agent(s) will be saved after every
    # user message.  This may have performance implications, so you
    # can otherwise choose when to save explicitly using client.save().
    auto_save=True,

    # Quickstart will automatically configure MemGPT (without having to run `memgpt configure`
    # If you choose 'openai' then you must set the api key (env or in config)
    quickstart=QuickstartChoice.memgpt_hosted,

    # Allows you to override default config generated by quickstart or `memgpt configure`
    config={}
)

# Create an AgentConfig with default persona and human txt
# In this case, assume we wrote a custom persona file "my_persona.txt", located at ~/.memgpt/personas/my_persona.txt
# Same for a custom user file "my_user.txt", located at ~/.memgpt/humans/my_user.txt
agent_config = AgentConfig(
    name="CustomAgent",
    persona="my_persona",
    human="my_user",
    preset="memgpt_chat",
    model="gpt-4",
)

# Create the agent according to AgentConfig we set up. If an agent with
# the same name already exists it will simply return, unless you set
# throw_if_exists to 'True'
agent_id = client.create_agent(agent_config=agent_config)

# Create a helper that sends a message and prints the assistant response only
def send_message(message: str):
    """
    sends a message and prints the assistant output only.
    :param message: the message to send
    """
    response = client.user_message(agent_id=agent_id, message=message)
    for r in response:
        # Can also handle other types "function_call", "function_return", "function_message"
        if "assistant_message" in r:
            print("ASSISTANT:", r["assistant_message"])
        elif "thoughts" in r:
            print("THOUGHTS:", r["internal_monologue"])

# Send a message and see the response
send_message("Please introduce yourself and tell me about your abilities!")